0

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?

1
  • i tried with adding echo $line under while read line. it is reading the second line. it is taking the first line from file serverlist properly. it is not taking the second line. Commented May 21, 2014 at 14:58

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.