Global variable not updated in while loop when reading from pipe
From:
jwiegand
Subject:
Global variable not updated in while loop when reading from pipe
Date:
Thu, 13 May 2004 16:38:40 -0000
Configuration Information [Automatically generated, do not change]:
Machine: i686
OS: linux-gnu
Compiler: gcc
Compilation CFLAGS: -DPROGRAM='bash' -DCONF_HOSTTYPE='i686'
-DCONF_OSTYPE='linux-gnu' -DCONF_MACHTYPE='i686-pc-linux-gnu'
-DCONF_VENDOR='pc' -DSHELL -DHAVE_CONFIG_H -I. -I. -I./include -I./lib
-D_GNU_SOURCE -O2 -g -march=i386 -mcpu=i686
uname output: Linux bbflov02.fiberlink.com 2.4.18-14 #1 Wed Sep 4 13:35:50 EDT
2002 i686 i686 i386 GNU/Linux
Machine Type: i686-pc-linux-gnu
Bash Version: 2.05b
Patch Level: 0
Release Status: release
Description:
The variables free, use, and siz are supposed to accumulate the
variables read in from the pipeline. However, once the while loop is done,
their value outside of the loop is still 0. The script works as expected in
the Korn shell.
Repeat-By:
Run the following script:
#!/bin/bash -x
free=0
use=0
siz=0
echo -e "1 2 3\n1 2 3" |
while read s u f
do
echo "Line in: $s $u $f"
free=$(( $f + $free ))
use=$(( $u + $use ))
siz=$(( $s + $siz ))
echo "Total Size Used Space Available"
echo "---------- ---------- ---------"
echo "$siz $use $free"
done
echo "Total Size Used Space Available"
echo "---------- ---------- ---------"
echo "$siz $use $free"
free=$(( 0 + $free ))
use=$(( 0 + $use ))
siz=$(( 0 + $siz ))