我有一个shell脚本,从两个date之间的日志文件中提取详细信息,并在输出上执行一个命令以生成一些报告。日志文件位于不同的服务器上,脚本在不同的服务器上执行。 脚本看起来像:
#!/bin/sh time1=$1 time2=$2 echo `ssh -i key user@ip -yes cat file | awk '{if(substr($4,2)>="'$time1'" && substr($4,2)<="'$time2'") print $0;}'` > 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
当直接在服务器上运行相同的命令时,它会生成所需的输出,但不能在远程执行时输出
所以我的问题是如何得到正确的输出,这是一个很好的方法来做到这一点?
正如在各个地方(如http://unix.derkeiler.com/Newsgroups/comp.unix.shell/2007-05/msg00584.html )中所解释的那样,echo输出由空格分隔的参数
因此,您必须将反引号中的命令结果作为一个parameter passing
echo "`some command that outputs multiple lines here`"
或者不使用echo但例如printf