【问题标题】:Executing command remotely on another linux server from a shell script从 shell 脚本在另一台 linux 服务器上远程执行命令
【发布时间】:2015-04-24 13:27:36
【问题描述】:

我有一个 shell 脚本,它从两个日期之间的日志文件中提取详细信息,并在输出上执行命令以生成一些报告。日志文件位于不同的服务器上,脚本在不同的服务器上执行。脚本看起来像:

#!/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

当命令直接在服务器上执行时,它会生成所需的输出,但在远程执行时不会。
所以我的问题是如何获得正确的输出,这是一个好方法吗?

【问题讨论】:

  • 反引号中的echo 不仅愚蠢,而且是错误的。只需将echo `whatever` 替换为whatever。另见porkmail.org/era/unix/award.html#echo
  • 删除反引号不会产生任何输出。
  • 谢谢你,我明白了,反引号忽略了\n。所以我所做的是在 " " 中添加命令。像这样:echo "`command`"

标签: linux bash shell ssh server


【解决方案1】:

从上面的 cmets 中,您仍在使用“echo”。 @tripleee 是正确的。别。也不要在开始时使用无意义的 cat - 只需将 std in 重定向到您的下一个命令。

#!/bin/sh
time1=$1
time2=$2
ssh -i key  user@ip -yes cat file | awk '{if(substr($4,2)>="'$time1'" && substr($4,2)<="'$time2'") print $0;}' > tmp.log

some operation to get detail <tmp.log

【讨论】:

    猜你喜欢
    • 2017-09-26
    • 2017-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-24
    • 2019-09-02
    • 1970-01-01
    • 2021-11-27
    相关资源
    最近更新 更多