linux while循环读文件,linux shell脚本用while read逐行读取文本的问题

1、while使用重定向机制,ip.txt文件中的信息都已经读入并重定向给了整个while语句。所以当我们在while循环中再一次调用read语句,就会读取到下一条记录。问题就出在这里,ssh语句正好回读取输入中的所有东西。为了禁止ssh读所有东西增加一个< /dev/null,将ssh 的输入重定向输入

while read ips;

do

echo $ips;

upt=`ssh root@${ip1} uptime < /dev/null;`;

echo $upt;

done < ip.txt;

2、用for

for ips in `cat ip.txt`; do

echo ${ips};

upt=`ssh root@${ips} uptime`;

echo $upt;

done

1、为什么用2、兄弟提到for语句,也写得很精辟。for语句怎么不存在while语句刚才的那个问题?

3、刚才楼上有个兄弟提到用-n参数,我试过也是可以的,你可以帮他回答一下为什么这个-n也可以的吗?

1、< /dev/null是告诉ssh如果要获取就从/dev/null获取,避免了获取while缓存

2、for没有一次把文件内容缓存获取过来

3、看一下man ssh -n参数 Redirects stdin from /dev/null (actually, prevents reading from stdin).和

收集了一下,ssh的命令解释:Redirects stdin from /dev/null (actually, prevents reading from stdin). This must be used when ssh is run in the background. A common trick is to use this to run X11 programs on a remote machine. For example, ssh -n shadows.cs.hut.fi emacs & will start an emacs on shadows.cs.hut.fi, and the X11 connection will be automatically forwarded over an encrypted channel. The ssh program will be put in the background. (This does not work if ssh needs to ask for a password or passphrase; see also the -f option.)

也就是防止stdin阅读到空的内容,标准输入的内容有了保障,那么你的循环就会循环到你的标准输入到没有为止。可以供你参考一下。

ssh是一个加密通道命令,以我的理解来看,就是会把你之前的那些stdin给加密没了,循环也就继续不下去了。加入了-n就防止了这种情况发生。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值