Re: Bash bug?

From: Steven W. Orr (steveo_at_syslang.net)
Date: 09/30/05

  • Next message: Robin Mordasiewicz: "Re: iptables"
    Date: Fri, 30 Sep 2005 10:56:23 -0400 (EDT)
    To: Zoltan Boszormenyi <zboszor@freemail.hu>
    
    

    On Monday, Sep 26th 2005 at 07:12 +0200, quoth Zoltan Boszormenyi:

    =>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"
    =>--------------------------------

    #!/bin/bash

    HAS_A=0
    HAS_B=0
    HAS_C=0
    while read lbl dir
    do
        [[ $lbl = A ]] && HAS_A=1
        [[ $lbl = B ]] && HAS_B=1
        [[ $lbl = C ]] && HAS_C=1
        echo "A: $HAS_A B: $HAS_B C: $HAS_C"
    done < a.txt
    echo "Final A: $HAS_A B: $HAS_B C: $HAS_C"

    cat | while causes the while to be in a subshell.

    =>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
    =>--------------------------------

    -- 
    Time flies like the wind. Fruit flies like a banana. Stranger things have  .0.
    happened but none stranger than this. Does your driver's license say Organ ..0
    Donor?Black holes are where God divided by zero. Listen to me! We are all- 000
    individuals! What if this weren't a hypothetical question?
    steveo at syslang.net
    -- 
    fedora-list mailing list
    fedora-list@redhat.com
    To unsubscribe: https://www.redhat.com/mailman/listinfo/fedora-list
    

  • Next message: Robin Mordasiewicz: "Re: iptables"

    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)
    • 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)
    • 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: create/access dynamic growth list
      ... >>Currently I the following method can only read and store data that ... >>Since there are unknown number of lines in a configuration file, ... Since each line of the configuration file need to be parsed into "key" ... Can I use the following operation to store these pair of tokens? ...
      (comp.lang.python)