How to write a case condition in shell?

In ksh shell – case statement
case variable_name in
pattern1)
statements1
;;
pattern2)
statements2
;;
# default (catch all remaining)
*)
statements3
;;
esac

Example:
case $answer in
yes|Yes|y) # received yes
echo got a positive answer
;;
no|n) # received no
echo got a ‘no’
;;
q*|Q*)
#assume the user wants to quit
exit
;;
*)
echo This is the default clause
;;
esac

In csh shell – case statement
Example:
switch ( $color )
case blue:
echo $color is blue
breaksw
case red:
case purple
echo $color is red or purple
breaksw
default:
echo “Not a valid color”
endsw

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.