【问题标题】:Bash: ssh input to while loopBash:ssh 输入到 while 循环
【发布时间】:2015-12-11 23:55:36
【问题描述】:

我正在尝试查询远程服务器上的日志文件的内容,但是,我似乎无法让它工作。

#!/bin/bash
while read line; do
        echo "Do stuff to the file, line by line"
done < ( ssh -n user@server "cat /path/to/file" )

第一个括号出现语法错误。如果我删除括号,我会在“-n”标志处收到语法错误。

一切都在 shell 中正常工作,所以我假设这里有一些我没有正确理解的行为。

谢谢!

【问题讨论】:

    标签: bash loops ssh while-loop aix


    【解决方案1】:

    您需要另一个&lt; 来替换进程。

    #!/bin/bash
    while read line; do
            echo "Do stuff to the file, line by line"
    done < <( ssh -n user@server "cat /path/to/file" )
    

    第一个&lt; 指定输入重定向。 &lt;(...) 构造是进程替换,类似于命令替换,但将封闭命令的输出视为文件,而不是字符串。

    【讨论】:

      猜你喜欢
      • 2013-11-16
      • 2020-02-15
      • 2018-03-23
      • 2021-05-13
      • 2016-04-26
      • 2013-04-23
      • 2015-12-09
      • 2012-10-13
      • 2013-09-06
      相关资源
      最近更新 更多