Case
Allows you to choose between several patterns. First, check the value of the variable after the keyword case and compared with all the options in turn. Of course, must be the same as the standard to which you want to appeal. If the match is successful, the order is made or the command assigned to a given pattern. Otherwise, the default command used is marked with an asterisk: *) default_command. What is a good safeguard against mistakes made by our script. Case statement is ideally suited for the processing of arguments passed on the command line (of course combined with a while loop for example).
Attention
In many cases, case command can be replaced by the corresponding command if-elif-else ;) described in the section on the if command.
Syntax
case <variable> in
"Pattern1") command1;;
"Pattern2") command2;;
"Pattern3") command3;;
*) default_command
esac
Attention
It is important to place double semicolons after the block corresponding to the model, their absence will cause bad the script (most likely will not be reported to the warning!).
Example
#/bin/bash
echo "Enter the number of the month"
read d
case "$ d" in
"1") echo 'January, cold ... I dream of skiing ';;
"2") echo 'February, winter holiday hurray!' ;;
"3") echo 'March, only to spring ... " ;;
"4") echo 'April, spring will tell everyone ... " ;;
"5") echo 'May, "And then came May he lived in my heart ..." ';;
"6") echo 'June, no no summer just around the corner';;
"7") echo 'July, holidays! :) ';;
"8") echo 'August vacation! :) ';;
"9") echo 'September, hey ho,we are going to school :)';;
"10") echo 'October, ah, autumn ...' ;;
"11") echo 'November, gray and gloomy';;
"12") echo 'December, holidays, gifts, New Year';;
*) Echo "Nothing selected"
esac
In the script, put the numbers of months, each month is assigned to the command that displays a different message. If we use one to read by reading data from standard input variable d assigns a value of 1 will perform a jump to the pattern "1", and the screen will print the message:
January, the cold ... I dream about skiing
If you choose the number outside the range of our script will choose the default answer is marked with an asterisk, and then writes:
Nothing you have chosen
Attention
It is good to use quotes in place to prevent this practice from bad interpreting numbers and other characters.
You can also use special characters *? [], etc.