[CentOS] Bash scripting - Remotely ran commands break while loop

Thu Feb 2 03:03:33 UTC 2012
Peter Blajev <pblajev at ucsd.edu>

On Wed, Feb 1, 2012 at 2:53 PM, Stephen Harris <lists at spuddy.org> wrote:

> 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


This is it. Right on Stephen. Thank you very much. I can't believe I've
gone so long without knowing it.

This works for me. I still don't have full understanding of it but I'll do
some more reading.

Unfortunately I can't always use the (-n) option. If I wan't to send data
through the pipe then the (-n) won't work. For example (on top of my head):
  mysqldump dB | ssh peter at remoteServer "mysql dB"

In my script I ended up using "ssh -n" when I want to work on the output of
remotely ran command and "ssh" without (-n) when I want to send data over
ssh to a remote command.

This so far is not breaking the while loop and it seems to be working but
it makes me nervous.

Any note will be appreciated.

Thanks again.

--
Peter