Why doesnt if inside a while loop work ?

I am looping through a file in bash and performing some operations on it. Here is the code.

cat test.dat -

One
Two
Three

Case 1: With if inside while loop

Output:
One
file found :slight_smile:

------------------

isn't it supposed to print following output !!!
one
file found :slight_smile:
two
file found :slight_smile:
three
file found :slight_smile:
four
file found :slight_smile:

Case 2: I comment if condition inside loop

Output:

one
two
three
four

Can come one explain why is this happening ? Why is if breaking the loop.

-Steve

Add the -n option to the ssh command:

#!/bin/bash


 cat ~/tmp/test.dat | while read line
 do
 echo $line

 if ssh -n tempuser@host5592 "test -e '/home/tempuser/tmp/test.dat'"; then
 echo "file found :) "
 fi

 done
1 Like

Thanks a ton. That worked. Can you tell me what caused the issue.

Piping into a while loop in such a way reads from STDIN. When you ssh to a server it takes over STDIN. The -n switch to the ssh command prevents it from taking over STDIN.