击而迭代循环只有一次,每当体内含有SSH [英] Bash while loop iterates only once whenever body contains ssh

查看:152
本文介绍了击而迭代循环只有一次,每当体内含有SSH的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是从一个文本文件中读取主机信息,并把它传递给一个SSH命令:
该文本文件包含ssh命令主机,用户名和密码

I'm reading host information from a text file and pass it to an ssh command: The text file contains the host, user and password for the ssh command

while read LINE
do
    R_USER=$(echo $LINE | cut -d ',' -f 1)                  
    R_HOST=$(echo $LINE | cut -d ',' -f 2)                                  
    PY_SCRIPT=$(echo $LINE | cut -d ',' -f 4)               

    ssh $R_USER@$R_HOST 'touch /home/user/file_name.txt'

done </path_name/file_name

事实证明仅执行while循环一次,即使主机文本文件包含多个主机的信息。
当我删除ssh命令while循环被执行,因为在主机信息的文本文件行之多。

As it turns out the while loop is only executed once even if the host text file contains multiple host information. When I remove the ssh command the while loop gets executed as much as there are lines in the host information text file.

不知道为什么会这样。
这方面的消息?

Not sure why this is so. Any information on this?

罗兰

推荐答案

默认的标准输入的处理SSH的水渠从while循环中剩余的线。

The default standard input handling of ssh drains the remaining line from the while loop.

要避免这个问题,改变那里的问题命令从标准输入读取。如果没有标准输入需要被传递到命令,从特殊的/ dev / null的设备读取标准输入:

To avoid this problem, alter where the problematic command reads standard input from. If no standard input need be passed to the command, read standard input from the special /dev/null device:

while read LINE
  do
    R_USER=$(echo $LINE | cut -d ',' -f 1)                  
    R_HOST=$(echo $LINE | cut -d ',' -f 2)                                  
    PY_SCRIPT=$(echo $LINE | cut -d ',' -f 4)               

    ssh $R_USER@$R_HOST 'touch /home/user/file_name.txt' < /dev/null

done </path_name/file_name

或者,尝试使用 SSH -n 将美元,从标准输入读取p $ pvent SSH。例如:

Or alternatively, try using ssh -n which will prevent ssh from reading from standard input. For instance:

ssh -n $R_USER@$R_HOST 'touch /home/user/file_name.txt'

这篇关于击而迭代循环只有一次,每当体内含有SSH的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆