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

参考链接

https://blog.csdn.net/lidan3959/article/details/20452717?tdsourcetag=s_pctim_aiomsg

posted @ 2019-07-23 15:32  波士地盘  阅读(1170)  评论(0编辑  收藏  举报