]cat text_file | while read line
] total=`expr $total + $line`
]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
while read hostname
rsh $hostname "command" < /dev/null
BBN Corporation, Cambridge, MA
(BBN customers, call (800) 632-7638 option 1 for support)