Use of case structure and use of break in shell scripting
Shell supports case...esac statement which handles exactly this situation, and it does so more efficiently than repeated if...elif statements.
- The case statement allows you to easily check pattern (conditions) and then process a command-line if that condition evaluates to true.
- In other words the $variable-name is compared against the patterns until a match is found.
- *) acts as default and it is executed if no match is found.
- The pattern can include wildcards.
- You must include ;; at the end of each command-n. The shell executes all the statements up to the two semicolons that are next to each other.
- The esac is always required to indicate end of case statement.
case word in
Statement(s) to be executed if pattern1 matches
Statement(s) to be executed if pattern2 matches
Statement(s) to be executed if pattern3 matches
Default condition to be executed
|echo "Enter a number"|
case $num in
echo “you have entered a single digit number”
echo “you have entered a two-digit number”
echo “you have entered a three-digit number”
echo “your entry does not match any of the conditions”
break command is used to terminate the execution of for loop, while loop and until loop. It can also take one parameter i.e.[N]. Here n is the number of nested loops to break. The default number is 1.
|for i in `seq 1 10`|
For more Rajasthan Technical University CSE-IV Sem Linux Programming Lab Experiments Click Here