home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


UNIX in a Nutshell: System V Edition

UNIX in a Nutshell: System V EditionSearch this book
Previous: Reference: break Chapter 4
The Bourne Shell and Korn Shell
Next: Reference: cd
 

case



case

 

value

 

in


    

pattern1



)

 

cmds1



;;


    

pattern2



)

 

cmds2



;;


    .
    .
    .


esac

Execute the first set of commands ( cmds1 ) if value matches pattern1 , execute the second set of commands ( cmds2 ) if value matches pattern2 , etc. Be sure the last command in each set ends with ;; . value is typically a positional parameter or other shell variable. cmds are typically UNIX commands, shell programming commands, or variable assignments. Patterns can use file generation metacharacters. Multiple patterns (separated by | ) can be specified on the same line; in this case, the associated cmds are executed whenever value matches any of these patterns. See below and under eval for examples. (Note: the Korn shell allows pattern to be preceded by an optional open parenthesis, as in ( pattern ) . It's useful for balancing parentheses inside a $( ) construct.)

Examples

Read first command-line argument and take appropriate action:


case $1 in	#match the first arg
    no|yes) response=1;;
    -[tT])  table=TRUE;;
    *)      echo "unknown option"; exit 1;;
esac

Read user-supplied lines until user exits:


while :	# Null command; always true
do
   echo "Type . to finish ==> \c"
   read line
   case "$line" in
      .) echo "Message done"
         break ;;
      *) echo "$line" >> $message ;;
   esac
done


Previous: Reference: break UNIX in a Nutshell: System V Edition Next: Reference: cd
Reference: break Book Index Reference: cd

The UNIX CD Bookshelf Navigation The UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System