ssh breaks while loop - SSH

This is a discussion on ssh breaks while loop - SSH ; If file hostlist contains: hosta hostb hostc then execute this shell script: while read host do ssh $host ls -al .profile done -rw------- 1 someuser users 2058 Oct 7 2005 .profile the while loop executes one time and quits. But, ...

+ Reply to Thread
Results 1 to 4 of 4

Thread: ssh breaks while loop

  1. ssh breaks while loop

    If file hostlist contains:
    hosta
    hostb
    hostc

    then execute this shell script:

    while read host
    do
    ssh $host ls -al .profile
    done < hostlist

    -rw------- 1 someuser users 2058 Oct 7 2005 .profile

    the while loop executes one time and quits.

    But,

    while read host
    do
    echo $host
    done < hostlist

    hosta
    hostb
    hostc

    executes 3 times

    Am I missing something or is this a bug?




  2. Re: ssh breaks while loop

    On Fri, 04 Aug 2006 21:53:00 -0500, Charlie Brown wrote:

    > then execute this shell script:
    >
    > while read host
    > do
    > ssh $host ls -al .profile
    > done < hostlist
    >
    > -rw------- 1 someuser users 2058 Oct 7 2005 .profile
    >
    > the while loop executes one time and quits.


    the first ssh passes all your stdin to the ssh session so the loop is
    finished once the ssh completes.
    Try ssh -n $host ls -al .profile

    The -n will prevent it from reading stdin.

    JohnK

  3. Re: ssh breaks while loop

    "Charlie Brown" writes:

    >If file hostlist contains:
    >hosta
    >hostb
    >hostc


    >then execute this shell script:


    >while read host
    >do
    > ssh $host ls -al .profile
    >done < hostlist


    >-rw------- 1 someuser users 2058 Oct 7 2005 .profile


    >the while loop executes one time and quits.


    ssh probably empties the stdin, since the hostlist is also attached to the
    stdin of ssh.
    I would do
    for host in `cat hostlist`
    do
    ssh $host ls -al .profile
    done


    >But,


    >while read host
    >do
    > echo $host
    >done < hostlist


    >hosta
    >hostb
    >hostc


    >executes 3 times


    >Am I missing something or is this a bug?





  4. Re: ssh breaks while loop

    Thanks JohnK - the -n does the trick and resolves the issue.

    Thanks Unruh - your suggestion also works, but in the bigger scope of my
    script a while loop ultimately will be more effective.

    CB

    "JohnK" wrote in message
    newsan.2006.08.05.07.24.35.363231@gmail.com...
    > On Fri, 04 Aug 2006 21:53:00 -0500, Charlie Brown wrote:
    >
    >> then execute this shell script:
    >>
    >> while read host
    >> do
    >> ssh $host ls -al .profile
    >> done < hostlist
    >>
    >> -rw------- 1 someuser users 2058 Oct 7 2005 .profile
    >>
    >> the while loop executes one time and quits.

    >
    > the first ssh passes all your stdin to the ssh session so the loop is
    > finished once the ssh completes.
    > Try ssh -n $host ls -al .profile
    >
    > The -n will prevent it from reading stdin.
    >
    > JohnK




+ Reply to Thread