Re: Bash bug?

From: Zoltan Boszormenyi (zboszor_at_freemail.hu)
Date: 09/26/05

  • Next message: harold class: "Re: fedora-list Digest, Vol 19, Issue 193"
    Date: Mon, 26 Sep 2005 07:36:46 +0200
    To: For users of Fedora Core releases <fedora-list@redhat.com>
    
    

    Zoltan Boszormenyi írta:
    > 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.
    >
    > Best regards,
    > Zoltán Böszörményi

    I just tried to narrow the test for the bug:

    ---b.sh-------------------------
    #!/bin/bash

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

    Result:

    --------------------------------
    $ ./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.

    Can someone help me? Thanks in advance and 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: harold class: "Re: fedora-list Digest, Vol 19, Issue 193"

    Relevant Pages

    • Re: Bash bug?
      ... > a directory in a line, depending on the tokens, ... > the configuration file whether all the required/optional ... The first exists outside the while loop, ... Even if they were environment variables and had a permanent life outside ...
      (Fedora)
    • 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)
    • Re: Bash bug?
      ... =>a directory in a line, depending on the tokens, ... =>the configuration file whether all the required/optional ... Time flies like the wind. ... Stranger things have .0. ...
      (Fedora)
    • Re: Intermittant DNS problems
      ... which NW disables (might be worth raising a bug on that). ... configuration file may have disappeared from under named's feet. ... To unsubscribe: http://www.redhat.com/mailman/listinfo/fedora-list ...
      (Fedora)
    • RE: PPPoed crashed!!
      ... Pppoe is one of the major work horses of FBSD. ... There being an bug is very unlikely in 4.9. ... Odds are you have mis-configured an configuration file. ...
      (freebsd-questions)