Case Statements in Shell Scripts

Contents

keyboard_tab
Case Scripting
1
Case Statements
PREVIEW5m 43s

The course is part of this learning path

Bash Scripting and Shell Programming
course-steps
11
certification
1
Case Statements
Overview
Difficulty
Intermediate
Duration
6m
Students
5
Ratings
5/5
starstarstarstarstar
Description

In this brief course, you will learn how to use case statements in shell scripts.

Transcript

In this lesson you will learn when to use the case statement and how to do it. You already know how to make decisions and vary the flow of your scripts by using if statements. If you find yourself using an if statement to compare the same variable against different values you could use a case statement in its place. Some people argue that for these situations a case statement is easier to read. In any case, you'll see case statements in shell scripts and you'll want to at least understand what they are doing and how they work. One common place to find case statements and use is in system startup scripts for example. To create a case statement, start with the word case, follow it with an expression, which is most commonly a variable. And end the line with a word in. Next you list a pattern or a value you want to test against the variable in the pattern with the parentheses. If the pattern is matched the commands following it will be executed. The execution will stop when a double semi-colon is reached and the script will continue after the case statement. You can test for multiple values using the same pattern. Finally, the case statement is ended with esac, which is case spelled backwards. Here's an example case statement. You'll find something similar to this in startup scripts or scripts that are used to control the behavior of a program. The case statement examines the value of $1. Remember that $1 is the first argument supplied to the shell script. If $1 is equal to start then user sbin sshd is executed. If $1 is equal to stop then the kill command is executed. If $1 doesn't match start or stop then nothing happens and the shell script continues after the case statement. Note that the patterns are case sensitive. If someone were to execute this shell script and use start and all capital letters as the first argument, nothing would happen. The case statement only matches start in all lowercase letters and stop in all lower case letters. Here's a slightly modified version of the case statement. In this example, a wildcard is used. An asterisk or a star as I like to call it matches anything. Wildcards are covered more in depth in another section of this course. What you learn about pattern matching there applies to case statements as well. In this example anything other than start and stop will cause the last pattern to be matched. In that case, the echo and exit commands will be executed. I used a semi colon here to separate the command so that they would fit on the screen but you could have just as well placed them on separate lines. This is yet another slightly modified version of the same case statement. This time you might notice that a pipe was used. You can use the pipe as an 0. So if $1 is equal to start in all lower case or start in all upper case, the sshd command is executed. We did the same thing with stop. And like the previous example the wildcard will act as a catchall and match anything else. Here's an example where you are asking for input from the user. This input is stored in the variable answer. Even though you may ask the user to enter a lowercase Y or a lowercase N, they may do something slightly different. Instead of being so strict you can adjust the pattern matching in your case statement. Like I said earlier, all the rules for wild cards are in play for the matching section of the case statement. Here, we are going to use character classes. As a quick reminder, character classes are simply a list of characters between brackets. The character class matches exactly one character and a match occurs for any of the characters included between the brackets. The first character class in this example is lowercase Y and uppercase Y. So if the user entered a lower case or an upper case Y the first pattern will match and then "You answered yes," will be echoed to this screen. Also, if the user entered Y-E-S using any combination of upper or lower case letters the pattern following the pipe will match and "You answered yes," will be echoed to the screen. That pattern is three characters in length and is comprised of three character classes. The first character class is a lowercase and uppercase Y. The second is a lowercase E and upper case E. And the third character class is a lowercase S and an uppercase S. We use the exact same concept to match for N or no as an answer. The last pattern is a star. So it will match anything that did not match the above patterns. You don't have to use the star wildcard by itself. In this example, the first pattern will match anything that starts with a lowercase or uppercase Y. This will include answers like Y, Y-E-S or even Y-U-P. In this lesson you learned what case statements are used for and how to create them. If you find yourself using an if statement to compare the same variable against different values you can use a case statement in its place. You learned how to create patterns using wildcards. You also learned how to execute the same block of code from multiple values by separating the patterns with a pipe.

About the Author
Avatar
Jason Cannon
Founder, Linux Training Academy
Students
304
Courses
40
Learning Paths
4

Jason is the founder of the Linux Training Academy as well as the author of "Linux for Beginners" and "Command Line Kung Fu." He has over 20 years of professional Linux experience, having worked for industry leaders such as Hewlett-Packard, Xerox, UPS, FireEye, and Amazon.com. Nothing gives him more satisfaction than knowing he has helped thousands of IT professionals level up their careers through his many books and courses.

Covered Topics