shell 脚本中 while 只执行一次

实例代码

while read line ;
do
ssh  -p20002 $line  -o StrictHostKeyChecking=no xxxxxxxxx
done < ip.txt

while 只循环一次原因:

while 读取文本的第一行,然后执行循环,执行到ssh这部时,因为ssh会读取标准输入(stdin),把所有文本都读取了,因此while没有数据,便跳出循环。

解决方法:

在ssh中使用 -n 参数;


-n Redirects stdin from /dev/null (actually, prevents reading from stdin).This must be used when ssh is run in the background.

即将标准输入重定向到/dev/null

猜你喜欢

转载自www.cnblogs.com/boxker/p/11232051.html
今日推荐