Peter Blajev
2012-Feb-01 21:07 UTC
[CentOS] Bash scripting - Remotely ran commands break while loop
I have two CentOS5 systems server1 and server2. There is user peter on server1 who can ssh to server2 using public ssh keys and no password is needed. What I noticed is that running remote ssh commands in bash script breaks while loops. =====#!/bin/sh for i in server2 server2; do echo "--> Start" ssh peter@$i ls echo "--> END" done echo " server2 server2" | \ while read confLine; do echo "--> $confLine" ssh peter@$confLine ls echo "--> END $confLine" done === The "for" loop in the script above will run twice but the "while" loop below it will run only once. This is very simple to test and I've tried it on different systems including CentOS6 and OpenSolaris with the same result. Any idea what would cause the ssh command to break the while loop? Thanks Peter
Alexander Dalloz
2012-Feb-01 21:46 UTC
[CentOS] Bash scripting - Remotely ran commands break while loop
Am 01.02.2012 22:07, schrieb Peter Blajev:> I have two CentOS5 systems server1 and server2. There is user peter on > server1 who can ssh to server2 using public ssh keys and no password is > needed. > > What I noticed is that running remote ssh commands in bash script breaks > while loops. > > =====> #!/bin/sh > for i in server2 server2; do > echo "--> Start" > ssh peter@$i ls > echo "--> END" > done > > echo " server2 > server2" | \ > while read confLine; do > echo "--> $confLine" > ssh peter@$confLine ls > echo "--> END $confLine" > done > ===> > The "for" loop in the script above will run twice but the "while" loop > below it will run only once. > > This is very simple to test and I've tried it on different systems > including CentOS6 and OpenSolaris with the same result. > > Any idea what would cause the ssh command to break the while loop? > > Thanks > PeterThat has simply nothing to do with SSH. Compare following: echo "foo bar" | while read LINE; do echo $LINE; done and echo -e "foo\nbar" | while read $LINE; do echo $LINE; done Alexander
Stephen Harris
2012-Feb-01 22:53 UTC
[CentOS] Bash scripting - Remotely ran commands break while loop
On Wed, Feb 01, 2012 at 01:07:31PM -0800, Peter Blajev wrote:> echo " server2 > server2" | \ > while read confLine; do > echo "--> $confLine" > ssh peter@$confLine ls > echo "--> END $confLine" > done> The "for" loop in the script above will run twice but the "while" loop > below it will run only once.> Any idea what would cause the ssh command to break the while loop?"ssh" is reading from stdin and passing the data over to the remote machine. You can test this with ssh peter@$confLine 'read x ; echo we got $x' To stop it doing this, use the "-n" flag ssh -n peter@$confLine ls -- rgds Stephen