[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

Re: Bash bug?

Zoltan Boszormenyi wrote:
Zoltan Boszormenyi írta:


I am trying something like the following,
with a configuration file containing a token and
a directory in a line, depending on the tokens,
certain actions should be taken later. Validating
the configuration file whether all the required/optional
tokens are in the file should go like this:

A directory1
B directory2
C directory3


cat a.txt | while read i ; do
        if [ "`echo $i | awk '{ print $1 }'`" = "A" ]; then
        if [ "`echo $i | awk '{ print $1 }'`" = "B" ]; then
        if [ "`echo $i | awk '{ print $1 }'`" = "C" ]; then
        echo "A: $HAS_A B: $HAS_B C: $HAS_C"
echo "Final A: $HAS_A B: $HAS_B C: $HAS_C"

Result is:

$ ./a.sh
A: 1 B: 0 C: 0
A: 1 B: 1 C: 0
A: 1 B: 1 C: 1
Final A: 0 B: 0 C: 0

It seems to be a bug to me, the envvars lose their values
they gained in the loop. It's an ancient bug I must add,
I just rechecked it and bash in RedHat 7.1 behaves the same.
How can I preserve the variables' values? Putting "export"
in front of every assignments doesn't help.

Best regards,
Zoltán Böszörményi

I just tried to narrow the test for the bug:


while [ $A -gt 0 ]; do
        A=$(($A - 1))
        echo "A: $A X: $X"
echo "Final A: $A X: $X"


$ ./b.sh
A: 1 X: 1
A: 0 X: 1
Final A: 0 X: 1

The variables here keep their values. Hm. Then the bug may come
from the "cat a.txt | while read i; do" construct, maybe another
sub-shell is opened for this loop. But then putting "export"
in front of the assignments inside the loop should solve this but
it doesn't. It still seems to be a bug to me somewhere.

Yes, the "cat a.txt | while read i; do ..." construct does cause
the "while" loop to be executed in a subshell.  Everything is working
as it should.  The only bug is in your understanding of how processes
and environments work. Each process has its very own environment, and exporting a variable causes a child process's initial enviromnent to
receive a copy of that variable.  Nothing the child subsequently does
to that copy has any effect on the parent process.

Hopefully you've got a copy of the bash FAQ installed on your system
(/usr/share/doc/bash-3.0/FAQ on mine).  Take a look at answer E4.

Bob Nichols         Yes, "NOSPAM" is really part of my email address.

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]