简体   繁体   English

while循环在shell脚本中不起作用

[英]while loop is not working in shell script

Wednesday, May 21, 2014 10:09 AM I am running below two programs 星期三,2014年5月21日上午10:09我在两个程序下运行

first program: 第一个程序:

while read line

do 

if (cat /etc/passwd | grep -w ^"userid")
then
echo $line
fi
done < serverlist

output: 输出:

userid:kjhfkjshd:hjgf:hsgdf:hsgbdf
server1
userid:kjhfkjshd:hjgf:hsgdf:hsgbdf
server2

second program: 第二个程序:

while read line
do
if ( sudo /bin/ssh $line /bin/cat /etc/passwd | grep -w ^"userid" )
then
echo $line
fi
done < serverlist

OUTPUT OUTPUT

userid:kjhfkjshd:hjgf:hsgdf:hsgbdf
server1

it is not going to check second server. 它不会检查第二台服务器。 i am not able to find what is wrong in second program. 我无法找到第二个程序的错误。 first program is fine. 第一个程序没问题。 can anybody help me please? 有人可以帮我吗?

This is a common perceived "problem" when using ssh in a loop. 在循环中使用ssh时,这是一个常见的“问题”。 The root cause is that, when ssh is spawned the first time, it consumes the rest of your input, so on the second iteration of your loop, it finds that there is nothing to do and terminates. 根本原因是,当第一次生成ssh时,它会消耗你输入的其余部分,因此在循环的第二次迭代中,它会发现无事可做并终止。

The way to get around this is to feed /dev/null to ssh as input, either by doing ssh -n ... or a more explicit ssh ... < /dev/null . 解决这个问题的方法是将/dev/null作为输入提供给ssh ,方法是执行ssh -n ...或更明确的ssh ... < /dev/null

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM