
In Shell programming we combine multiple commands in a file (called shell script) to perform complex tasks.
A shell script normally has an extension .sh but is not mandatory.
Handling input:
read command is used to take input from the user.
vim myscript.sh
# script to display date, time, directory and its contents
Echo “Today’s date is”
date
sleep 2
echo “your current directory is”
pwd
sleep 2
echo “it contains:”
ls | more
echo “What is your name?”
read n
echo “Welcome to Shell-Programming, $n”
(save & exit)
. /myscript.sh
Exit Status
Every process return a value after its execution:
0 - The Expression parameter is true.
1 - The Expression parameter is false or missing.
>1 - An error occurred.
$?
(To display exit status)
The && and || operators:
&& command used when we want to execute the second
command only if the first command succeeds.
grep ‘Unix’ book.list && echo “book found” (‘ is “ key)
|| it is just opposite of &&.It is used when we want to execute the second command only if the first command fails.
grep ‘Maths’ book.list || echo “book not found”
Control Structures:
It include if … then, for … in, while, until, case statements and
break and continue statements.
Conditional Execution:
the if conditional and test are used in shell scripts to test for a
condition and then act according to the result of test.
Syntax:
if condition
then
command 1
command 2
else
command 3
command 4
fi
Note: if you want to use then in the same line as if,
use semicolon as follows
if condition; then ……
The if-then-else construct test whether the exit status of a command (condition following if) is 0 (i.e. True), and if so it execute the set of commands following then, otherwise it executes the set of commands following else. The else portion is optional.
Example:
vim a.sh
if grep ‘Unix’ book.list
then
echo “book found”
else
echo “book not found”
fi
(save and exit)
Nested if-else and elif:
The if-else construct can be nested. For example the following is a nested if conditional that displays the corresponding name of day:
vim b.sh
echo “Enter the no. for days in Week: "
read a
if [ $a –eq 1 ]; then echo “Mon”
else if [ $a –eq 2 ]; then echo “Tue”
else if [ $a –eq 3 ]; then echo “Wed”
else if [ $a –eq 4 ]; then echo “Thu”
else if [ $a –eq 5 ]; then echo “Fri”
else if [ $a –eq 6 ]; then echo “Sat”
else if [ $a –eq 7 ]; then echo “Sun”
else echo “invalid number”
fi fi fi fi fi fi fi
(save and exit)
Or (else if can be replaced by elif,in the following, fi will be used only once as there isonly one if, elif don't require corresponding terminating fi statement)
vim b.sh
echo “Enter the no. for days in Week: “
read a
if [ $a –eq 1 ]; then echo “Mon”
elif [ $a –eq 2 ]; then echo “Tue”
elif [ $a –eq 3 ]; then echo “Wed”
elif [ $a –eq 4 ]; then echo “Thu”
elif [ $a –eq 5 ]; then echo “Fri”
elif [ $a –eq 6 ]; then echo “Sat”
elif [ $a –eq 7 ]; then echo “Sun”
else echo “invalid number”
fi
(save and exit)
String tests:
test 100 –eq 200; echo $?
{Test whether 100 is equal to 200
(Output: 0= right, 1= wrong)}
or
Note: null string = a character string that contains no data.
a=10; b=20
test $b –gt $a
echo $?
0
Example:
vi 1.sh
a=10; b=20
if test $b –gt $a
then
echo “b is greater than a”
else
echo “b is not greater than a”
fi
(save & exit)
vi 2.sh
echo “Type any number: ”
read a
echo “Type another number: ”
read b
if [ $a -gt $b ]
then
echo “$a is a greater than $b”
else
echo “$a is less than $b”
fi
(save & exit)
FILE TEST:
-e |
file exists |
-f |
file is a regular file (i.e. it is not a directory or device file) |
-s |
file is not of zero size |
-d |
file is a directory |
-b |
file is a block device |
-c |
file is a character device |
-r |
file has read permission (for the user running the test) |
-w |
file has write permission (for the user running the test) |
-x |
file has execute permission (for the user running the test) |
test file1 -nt file2; echo $?
(-nt= newer than)
test file1 -ot file2; echo $?
(-ot= older than)
test -e abc; echo $?
(-e= whether abc (file/directory) exist or not; 1=no, 0=yes)
test -f abc; echo $?
(-f= whether abc is a file or not; 1=no, 0=yes)
ls –l /var/lib/mysql/mysql.sock
srwxrwxrwx 1 mysql mysql 0 Oct 8 19:54 /var/lib/mysql/mysql.sock
(the first letter <s> denote it’s a socket)
test -S /var/lib/mysql/mysql.sock; echo $?
{Result = 0 (yes)}
test -s file; echo $?
-s=whether it’s a zero file or not [a file with no content (zero size)]
abc (written something) result = 1
xyz [written nothing (0 size) result=0
(don’t take it wrong, it’s true)
Note: man test
For and sequence:
the loop or iteration construct directs a program to perform a set of operations again and again until a specified condition is achieved.
Shell provides the following two commonly used loop constructs:
a) for
b) while
Syntax: (for loop)
for variable in [list]
do
commands
done
Note: [list] specifies a list of values which the variable can take. The loop is then executed for each value mentioned in the list. For example, the following will display three planets of the solar system
for planet in Mercury Venus Earth
do
echo $planet
done
Syntax: (while loop)
while [condition]
do
commands
done
Note:
while loop construct contains the condition first. If the condition is satisfied, the control executes the statement following the while loop, else it ignores these statements.
The commands between do and done are executed as long as the condition holds true.
For example, the following will print the values 1 to 10 in reverse order. i is first initialized to 10. The commands within the do-done block will print the value of i and then decrement it as long as value in i is greater than 0 (1):
i=10
while [ $i -gt 0 ]
do
echo $i
i=`expr $i - 1` (where ` = ~` key)
done
The following while loop check every 20 seconds whether a particular file (say abc.txt) exists or not, and as soon as the file becomes available, it displays its contents, and if file (abc.txt) doesn't exist it loop continues to run.
while [ ! -e abc.txt ]
do
sleep 20
done
cat abc.txt
Continue and break:
The break statement cause an immediate exit from the loop in which it is written.
Note:
If you need to come out of a running program immediately without letting it to perform any further operation in loop, then you can use break statement.
Example:
vi script.sh
i=5
while [ $i -le 5 ]
do
echo $i
if [ $i -le 0 ]
then
echo “0 or –ve value found”
break
fi
i=`expr $i - 1`
done
(save & exit)
Case:
The case statement matches an expression with multiple alternatives.
case “$variable” in
condition 1) commands;;
condition 2) commands;;
----------------------
----------------------
condition n) commands
esac
Note:
Each alternative ends with a ) and each condition block with two semicolons (;;). The last condition block need not be terminated with ;;. The case block ends with esac (case spelled backwards).
Example:
vim case.sh
echo “Press any key”
read key
echo “You have typed a”
case “$key” in
[a-z]) echo “lowercase letter”;;
[A-Z]) echo “uppercase letter”;;
[0-9]) echo “digit”;;
*) echo “punctuation, space or any other key”
esac
Note:
* has got double meaning if used in the case construct. (When used as a part of a pattern it function as a wild-card and) when used as the last option it matches a value not matched by the previous options. If no match is found, the command following the option (*) is executed.
Using Positional Parameters:
One can also take inputs from the command line. That is, you specify the arguments in the command line along with the script-name. The arguments so specified are assigned to positional parameters. The first argument stored in the positional parameter $1, the second into $2 and so on. $0 will have the script-name.
Note: after $9 the argument must be enclosed in brackets i.e. $[10], $[11], $[12] etc.
Example:
vim myscript.sh
echo “Your first argument is: $1”
echo “Your second argument is: $2”
echo “Your third argument is: $3”
echo “Name of the Shell Script is: $0”
(save and exit)
sh myscript.sh are you free? (displays the following)
Your first argument is: are
Your second argument is: you
Your third argument is: free?
Name of the Shell Script is: myscript.sh
Note:
In the above command the positional parameter $0 is assigned to myscript.sh, $1= “are”, $2=“you” and $3=“free?”
Shell Script Debugg
Note:
To execute an script script.sh you can run either of following commands:
sh script.sh
bash script.sh
. script.sh
set -x
(To turn ON Shell Script Debugging)
set +x
(To turn OFF Shell Script Debugging)
bash -x script-name
(To debug the script)





