Results 1 to 2 of 2
  1. #1
    Join Date
    Mar 2004
    Posts
    7

    Unanswered: value present in a list

    hi,

    i just want to test if a value say $VAR is present in a list
    so i have a list like

    LIST='A,B,C,D,E'

    and now i get VAR filled in from somewhere and want to check if it is in the LIST...the items are delimited by ',' here

    i may need to do a switch case sort of depending on whether VAR is in LIST or not.... so my imaginary construct wud go like

    if [ $VAR in $LIST ] then
    echo '$VAR present in the list'
    fi


    now someone please help me get the correct shell conctructs for it.

    bye

  2. #2
    Join Date
    Jan 2004
    Location
    Bordeaux, France
    Posts
    320
    You can do like this :

    Code:
    if echo ",$LIST," | grep -q ",$VAR," 
    then
       echo '$VAR present in the list'
    fi
    If you want, you can define a function for this purpose :

    Code:
    IsInList() {
       echo ",$2," | grep -q ",$1,"
    }
    
    if IsInList "$VAR" "$LIST"
    then
       echo '$VAR present in the list'
    fi
    Jean-Pierre.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •