Re: Scoop of variables in While loops???
From: Alan Connor (zzzzzz_at_xxx.yyy)
Date: 04/29/05
- Next message: Ed Morton: "Re: ksh - trying to cat a file and grep 2 parameters"
- Previous message: Jhair Tocancipa Triana: "Re: Scoop of variables in While loops???"
- In reply to: Sebastian: "Scoop of variables in While loops???"
- Next in thread: Ed Morton: "Re: Scoop of variables in While loops???"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Fri, 29 Apr 2005 10:45:05 GMT
On comp.unix.shell, in
<6acda821.0504290131.285f165e@posting.google.com>,
"Sebastian" wrote:
> Hi,
>
> Why does my variables not keep the values when I exit
> the while loop below? How should I do to get them to
> keep their value after the loop is finished?
>
> COUNTER=0
> while read data_line do
>
> COUNTER=`expr $COUNTER + 1`
>
> echo $COUNTER
>
> done < files.txt
>
> echo""
> echo $COUNTER
>
> Gives the following output: 1 2 3 4 0
>
> Why?
Works fine in bash 2. What shell are you using?
I'd guess that the loop is running in a sub-shell, and
the parent shell doesn't normally inherit variable values
from its children.
You might try adding the line:
export $COUNTER
after echo $COUNTER.
If that fails, do
echo $COUNTER > /tmp/counterfileXXXXXXXXX
and read that file at the end of the script, then rm
it.
AC
-- alanconnor AT earthlink DOT net Use your real return address or I'll never know you even tried to mail me. http://tinyurl.com/2t5kp ~
- Next message: Ed Morton: "Re: ksh - trying to cat a file and grep 2 parameters"
- Previous message: Jhair Tocancipa Triana: "Re: Scoop of variables in While loops???"
- In reply to: Sebastian: "Scoop of variables in While loops???"
- Next in thread: Ed Morton: "Re: Scoop of variables in While loops???"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|