LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (http://www.linuxquestions.org/questions/linux-general-1/)
-   -   Unexpected result in while do done loop reading file (http://www.linuxquestions.org/questions/linux-general-1/unexpected-result-in-while-do-done-loop-reading-file-639146/)

steven.c.banks 05-01-2008 10: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;
do
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 12: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:
Code:

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


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

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


All times are GMT -5. The time now is 03:35 PM.