case Conditionals

You might find yourself generating a series of if/elif/elif/elif/else syntax and wondering whether a better way of handling a multiway test exists. The case statement provides a succinct way to express multiway tests. This construct is similar to a switch statement in some programming languages and is vaguely similar to computed go tos. The syntax checks a certain value against several cases. Each case is terminated by ;;, and the value to be tested appears after the case syntax. The syntax is as follows (keywords are bold):

					case test_variable in
test_pattern1)         command(s)            ;;
test_pattern2)         command(s)            ;;
pattern3|or_pattern4) command(s)            ;;
*)    commands_to_execute_for_default_case     ;;
esac

The patterns can include file matching ...

Get Korn Shell Programming by Example now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.