Shell Scripting L6
Shell Scripting L6
Lec-06
The case Statement
● The case statement supports multiway
branching based on the value of a single
string.
● General form:
case string in
pattern1)
command_set_11
;;
pattern2)
command_set_2
;; (similar to break in C prog.)
…
case Example
#!/bin/sh
echo -n 'Choose command [1-4] > '
read reply
echo
case $reply in
Use the pipe symbol “|” as a
"1")
logical
date or between several choices.
;;
"2"|"3")
pwd
;;
"4") Provide a default case when
ls no
;; other cases are matched.
*)
echo Illegal choice!
;;
esac
Redirection in Bourne Shell Scripts (2)
● Input redirection:
– wc -l users.txt
● Output: 2 users.txt
– Wc -l < userx.txt
● Output: 2
– ‘<<’ operator as an instruction to read input until it finds a
line containing the specified delimiter. All the input lines up
to the line containing the delimiter are then fed into the
standard input of the command.
– E.g. wc -l << EOF
● This is a simple lookup program
● for good (and bad) restaurants
● in Cape Town.
● EOF
– Output: 3
Redirection in Bourne Shell Scripts (3)
Another example:
#!/bin/sh
cat << EOF
This is a simple lookup program
for good (and bad) restaurants
in Cape Town.
EOF
● Output:
This is a simple lookup program
for good (and bad) restaurants
in Cape Town.
Redirection in Bourne Shell Scripts (4)