/ factorpad.com / tech / linux-essentials / case-command.html
An ad-free and cookie-free website.
Intermediate
Learn to build a program menu within a script at the Linux command line.
Videos can also be accessed from the Linux Essentials Playlist on YouTube.
Linux bash Shell Script case Statement with Examples (3:55)
Our ninety-seventh word, or command (keyword) to memorize is
case
from our category
Workflow.
case
allows you to build a
program menu.
-options | description |
---|---|
|
None for the case keyword |
Recall from videos (tutorials) #87 to #96, we've used a script to
demonstrate workflow. Now we'll cover
case
and take a break from that
script and return to it in video (tutorial) #100.
Before we start, it helps to think of commands as mini programs and
most follow this structure:
command -option(s) argument(s)
.
The case
command (keyword) has
no traditional options and no arguments as it has its
own specific structure. case
and
the other option we covered for setting up menus
if
are both technically keywords.
Unlike most commands, help is not available with double-dash
--help
, here as
case
is a shell built-in
covered in the bash
manual page.
So why is case
an important command (keyword)?
Well, case
offers a visually appealing
and easy to code alternative to if
, when
setting up menus.
And now you know how to do that.
Okay, the best way to embed this in your memory is by typing in your own terminal window.
Find this on your Mac using a program called Terminal. On Linux use Terminal or Konsole, and currently Microsoft is adding this functionality to Windows.
Here we go. We'll use nano
to see the
structure of case
in a script here,
because that's the best way to go. These usually go multi-lines, right?
(Below is the screen from within nano
.)
Let's walk through the menu setup here, first with a screen
clear
, then
echo
a multi-line block and then a
read
statement to take data in, and
that was from video (tutorial) #89, with a text block and the variable
called choice.
Then the # case code
section starts
with the case
keyword, a variable,
the word in
and then the search term,
or patterns before the parentheses and then the commands go between
there and ;;
double semi-colons.
For the last one, we use a star *
catch-all and it ends in esac
, which
is case
spelled backwards.
(Hit Ctrl-x
to leave
nano
and
y
to confirm saving.)
And last, let's give this a go and run
testcase
.
(The first line of the bash shell script clears the screen.)
Here's our simple menu is sitting within this section, and we didn't wrap this in a loop, so all of this will end, and so let's try 3. Very good.
Okay now you know how to use case
.
And you know the syntax for commands, options and arguments.
One last tip about the case
command
(keyword). So earlier when I said patterns,
bash
will accept expansions,
classes or fixed letters if you'd like to put those in there.
Okay, thanks for visiting today. I hope this was a helpful
introduction to the case
command (keyword).
For an overview of the 100 videos, the 8 quizzes, a cheat sheet, the categories and a Q&A section, visit:
This is not a website to lose contact with. Subscribe at YouTube, follow @factorpad on Twitter and join the no-spam email list.
while
statement,
hit Back.for
statement, click
Next./ factorpad.com / tech / linux-essentials / case-command.html
A newly-updated free resource. Connect and refer a friend today.