-   Linux - General (
-   -   Unexpected result in while do done loop reading file (

steven.c.banks 05-01-2008 11:40 AM

Unexpected result in while do done loop reading file
I have this while do done loop in a bash shell script:

while read line;
echo $line
ssh root@anotherserver "hostname"
echo "here1"
done </tmp/get_mbx_stats.tmp

It reads the first line of /tmp/get_mbx_stats.tmp OK, and I get the correct results from the ssh and the echo commands, but then it drops out of the loop, even though there are many more lines in /tmp/get_mbx_stats.tmp.

If I comment out the ssh command, it works as expected - it reads and echoes all the lines in the input file.

I have tried many variations but they always fail with the same issue.

(Actually I need to get information from "anotherserver" - not simply its hostname - but I boiled it down to the simplest ssh I could think of).

Is the ssh command changing the file status in some way?

Thanks in advance...

colucix 05-01-2008 01:12 PM

This is a known issue with ssh trying to read from standard input and sending a SIGTTIN signal to the parent shell upon exit. You can try the option -n of ssh, that circumvent this problem, redirecting standard input from /dev/null or explicitly redirect standard input in the ssh command line:

ssh -n root@anotherserver "hostname"
ssh root@anotherserver "hostname" < /dev/null

steven.c.banks 05-01-2008 01:32 PM

Thank you!!! I used the -n option, and that fixed the problem!!

All times are GMT -5. The time now is 11:41 AM.