0% found this document useful (0 votes)
2 views7 pages

Shell Scripting L6

Uploaded by

jefoy11983
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views7 pages

Shell Scripting L6

Uploaded by

jefoy11983
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Shell Programming

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)

Discard the output:


command > /dev/null
e.g. grep “UNIX” file.txt > /dev/null
Assignment-1 Additional Questions
3. Write a shell program to concatenate two strings
and find the length of the resultant string (Hint: s3 =
$s1$s2.
s3 will become the concatenation of s1 and s2).
4. Write a shell program to display the alternate
digits in a given 7-digit number starting from the
first digit.
5. Write a shell script to find the smallest of three
numbers.

You might also like