Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 循环只在主体中使用'ssh'迭代一次_Bash_Ssh - Fatal编程技术网

Bash 循环只在主体中使用'ssh'迭代一次

Bash 循环只在主体中使用'ssh'迭代一次,bash,ssh,Bash,Ssh,我经常看到一些基本上与…相同的问题 当我在循环体中调用ssh时,我的while循环只迭代一次 while read -r line; do ssh somehost "command $line" done < argument_list.txt 读取-r行时;做 ssh somehost“命令$line” 完成

我经常看到一些基本上与…相同的问题


当我在循环体中调用
ssh
时,我的
while
循环只迭代一次

while read -r line; do
    ssh somehost "command $line"
done < argument_list.txt
读取-r行时
;做
ssh somehost“命令$line”
完成


ssh
也从标准输入读取,因此在下一次调用
read
之前,对
ssh
的第一次调用将消耗
参数列表.txt
的其余部分。要修复此问题,请使用以下任一方法从
/dev/null
重定向
ssh
的标准输入

ssh somehost "command $line" < /dev/stdin
如果
ssh
确实需要从标准输入中读取数据,您不希望它从
参数\u list.txt
中读取更多数据。在这种情况下,需要为while循环使用不同的文件描述符

while read -r line <&3; do
    ssh somehost "command $line"
done 3< argument_list.txt
while read -r line <&3; do
    ssh somehost "command $line"
done 3< argument_list.txt
while read -r -u 3 line; do
    ssh somehost "command $line"
done 3< argument_list.txt