If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > value present in a list

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-24-04, 08:30
amolbc amolbc is offline
Registered User
 
Join Date: Mar 2004
Posts: 7
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
Reply With Quote
  #2 (permalink)  
Old 03-24-04, 09:25
aigles aigles is offline
Registered User
 
Join Date: Jan 2004
Location: Bordeaux, France
Posts: 319
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.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On