Shell Script: Case structure and Select Statement

The case structure like if statement is used in decision making in the shell script. We already find out how to make the decisions using if and else statement. Case structure provides a multiple branch decision mechanism. The case statement works both on bash shell and korn shell. You can install on Korn shell in any distro, see here. On the other hand, select statement is used to print the menu and do anything the user wants under the menu. Select statement only works on Korn shell. Select statement comes in very handy while making the menu in the program.

CASE:

The syntax of the case structure is:

case test-string in
pattern-1)
commands-1
;;
pattern-2)
commands-2
;;
pattern-3)
commands-3
;;
...
esac

Let’s take an example. We are going to build a program which will ask the user to enter any one alphabet among the three. For three different alphabets we will have the three different messages. For example: if the user inputs A we will print something like You have entered A and if the user inputs B we will print You have entered B. We will have the different statements for different user inputs.

echo "Enter A, B, or C: c"
read alphabet
case "$alphabet" in
a|A)
echo You entered A
;;
b|B)
echo You entered B
;;
c|C)
echo You entered C
;;
*)
echo Only enter A, B or C!!!!
;;
esac

The program shows itself. The alphabet that the user inputs is stored in the variable called alphabet. The “|” character in a|A is OR. That means, if the user inputs a or A, it will print you entered A. Remember Linux is case sensitive thus a and A is different. It will check the variable alphabet from the top. If it’s A it will print the statement and the program exits and likewise if it’s B and C it will print the respective statement and the program exits. But, if the user inputs other alphabet except A, B or C Only enter A, B or C statement will be printed. See the output:

case structure in shell script

SELECT:

First, let me remind you again select statement works only in Korn shell. So make sure korn shell is installed and while running the shell script type:

ksh program_name

Select statement mainly consist of three things: Menu, PS3 and block of code between do and done. The select statement displays the menu and asks the user inputs by default. The default value of PS3 is #?. Let’ see an example without PS3.

select expression in Addition Subtraction Multiplication division
do
break
done

The first line of code prints the menu with Addition, Subtraction, Multiplication and Division. These all are stored in a variable called expression which is defined after select. The block of code under do and done is executed afterwards. Since, we don’t want to do anything now, we just break the select statement. See the output:

select statement without PS3As you can see #? is printed by default where user has to input something. So it’s gets confusing for user.

Here’s the shell script select statement with PS3.

PS3="Input numbers:"
select expression in Addition Subtraction Multiplication division
do
break
done

select with PS3 in shell scriptAs you can see, Input numbers is printed by using PS3.

0saves
Posted in Shell Scripting | Tagged , , | Leave a comment

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>