Unix Shell Scripts
-Prakash Lambha
Conditional Statements
 Unix Shell supports conditional statements
which are used to perform different actions
based on different conditions.
 Here we will explain following two decision making
statements:
1) The if...else statements
2) The case...esac statement
The if...else statements:
 If else statements are useful decision making statements
which can be used to select an option from a given set of
options.
 Unix Shell supports following forms of if..else
statements:
1.1) if...fi statement
1.2) if...else...fi statement
1.3) if...elif...else...fi statement
1.1 The if...fi statement
 The if...fi statement is the fundamental control
statement that allows Shell to make decisions and
execute statements conditionally.
 Syntax:
if [ expression ]
then
Statement(s) to be executed if expression is true
fi
1.1 The if...fi statement (Cont.)
 Here Shell expression is evaluated. If the resulting value
is true, given statement(s) are executed.
 If expression is false then no statement would be not
executed.
 Most of the times you will use comparison operators
while making decisions.
 Give you attention on the spaces between braces and
expression. This space is mandatory otherwise you
would get syntax error.
 If expression is a shell command then it would be
assumed true if it return 0 after its execution.
 If it is a Boolean expression then it would be true if it
returns true.
1.1 The if...fi statement (Cont.)
 Example:
#!/bin/sh
a=10
b=20
if [ $a == $b ]
then
echo "a is equal to b"
fi
if [ $a != $b ]
then
echo "a is not equal to b"
fi
This will produce following result:
1.2 The if...else...fi
 The next form of control statement that allows Shell
to execute statements in more controlled way and
making decision between two choices.
 Syntax:
if [ expression ]
then
Statement(s) to be executed if expression is true
else
Statement(s) to be executed if expression is not true
fi
1.2 The if...else...fi (Cont.)
 Example:
#!/bin/sh
a=10
b=20
if [ $a == $b ]
then
echo "a is equal to b"
else
echo "a is not equal to b"
fi
This will produce following result:
a is not equal to b
1.3 The if...elif...fi
 is the one level advance form of control statement that allows Shell to make
correct decision out of several conditions.
Syntax:
if [ expression 1 ]
then
Statement(s) to be executed if expression 1 is true
elif [ expression 2 ]
then
Statement(s) to be executed if expression 2 is true
elif [ expression 3 ]
then
Statement(s) to be executed if expression 3 is true
else
Statement(s) to be executed if no expression is true
fi
1.3 The if...elif...fi (Cont.)
 Example:
#!/bin/sh
a=10
b=20
if [ $a == $b ]
then
echo "a is equal to b"
elif [ $a -gt $b ]
then
echo "a is greater than b"
elif [ $a -lt $b ]
then
echo "a is less than b"
else
echo "None of the condition met"
fi
2. The case...esac
 You can use multiple if...elif statements to perform a
multi-way branch.
 However, this is not always the best solution,
especially when all of the branches depend on the
value of a single variable.
 Unix Shell supports case...esac statement which
handles exactly this situation, and it does so more
efficiently than repeated if...elif statements.
2. The case...esac (Cont.)
 Syntax:
case word in
pattern1)
Statement(s) to be executed if pattern1 matches
;;
pattern2)
Statement(s) to be executed if pattern2 matches
;;
pattern3)
Statement(s) to be executed if pattern3 matches
;;
esac
2. The case...esac (Cont.)
 Example:
#!/bin/sh
FRUIT="kiwi"
case "$FRUIT" in
"apple") echo "Apple pie is quite tasty."
;;
"banana") echo "I like banana nut bread."
;;
"kiwi") echo "New Zealand is famous for kiwi."
;;
esac
 This will produce following result:
New Zealand is famous for kiwi.
Loops: While Loop
 The while loop enables you to execute a set of
commands repeatedly until some condition occurs. It
is usually used when you need to manipulate the
value of a variable repeatedly.
 Syntax:
while command
do
Statement(s) to be executed if command is true
done
Loops: While Loop
 Example:
Here is a simple example that uses the while loop to
display the numbers zero to nine:
#!/bin/sh
a=0
while [ $a -lt 10 ]
do
echo $a
a=`expr $a + 1`
done
Loops: For Loop
 The for loop operate on lists of items. It repeats a set of
commands for every item in a list.
 Syntax:
for var in word1 word2 ... wordN
do
Statement(s) to be executed for every word.
done
 Here var is the name of a variable and word1 to wordN
are sequences of characters separated by spaces
(words).
 Each time the for loop executes, the value of the variable
var is set to the next word in the list of words, word1 to
wordN.
Loops: For Loop
 Here is a simple example that uses for loop to span
through the given list of numbers:
#!/bin/sh
for var in 0 1 2 3 4 5 6 7 8 9
do
echo $var
done
Loops: Until Loop
 The while loop is perfect for a situation where you
need to execute a set of commands while some
condition is true. Sometimes you need to execute a
set of commands until a condition is true.
 Syntax:
until command
do
Statement(s) to be executed until command is true
done
Loops: Until Loop
 Here Shell command is evaluated.
 If the resulting value is false, given statement(s) are executed.
 If command is true then no statement would be not executed and program
would jump to the next line after done statement.
 Example:
Here is a simple example that uses the until loop to display the numbers
zero to nine:
#!/bin/sh
a=0
until [ ! $a -lt 10 ]
do
echo $a
a=`expr $a + 1`
done

Unix shell scripts

  • 1.
  • 2.
    Conditional Statements  UnixShell supports conditional statements which are used to perform different actions based on different conditions.  Here we will explain following two decision making statements: 1) The if...else statements 2) The case...esac statement
  • 3.
    The if...else statements: If else statements are useful decision making statements which can be used to select an option from a given set of options.  Unix Shell supports following forms of if..else statements: 1.1) if...fi statement 1.2) if...else...fi statement 1.3) if...elif...else...fi statement
  • 4.
    1.1 The if...fistatement  The if...fi statement is the fundamental control statement that allows Shell to make decisions and execute statements conditionally.  Syntax: if [ expression ] then Statement(s) to be executed if expression is true fi
  • 5.
    1.1 The if...fistatement (Cont.)  Here Shell expression is evaluated. If the resulting value is true, given statement(s) are executed.  If expression is false then no statement would be not executed.  Most of the times you will use comparison operators while making decisions.  Give you attention on the spaces between braces and expression. This space is mandatory otherwise you would get syntax error.  If expression is a shell command then it would be assumed true if it return 0 after its execution.  If it is a Boolean expression then it would be true if it returns true.
  • 6.
    1.1 The if...fistatement (Cont.)  Example: #!/bin/sh a=10 b=20 if [ $a == $b ] then echo "a is equal to b" fi if [ $a != $b ] then echo "a is not equal to b" fi This will produce following result:
  • 7.
    1.2 The if...else...fi The next form of control statement that allows Shell to execute statements in more controlled way and making decision between two choices.  Syntax: if [ expression ] then Statement(s) to be executed if expression is true else Statement(s) to be executed if expression is not true fi
  • 8.
    1.2 The if...else...fi(Cont.)  Example: #!/bin/sh a=10 b=20 if [ $a == $b ] then echo "a is equal to b" else echo "a is not equal to b" fi This will produce following result: a is not equal to b
  • 9.
    1.3 The if...elif...fi is the one level advance form of control statement that allows Shell to make correct decision out of several conditions. Syntax: if [ expression 1 ] then Statement(s) to be executed if expression 1 is true elif [ expression 2 ] then Statement(s) to be executed if expression 2 is true elif [ expression 3 ] then Statement(s) to be executed if expression 3 is true else Statement(s) to be executed if no expression is true fi
  • 10.
    1.3 The if...elif...fi(Cont.)  Example: #!/bin/sh a=10 b=20 if [ $a == $b ] then echo "a is equal to b" elif [ $a -gt $b ] then echo "a is greater than b" elif [ $a -lt $b ] then echo "a is less than b" else echo "None of the condition met" fi
  • 11.
    2. The case...esac You can use multiple if...elif statements to perform a multi-way branch.  However, this is not always the best solution, especially when all of the branches depend on the value of a single variable.  Unix Shell supports case...esac statement which handles exactly this situation, and it does so more efficiently than repeated if...elif statements.
  • 12.
    2. The case...esac(Cont.)  Syntax: case word in pattern1) Statement(s) to be executed if pattern1 matches ;; pattern2) Statement(s) to be executed if pattern2 matches ;; pattern3) Statement(s) to be executed if pattern3 matches ;; esac
  • 13.
    2. The case...esac(Cont.)  Example: #!/bin/sh FRUIT="kiwi" case "$FRUIT" in "apple") echo "Apple pie is quite tasty." ;; "banana") echo "I like banana nut bread." ;; "kiwi") echo "New Zealand is famous for kiwi." ;; esac  This will produce following result: New Zealand is famous for kiwi.
  • 14.
    Loops: While Loop The while loop enables you to execute a set of commands repeatedly until some condition occurs. It is usually used when you need to manipulate the value of a variable repeatedly.  Syntax: while command do Statement(s) to be executed if command is true done
  • 15.
    Loops: While Loop Example: Here is a simple example that uses the while loop to display the numbers zero to nine: #!/bin/sh a=0 while [ $a -lt 10 ] do echo $a a=`expr $a + 1` done
  • 16.
    Loops: For Loop The for loop operate on lists of items. It repeats a set of commands for every item in a list.  Syntax: for var in word1 word2 ... wordN do Statement(s) to be executed for every word. done  Here var is the name of a variable and word1 to wordN are sequences of characters separated by spaces (words).  Each time the for loop executes, the value of the variable var is set to the next word in the list of words, word1 to wordN.
  • 17.
    Loops: For Loop Here is a simple example that uses for loop to span through the given list of numbers: #!/bin/sh for var in 0 1 2 3 4 5 6 7 8 9 do echo $var done
  • 18.
    Loops: Until Loop The while loop is perfect for a situation where you need to execute a set of commands while some condition is true. Sometimes you need to execute a set of commands until a condition is true.  Syntax: until command do Statement(s) to be executed until command is true done
  • 19.
    Loops: Until Loop Here Shell command is evaluated.  If the resulting value is false, given statement(s) are executed.  If command is true then no statement would be not executed and program would jump to the next line after done statement.  Example: Here is a simple example that uses the until loop to display the numbers zero to nine: #!/bin/sh a=0 until [ ! $a -lt 10 ] do echo $a a=`expr $a + 1` done

Editor's Notes