Re: Bash bug?

From: Jeff Vian (jvian10_at_charter.net)
Date: 09/26/05

  • Next message: Eric Tanguy: "school room network"
    To: For users of Fedora Core releases <fedora-list@redhat.com>
    Date: Mon, 26 Sep 2005 02:11:06 -0500
    
    

    On Mon, 2005-09-26 at 07:12 +0200, Zoltan Boszormenyi wrote:
    > Hi,
    >
    > 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.txt----------------------
    > A directory1
    > B directory2
    > C directory3
    > --------------------------------
    >
    > -----a.sh-----------------------
    > #!/bin/bash
    >
    > HAS_A=0
    > HAS_B=0
    > HAS_C=0
    > cat a.txt | while read i ; do
    > if [ "`echo $i | awk '{ print $1 }'`" = "A" ]; then
    > HAS_A=1
    > fi
    > if [ "`echo $i | awk '{ print $1 }'`" = "B" ]; then
    > HAS_B=1
    > fi
    > if [ "`echo $i | awk '{ print $1 }'`" = "C" ]; then
    > HAS_C=1
    > fi
    > echo "A: $HAS_A B: $HAS_B C: $HAS_C"
    > done
    > 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.
    >
    Careful, those are not environment variables as you imply by the
    statement above when you call them envars.

    This is caused by the scope of variables.
    You are actually creating 2 different copies of each HAS_A, HAS_B, and
    HAS_C.
    The first exists outside the while loop, the second exists only inside
    the while loop. And all only exist while the script is executing. Even
    though the name is the same, the scope is different.

    Even if they were environment variables and had a permanent life outside
    the script, the value assigned still only lasts as long as the calling
    process lives (in this case the while loop). I tested this to verify.
     
    > Best regards,
    > Zoltán Böszörményi
    >

    -- 
    fedora-list mailing list
    fedora-list@redhat.com
    To unsubscribe: https://www.redhat.com/mailman/listinfo/fedora-list
    

  • Next message: Eric Tanguy: "school room network"

    Relevant Pages

    • Re: Bash bug?
      ... > a directory in a line, depending on the tokens, ... > the configuration file whether all the required/optional ... It's an ancient bug I must add, ... sub-shell is opened for this loop. ...
      (Fedora)
    • Re: looking for implementation of strtok
      ... contains a ',' character. ... we enter the ugly loop that simply implements strspn: ... loop sets spanp to delim, then loops while seting sc (the "span ... If there are no more tokens, we set *lastp to NULL (this is, I ...
      (comp.lang.c)
    • Re: Could have been nasty.
      ... it would have been Aylesbury north loop. ... One of those tokens may be a 'short staff' token, ...
      (uk.railway)
    • Re: Stupid Newbie Needs Help
      ... Without the loop the program works fine with the ... with 0-terminated strings, that way you can take advantage of C's ... hold up to 10 tokens, where each token may be up to 80 characters ... should give a clue of how the variable/constant/function/macro is ...
      (comp.lang.c)
    • Bash bug?
      ... a directory in a line, depending on the tokens, ... the configuration file whether all the required/optional ... It's an ancient bug I must add, ...
      (Fedora)