通过 shell 脚本在另一台 linux 服务器上远程执行命令
Executing command remotely on another linux server from a shell script
我有一个 shell-脚本,它从两个日期之间的日志文件中提取详细信息,并对输出执行命令以生成一些报告。日志文件在不同的服务器上,脚本在不同的服务器上执行。脚本看起来像:
#!/bin/sh
time1=
time2=
echo `ssh -i key user@ip -yes cat file | awk '{if(substr(,2)>="'$time1'" && substr(,2)<="'$time2'") print [=12=];}'` > tmp.log
`cat tmp.log | some operation to get detail`
输出预计为多行,如:
ip1 date1 - - request1 file1 method1 status1
ip2 date2 - - request2 file2 method2 status2
但是(通过我的命令)生成的输出被连接成一行,包含所有详细信息,例如:
ip1 date1 - - request1 file1 method1 status1 ip2 date2 - - request2 file2 method2 status2
当命令直接在服务器上执行时,它会生成所需的输出,但在远程执行时不会。
所以我的问题是如何获得正确的输出,这是一个好方法吗?
根据您上面的评论,您仍在使用 "echo"。 @tripleee 是正确的。不。也不要在开始时使用毫无意义的 cat - 只需将 std 重定向到你的下一个命令。
#!/bin/sh
time1=
time2=
ssh -i key user@ip -yes cat file | awk '{if(substr(,2)>="'$time1'" && substr(,2)<="'$time2'") print [=10=];}' > tmp.log
some operation to get detail <tmp.log
我有一个 shell-脚本,它从两个日期之间的日志文件中提取详细信息,并对输出执行命令以生成一些报告。日志文件在不同的服务器上,脚本在不同的服务器上执行。脚本看起来像:
#!/bin/sh
time1=
time2=
echo `ssh -i key user@ip -yes cat file | awk '{if(substr(,2)>="'$time1'" && substr(,2)<="'$time2'") print [=12=];}'` > tmp.log
`cat tmp.log | some operation to get detail`
输出预计为多行,如:
ip1 date1 - - request1 file1 method1 status1
ip2 date2 - - request2 file2 method2 status2
但是(通过我的命令)生成的输出被连接成一行,包含所有详细信息,例如:
ip1 date1 - - request1 file1 method1 status1 ip2 date2 - - request2 file2 method2 status2
当命令直接在服务器上执行时,它会生成所需的输出,但在远程执行时不会。
所以我的问题是如何获得正确的输出,这是一个好方法吗?
根据您上面的评论,您仍在使用 "echo"。 @tripleee 是正确的。不。也不要在开始时使用毫无意义的 cat - 只需将 std 重定向到你的下一个命令。
#!/bin/sh
time1=
time2=
ssh -i key user@ip -yes cat file | awk '{if(substr(,2)>="'$time1'" && substr(,2)<="'$time2'") print [=10=];}' > tmp.log
some operation to get detail <tmp.log