user3180371
user3180371

Reputation: 37

while loop is not working in shell script

Wednesday, May 21, 2014 10:09 AM I am running below two programs

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

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?

Upvotes: 0

Views: 759

Answers (1)

twalberg
twalberg

Reputation: 62379

This is a common perceived "problem" when using ssh in a loop. 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.

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.

Upvotes: 2

Related Questions