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