在while中的ssh_shell

small parking
Table of Contents

在while中的ssh

现象

files 有三行

echo  -e '1\n2\n3' > files
cat files
1
2
3

运行截图

while只会执行一次就停止。

如何修复

while read line
do
    echo $line xxx
    ssh xxxx "touch ss" < /dev/null
done < files

注意: 这个 < /dev/null 是关键

或者换成ssh -n

具体原因: 因为while 在读取文件 从stdin读取文件,然后ssh 也在使用stdin,ssh如果使用了stdin ,那while 就没有stdin了,因此while只会执行一次就停止。

将 stdin 从 /dev/null 重定向(实际上可阻止读取 stdin)。 当在后台运行 ssh 时,必须使用此功能。

stackoverflow