From the course: Linux Bash Shells and Scripts: Streamlining Tasks and Enhancing Workflows with Automation
Unlock this course with a free trial
Join today to access over 24,900 courses taught by industry experts.
Case statements - Linux Tutorial
From the course: Linux Bash Shells and Scripts: Streamlining Tasks and Enhancing Workflows with Automation
Case statements
- [Instructor] Case statements can be handy. The syntax is a bit of a challenge, so you need to look at this kind of carefully. Otherwise, it could be frustrating. Case statement uses the word case, then you have some sort of expression, typically the value of a variable, then the word in. And then on a new line, you have some sort of pattern, and the patterns can get kind of sophisticated. And then you have a closed paren. There's no open paren there's just a closed paren. Then you have a list of commands that will get executed if the expression matches the pattern. And then you end that list with two semicolons. So you can have a bunch of lines. And then at the end, after all the lines, put two semicolons. And then you can have another pattern and so on. And then you end the case statement with the word case spelled backwards, esac. Let's look at an example. So we have case, and then we're going to get the value of the variable ans, the answer. And then we have the word in. And then…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
The typeset and declare commands for variables and arrays5m 59s
-
(Locked)
Looping with while sequences and reading input6m 12s
-
(Locked)
Looping with for, the seq command, and backticks8m 3s
-
(Locked)
Defining functions and using return and exit4m 17s
-
(Locked)
Using file descriptors, file redirection, and pipes7m 23s
-
(Locked)
Here documents3m 54s
-
(Locked)
Open and close file descriptors5m 22s
-
(Locked)
Control-flow if-then-else with the test command4m 42s
-
(Locked)
Case statements3m 45s
-
(Locked)
Using arithmetic operators3m 26s
-
(Locked)
Challenge: Variables in functions, loops, and arithmetic2m 46s
-
(Locked)
Solution: Variables in functions, loops, and arithmetic2m 42s
-
-
-
-