Name
switch
Synopsis
switch
Process commands depending on the value of a variable. When you need to handle more than three choices, switch is a useful alternative to an if-then-else statement. If the string variable matches pattern1, the first set of commands is executed; if string matches pattern2, the second set of commands is executed; and so on. If no patterns match, execute commands under the default case. string can be specified using command substitution, variable substitution, or filename expansion. Patterns can be specified using the pattern matching symbols *, ?, and [ ]. breaksw is used to exit the switch. If breaksw is omitted (which is rarely done), the switch continues to execute another set of commands until it reaches a breaksw or endsw. Following is the general syntax of switch, side by side with an example that processes the first command-line argument:
switch (string)switch ($argv[1])casepattern1:case -[nN]:commandsnroff $file | lpbreakswbreakswcasepattern2:case -[Pp]:commandspr $file | lpbreakswbreakswcasepattern3:case -[Mm]:commandsmore $filebreakswbreaksw.case -[Ss]:.sort $file.breakswdefault:default:commandsecho "Error--no such option"exit 1breakswbreakswendswendsw
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access