Unable to read assign values to two variables in while loop

I am trying to read a input file which has two columns separated by space
Input file

server1 server2
server3 server4
server5 server6

When i execute the below while code it reads line by line and a and b variables are able to successfully fetch the values

while read a b
do
echo "$a"
echo "$b"
done < inputfile

But when i use a ssh statement . The while code just reads the first line and after that the script exits. Not able to understand this behaviour.

while read a b
do
ssh -q $a uname -a
done < inputfile

-n option to ssh will avoid this.

while read a b 
do 
ssh -qn $a uname -a 
done < inputfile
1 Like

To explain in more detail, the ssh application is just trying to read keyboard input from stdin as usual... But in this case, 'standard input' is 'inputfile', and it eats all your lines.

Hi,
Can anyone explain me below commands plz...

for OUT_TAB in $(eval echo '$JOB_OUTPUT_TABLE_NM'); do
DELIMITER=`echo "$DELIMITER" | sed 's/E//g'`

Thanks...

Thanks , It worked. But i am wondering , the for loop code for the same seems to be working fine.

for i in `cat inputfile`
do
ssh -q $i uname -a
done

But when i use the similar code in a while loop with one extra variable it seems to fail!

The while-read loop ends in <filename, which redirects filename into standard input.

The for-loop, which does not have <filename at the end, does not.

The while-loop is much better, but you must bear in mind that standard input is redirected for everything inside it.