Welcome back! 1
Let's go ahead and talk about the case statement.The case construct allows us to test strings and 2
numbers and is in fact a simpler form of the bash if-elif-else statements. 3
If you are coming from other programming languages, you should know that the case statement in Bash 4
is similar to the switch statement in C, C++ or Java. 5
This programming construct is not a loop, 6
it doesn't execute a block of code for n number of times. Instead, bash shell checks the conditions, 7
and controls the flow of the program. 8
The Bash case statement takes the following form; each case statement starts with the case keyworth, 9
followed by the expression and the in keyword. The entire statement ends with the esac keyword. 10
You can have multiple patterns separated by a vertical bar like a pipe. 11
The parentheses operator terminates the pattern list. 12
A pattern can contain special characters like asterisks or question marks. A pattern and the associated 13
commands are known as a clause and each clause must be terminated with double semicolons. 14
The commands corresponding to the first pattern that matches the expression are executed. It is common 15
practice to use the whilecard asterisk symbol as a final pattern to define the default case. 16
This pattern will always match! After the first match the case statement terminates with the status of 17
the last command that was executed. 18
Let's see how this works with an example. I'm creating a new script named favorite_pet.sh. 19
The user will enter his favorite pet, and based on his answer, the case statement will print out a 20
specific message echo -n "Enter your favorite pet:" 21
This time I'm displaying the message using echo and the -n option, not to add a new line after the 22
string; and read 23
pet I'm reading the answer in a variable called pet. 24
And now the case statement case "$PET" in and the first pattern will be dog echo 25
"Your favorite pet is the dog." 26
And don't forget, double semicolons at the end of the statements; and the second pattern, this time, 27
I'll use the OR operator, which is the vertical bar cat written in lowercase letters, | Cat 28
written with a capital C. 29
echo "You like cats." and ;; And the last pattern, if there is a string that contains 30
spaces, it should be enclosed in double quotes, for example, fish | 31
And "African Turtle" 32
So "Fish or tortoise are great." 33
And finally, asterisk and the closing parentheses; this is the default case that matches anything. 34
So if the user enters another value, the statements of this pattern will be executed. 35
"Your favorite pet is unknown." 36
And the esac keyword which terminates the case. I'm saving the script, adding the execution permission 37
and running it. 38
And I'm entering dog. 39
And the message is the expected one; now cat; perfect; Cat written with capital C. 40
Fish and African turtle. 41
And if the user enters anything else, the statements of the default pattern will be executed. 42
Let's see a second example this time a real-life one. I want to create a script that accepts 2 arguments. 43
The first argument is the signal number and the second one the process id. And the script will send the 44
signal number to the process id. 45
I'm first checking that the script is run with exactly two arguments. 46
Remember that the number of arguments is saved in the predefined variable $# 47
So if $# is not equal to 2, then I'll exit. 48
then echo "Run the script with 2 arguments: SIGNAL and PID." 49
and exit 50
and the case statement; I'm sending the right signal based on the value of the first argument of the 51
script, so case $1 If it is 1, I'm sending the SIGHUP signal to the process, to the PID. 52
So echo "Sending the SIGHUP signal 53
to 54
to $2" $2 being the process id. 55
kill -SIGHUP $2 56
If it's true, I'm sending the SIGINT signal to the process id. 57
By the way, you can see a list with all signals by running kill -l 58
kill -SIGINT $2 the precess id and of course, here,$2 as well, and another 59
one 15, the default signal echo sending the SIGINT signal to #2 60
And the default pattern "Signal number $1 will not be delivered." 61
That's my script! 62
I'm saving it, setting the required permission, and running it in another terminal. 63
I want you to see the script at the same time. 64
So I'm running the sleep command. 65
I'm finding its proccess id. 66
And I'm running the script. I'm sending the SIGHUP signal, so 1 and the process id 506 67
4 68
And the signal was delivered to the process. 69
If I ran the script without any argument, it will stay around the script with two arguments signal 70
and process id. This if testing condition was evaluated to true and this piece of code was run. 71
The same will happen if I ran the script with more than two arguments. 72
OK, that's all about case, thank you!