case use instead of if else


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting case use instead of if else
# 1  
Old 02-21-2012
case use instead of if else

Hi,
I'm writing a script where the requirement is of the sort
Code:
if echo $x | grep $a > /dev/null
then
COLOR1
elif echo $x | grep $b > /dev/null
then
COLOR2
..
..
fi

I want to replace this with case esac.
color to be changed based on $a $b .... a big list
also $x changes for each line of the file.
Smilie

Last edited by Franklin52; 02-21-2012 at 03:17 AM.. Reason: Please use code tags for code and data samples, thank you
# 2  
Old 02-21-2012
Code:
case $x in
*$a*)
  echo a
  ;;
*$b*)
  echo b
  ;;
*$c*)
  echo c
  ;;
*)
  echo default
  ;;
esac

This User Gave Thanks to chihung For This Post:
# 3  
Old 02-21-2012
Thanks Chihung,

It does work except for the fact which I didn't mention that

$a $b are multiple words | separated.

as grep can be used for searching multiple words
grep "aa|bb|cc" file

in case *$a*, this breaks.
# 4  
Old 02-21-2012
Try and see if this makes a difference:
Code:
case $x in
*"$a"*)
  echo COLOR1
  ;;
*"$b"*)
  echo COLOR2
  ;;
*"$c"*)
  echo COLOR3
  ;;
*)
  echo default
  ;;
esac

Could you post an example of what goes wrong, otherwise?

Last edited by Scrutinizer; 02-21-2012 at 04:24 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Change first letter of a word from lower case to upper case

Hi all, I am trying to find a way to change first letter in a word from lower case to upper case. It should be done for each first word in text or in paragraph, and also for each word after punctuation like . ; : ! ?I found the following command sed -i 's/\s*./\U&\E/g' $@ filenamebut... (7 Replies)
Discussion started by: georgi58
7 Replies

2. Shell Programming and Scripting

Conversion from Upper Case to Lower Case Condition based

Hello Unix Gurus : It would be really appreciative if can find a solution for this . I have records in a file . I need to Capitalize the records based on condition . For Example i tried the following Command COMMAND --> fgrep "2000YUYU" /export/home/oracle/TST/data.dat | tr '' ''... (12 Replies)
Discussion started by: tsbiju
12 Replies

3. Shell Programming and Scripting

[Solved] Change Upper case to Lower case in C shell

Is there a command that can switch a character variable from UPPER case to lower case? like foreach AC ( ABC BCD PLL QIO) set ac `COMMAND($AC)` ... end Thanks a lot! (3 Replies)
Discussion started by: rockytodd
3 Replies

4. Shell Programming and Scripting

sed ignoring case for search but respecting case for subtitute

Hi I want to make string substitution ignoring case for search but respecting case for subtitute. Ex changing all occurences of "original" in a file to "substitute": original becomes substitute Origninal becomes Substitute ORIGINAL becomes SUBSTITUTE I know this a little special but it's not... (1 Reply)
Discussion started by: kmchen
1 Replies

5. Shell Programming and Scripting

data array needs to change upper case to lower case

Hi all, i have a data array as followes. ARRAY=DFSG345GGG ARRAY=234FDFG090 ARRAY=VDFVGBGHH so on.......... i need all english letters to be change to lower case. So i am expecting to see ARRAY=dfsg345ggg ARRAY=234fdfg090 ARRAY=vdfvgbghh so on........ If i have to copy this data in... (8 Replies)
Discussion started by: usustarr
8 Replies

6. Shell Programming and Scripting

Script to Convert Upper case to Lower case

Hi All I have a script which extracts values from a Database (A persons name) and puts it into a variable in my script IE: $NAME However the Value in the DB is all in uppercase and contains the users first name and last name EG: > echo $NAME GRAHAM BOYLE > What I need is only the... (7 Replies)
Discussion started by: grahambo2005
7 Replies

7. Shell Programming and Scripting

how to convert value in a variable from upper case to lower case

Hi, I have a variable $Ctrcd which contains country names in upper case and i want to convert them into lower case. I have tried so many solutions from already existing threads but couldn't get the correct one. Can anybody help me with this..... Thanks a lot.. (2 Replies)
Discussion started by: manmeet
2 Replies

8. Shell Programming and Scripting

Script needed to select and delete lower case and mixed case records

HELLO ALL, URGENTLY NEEDED A SCRIPT TO SELECT AND DELETE LOWER AND MIXED CASE RECORDS FROM A COLUMN IN A TABLE. FOR EXAMPLE : Table name is EMPLOYEE and the column name is CITY and the CITY column records will be: Newyork washington ... (1 Reply)
Discussion started by: abhilash mn
1 Replies

9. UNIX for Dummies Questions & Answers

lower case to upper case string conversion in shell script

How can convert a Lower case variable value to an upper case in the kron shell script. (3 Replies)
Discussion started by: dchalavadi
3 Replies
Login or Register to Ask a Question