different behaviour of 'while read' and 'for' loops in bash

different behaviour of 'while read' and 'for' loops in bash

Post by Glenn Morri » Sat, 14 May 2005 22:17:26



I have a command I want to execute on several hosts via ssh. The hosts
are listed in a file "hosts.txt", one per line. I'm using bash.

First I tried this:

while read host; do
   ssh $host hostname
done < hosts.txt

but for some reason this only does the first host listed in hosts.txt,
not the rest. The uglier:

for host in `cat hosts.txt`; do
   ssh $host hostname
done

works fine and processes all hosts as it should.

All I can think is that the ssh in the first form somehow causes bash
to lose its place in the file "hosts.txt", but I have no idea how to
check that, or if that is to be expected...?

TIA for any clarification.

 
 
 

different behaviour of 'while read' and 'for' loops in bash

Post by Chris F.A. Johnso » Sat, 14 May 2005 22:45:19



> I have a command I want to execute on several hosts via ssh. The hosts
> are listed in a file "hosts.txt", one per line. I'm using bash.

> First I tried this:

> while read host; do
>    ssh $host hostname
> done < hosts.txt

> but for some reason this only does the first host listed in hosts.txt,
> not the rest. The uglier:

> for host in `cat hosts.txt`; do
>    ssh $host hostname
> done

> works fine and processes all hosts as it should.

> All I can think is that the ssh in the first form somehow causes bash
> to lose its place in the file "hosts.txt", but I have no idea how to
> check that, or if that is to be expected...?

    The while loop is reading from stdin; so is ssh. Use exec to
    redirect the input:

exec 3< hosts.txt
while read -u3 host
do
   ssh $host hostname
done
exec 3<&-

--
    Chris F.A. Johnson                     <http://cfaj.freeshell.org>
    ==================================================================
    Shell Scripting Recipes: A Problem-Solution Approach, 2005, Apress
    <http://www.torfree.net/~chris/books/ssr.html>

 
 
 

different behaviour of 'while read' and 'for' loops in bash

Post by Glenn Morri » Sat, 14 May 2005 23:22:09



>     The while loop is reading from stdin; so is ssh.

Oh, thank you! I see now that I can also use "ssh -n" to prevent ssh
reading from stdin (I'm surprised it does so). I also see now that
this issue has come up before. Sorry for not finding those threads, I
was searching on the wrong keywords.
 
 
 

1. Bash 'umask' builtin doesn't set 'x' permissions?

I was tinkering around with the Bash builtin command 'umask' (on a Red
Hat Linux 9 box) and noticed the following behavior:

    [bash]$ rm -f ./try; umask 0002; touch try; ls -l try
    -rw-rw-r--    1 me       me              0 Jul 11 14:49 try
    [bash]$ umask -S
    u=rwx,g=rwx,o=rx

Note that the 'x' bits are not turned on for the newly created file
'try'. IOW, I expected try's file permissions would be set to
"-rwxrwxr-x". Why weren't they? Is this a "Bash thing" or a "Red Hat
thing" or other?

--
Jim

To reply by email, remove "link" and change "now.here" to "yahoo"
jfischer_link5809{at}now.here.com

2. Network Sniffer Implementation in Kernel Mode

3. Problem with 'while' loop in 'sh'

4. modeline for XP17

5. (patch for Bash) Awk's NF for 'read'

6. Deleting a linux partition from a Windows system

7. 'exit' behavior in a while loop

8. Need help with install

9. Misterious behavior of 'dip' and 'find', help if u can...

10. Puzzling Behaviour From tr '\n' ' '

11. Is different 'Ethenet address' from 'Mac Address' ?

12. 'date' and 'clock' show different times

13. What's 'side effects' of Ksh built-ins?