Problem with 'while' loop in 'sh'

Problem with 'while' loop in 'sh'

Post by Barry Margoli » Mon, 12 May 1997 04:00:00




]#!/bin/sh
]total=0
]cat text_file | while read line
]do
]   total=`expr $total + $line`
]done
]echo $total
]
]Assuming that the text_file holds a list of digits, the echo at the end of the loop should display
]their sum.  But instead, its value is zero.  In fact, any variables set within the while loop seem
]to loose their setting after the 'done.

The commands in a while loop are executed in a child process.  So any
variable changes they make are not visible when the loop exits.

I'd be surprised if this isn't in the FAQ.

]In a similar example: if I place an 'rsh' command within the loop, and the text_file holds a list of
]machines to rsh to, the loop will exit after its first iteration regardless of how many machine names
]are in the input file.

rsh reads from its standard input, sending the data to the remote system in
case the command wants input.  So it's reading the rest of text_file, and
when the next iteration of the loop starts there's no data left for "read"
to read.  You can solve this problem by redirecting rsh's input to
/dev/null, e.g.

while read hostname
do
  rsh $hostname "command" < /dev/null
done

--
Barry Margolin
BBN Corporation, Cambridge, MA

(BBN customers, call (800) 632-7638 option 1 for support)

 
 
 

Problem with 'while' loop in 'sh'

Post by Jim McKinty » Tue, 13 May 1997 04:00:00


Good Day All

I often have cause to use a bourne shell script to process a text file on our Solaris 2.5 system.
Several times I have been frustrated by what I see as a bug in bourne shell.  Please correct
me if I'm wrong.  The following is a trivial example of the problem.

#!/bin/sh
total=0
cat text_file | while read line
do
   total=`expr $total + $line`
done
echo $total

Assuming that the text_file holds a list of digits, the echo at the end of the loop should display
their sum.  But instead, its value is zero.  In fact, any variables set within the while loop seem
to loose their setting after the 'done.

In a similar example: if I place an 'rsh' command within the loop, and the text_file holds a list of
machines to rsh to, the loop will exit after its first iteration regardless of how many machine names
are in the input file.

Any help appreciated

Regards

Jim

 
 
 

Problem with 'while' loop in 'sh'

Post by Bob Farquhars » Tue, 13 May 1997 04:00:00


Quote:>total=0
>cat text_file | while read line
>do
>   total=`expr $total + $line`
>done
>echo $total

>Assuming that the text_file holds a list of digits, the echo at the end of the loop should display
>their sum.  But instead, its value is zero.  In fact, any variables set within the while loop seem
>to loose their setting after the 'done.

Instead of using a cat file, input the command at the end of the loop.

Thus: -

total=0
 while read line
do
   total=`expr $total + $line`
done <$file
echo $total

 
 
 

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

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.

2. telnet/tcp server failing !!!

3. 'sh' after 'chroot'

4. root password

5. An extensive comparison of 'sh' and 'csh' language features

6. X11 forwarding via ssh and su

7. 'sh' script spawning Sun 'cmdtool' running program

8. wwwoffle - not working when online

9. 'for/next' loops in sh/csh??

10. Simple 'sed', 'awk', 'cut' problem

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

12. Help: problems with 'w', 'who' and 'last'

13. 'double' or 'dbl' or 'dble', the disk space utilty