Script.: Shell Scripts
Script.: Shell Scripts
As noted earlier, Unix shells are all fully-featured programming languages as well as simple
ways of invoking programs. A file containing a number of shell commands is called a shell
script.
Simple shell scripts are simply a sequence of simple commands (such as are many .profile files).
However, a full set of iterative and conditional constructs are also available.
The very big advantage of shell scripts is that they provide a simple way of automating a number
of otherwise manual tasks. If you need to type a sequence of commands in more than twice, you
probably want to write shell script. Remember, we programmers are all tool makers; we like
making things easier for ourselves.
The shell incorporates a powerful programming feature. It facilitates all programming
constructs such as looping, conditional statements, variable declaration, function etc. A shell
program is a series of UNIX commands that we have learned. The difference being that we had
till now been using these commands to perform an isolated task. But in a shell program, we will
combine these commands to perform complex task.
A sequence of shell commands stored in a file, the name of which is later used to execute
the stored sequence, is known as a shell script. We do not need a special compiler to execute the
shell scripts. These are interpreted and executed by the shell itself.
In shell script it is not necessary to give the name with extension .sh. But customarily
name them .sh extension.
Shell Variables
Shell variable are similar to the variable in other programming languages. They are used
to store and manipulate various types of data within a shell program. The shell variable can hold
a values of any data type, and its data type does not have to be set when declaring the variable.
Its data type is ascertained by the type of data we store in the variable. If we assign a character
value the variable become a character variable.
a=’A’
Values bounded by single quotes Note that there should be no space before and after the
assignment operator (=).
a=10
Numaric Variable
a= “Amit lal”
Float Variable
The rules for naming variable are similar as “C programming languages”.
The values is stored in a variable is retrieved by preceding the variable name with a doller ($)
sign.
Example:
a=10.25
echo “The values of a is $a”
10.25
b=$a
echo “The value of b is $b”
10.25
The result of ls-l command is assigned to the variable a.
1
a= ‘ls-l’
echo $a
Note : Remember that a command can be executed within another command by enclosing the
letter in single quotes( ‘ ’).
# This program display the date and time, current directory and its content.
echo “Todays Date is”
date
echo “Your Current Working Directory is”
pwd
echo “It contains”
ls| more
Execution
The script can be executed by using the command .sh followed by the filename. For
example the pcc can be invoked by the following command:
$ pcc.sh
You can used the chmode command to execute the file.
$ chmode +x pcc.sh
Once this is done you can execute the shell script giving the filename in the command line.
$ pcc.sh
When we execute a shell script, the login shell creates a new shell and the command in the shell
script is executed in this new shell. After executing your shell script, the new shell terminates and
the login shell again takes over the control.
The || command is just opposite of &&. It is used when we want to execute the second
command only if the first command fails. For example, the following statement will print the
message “book not found” if the grep command fails.
Valid expressions
2
$ expr 5 + 3
8
$ expr 5 - 3
2
$ expr 5 \* 3
15
$ expr 5 / 3
1
$ expr 5 % 3
2
A space on either side of the operator is essential in an expr statement. The * is escaped
using a \ so that the shell does not interprets it as one of its meta characters.
In string handling, expr can be used to compute the length of the string, locate the
position of a character in a string or extract a substring. Here expr requires two expressions
separated by a colon (:). The string on which a function to be performed is placed on the left side
of the colon(:) and a regular expression is placed on the left side.
The expression “.*” is used to compute the length of a string for example
$ expr “Pandharipande Computes”: ‘.*’
23
test
The test statement tests a given condition. It returns 1 if the condition is falseand return 0
if the condition is true. It uses certain operators known as test operators, for comparison. For
example –eq is used for “equal to”, -gt for “greater than” etc.
Example:
$ a=10 ; b=20
$ text $b –gt $a
$ echo $?
0 20 is greater than 10
$ text $a –eq $b
$ echo $?
1 20 is not equal to 10
Note the use of test in the above sequence. There should be space on either side of the test
operators. We used $? To find out the result of test command.
The test word can be replaced by the use of [ ], enclosing the condition within brackets as
follows:
$ a=10 ; b=20
3
$ [$b –gt $a]
$ echo $?
0
“test operator”
Operator Implies
-eq equal to
-ne not equal to
-gt greater than
-lt less than
-ge grater than equal to
-le less than equal to
-n str string is not a null string
str same as above
-2 str string str is a null string
str1=str2 string str1 is equal to str2
str1!=str2 string str1 is not equal to str2
Shell Programs
Output
Enter the Radius of Circle: -
3
Area of Circle:
28
4
echo ‘expr $base \* $height / 2’
echo “ ”
echo “Enter length and breadth of a rectangle”
read length
read breadth
echo “Area of Rectangle : “
echo ‘expr $length \* $breadth’
echo “Enter the diameter: ”
read d1
echo “Enter the diameter: ”
read d2
echo “Area of Rhombus is :”
echo ‘expr 1 \* $d1 \* $d2 / 2’
Output
The if Conditional
The if statement in used to check the condition. It is used to takes two-way decisions
depending on the fulfillment of a certain condition. In the shell, the statement uses the following
form, much like the one used in other languages:
if <condition is true>
then
<executes commands>
else
<executes commands>
fi
5
if evaluates a condition which accomplish its command line. If the condition is fulfilled (i.e.
returns a true exit status), then the sequence of commands following it is executes. Then
Keyword then and fi which must necessarily accompany every if conditional. The construct also
provides for an alternate action using the optional keyword else. This is not always required, and
the simplex form of the if statement:
if <condition is true>
then
<execute commands>
fi
# Enter the following sequence at the prompt to check whether the pattern director can be locate
in the file.lst.
# Write a shell script program to accept name, address and phone number & write it to a file.
echo “ Enter file name : = >”
read file1
touch $file1
if test –w $file1
then
echo “Enter name, address and phone number of an employee”
echo “Press ctrl+d to terminate….”
Cat>$file1
else
echo “file is not Writable : =>”
fi
Output
Enter file name : =>nitin
Enter name, address and phone number of an employee
Press ctrl+d to terminate….
Rishi
Dhontoli Nagpur
0712 6560274
//To display the contents of file
Cat mal.sh
Rishi
6
Dhontoli Nagpur
0712 6560274
Note: Tough the access and modification time of a file change when you try to read or write into
it respectively. You may sometime require to set them to predefined values.
Nested ifs:
The if can be used upto nested level. Following is the example that uses three argument to
take the pattern, as well as the input and output filenames. But first it checks whether the right
number of argument have been entered.
# Write a shell script program to check the given file is ordinary, special or directory.
Clear
echo “Enter the File Name :”
read fn
if test –f $fn
then
echo “File is a Ordinary file”
else
if test –d $fn
then
echo “File is a directory file”
else
if test –s $fn
then
echo “File is a special file”
else
echo “File does not exists”
fi
fi
fi
Syntax:
if <condition is true>
then
<execute command>
elif <condition is true>
then
<execute command>
7
<…..>
<…..>
else
<execute command>
fi
A single fi closes the construct, even though there may be a couple of elif statement for testing
alternate conditions. It also resembles the else-if form used by other languages.
$cat filetest.sh
echo “Enter the Name of the file : \c”
read flname
if [ ! –f $flname ]
then
echo “File Does not Exist”
elif [ ! –r $flname ]
then
echo “File is Not Readable”
elif [ ! –w $flname ]
then
echo “File is Not Writable”
else
echo “File is both readable and Writable”
fi
$_
Output
Test the script with two filenames – one that doesn’t exist and one that does:
$ filetest.sh
Enter the Name of the file : emp3.lst
File Does not Exist
$_
$ filetest.sh
Enter the Name of the file : emp.lst
File is Both readable and Writable.
$_
A common mistake made by many, when using test for testing file attribute, is to use the
shell’s wildcards as a substitute for file names. test doesn’t accept them at all, and uses only
literal string.
#Write a shell script program to find the entered number is odd or even.
clear
echo “Enter the Number :”
read n
echo “ ”
if [ ‘expr $n % 2 –eq 0]
then
8
echo “$n is Even”
else
echo “$n is Odd”
fi
Output:
Enter the Number :
6
6 is even
Enter the Number :
5
5 is Odd
# Write a shell script program to find the greatest among three numbers..
Clear
echo “ ”
echo “Enter three numbers”
read a b c
echo “ ”
if [ $a –gt $b –a $a –gt $b ]
then
echo “Grater number is $a”
elif [$b –gt $a –a $b –gt $c ]
then
echo “Greater number is $b”
else
echo “greater number is $c”
fi
Output:
Enter the Three Numbers
257
Greater number is 7
#Write a shell script to calculate the gross salary of employee with following conditions
#a) if the basic sal > 5000 then hra=5% and da=3%
#b) if the basic sal >10000 then hra=10% and DA = 3%
9
da = ‘expr $bs \* 3 / 100’
echo “ ”
echo da = $da
gross= ‘expr $bs + $da + $hra’
echo “ ”
echo “GROSS SALARY = $gross’
echo da = ‘$da’
gross = ‘expr $basic + $hra + $da’
echo “ ”
echo “GROSS SALARY = $gross”
fi
HRA = 350
DA = 210
GROSS SALARY = 7560
Syntax:
for variable in list
do
<execute commands>
done
The loop body is identical in structure to the while and until loops. The additional
keywords here are variable and list. The list consist of a series of character strings, with each
string separated from the other by white space. Each item in the list is assigned to the variable in
turn and the loop body is executed. It performs the loop as many times as there are words in the
list. A simple example can help you understand the things better:
$ for x in 1 2 4 5
>do
> echo “The value of x is $x”
10
>done
Output:
The value of x is 1
The value of x is 2
The value of x is 4
The value of x is 5
$_
Output:
Enter the range
5
1
23
456
7 8 9 10
echo \
11
done
Output:
Enter the range
5
1
22
333
4444
55555
Output:
1 2 3 4 5 6 7 8 9 10
12
read no
sum=0
while [ $no –ne 0 ]
do
rem=’expr $no % 10’
sum=’expr $sum \* 10 + $rem’
no=’expr $no / 10’
done
echo “ ”
echo “Reverse number is = $sum”
Output:
# Write a shell script program to print the factorial of the given number.
clear
echo “ ”
echo “Enter the number”
read num
fact=1
while test $num –ne 0
do
fact=’expr $fact \* $num’
num=’expr $num – 1’
done
echo “Factorial = $fact”
Output:
Enter the Number
5
Factorial = 120
13
c=’expr $a + $b’
echo “$c”
done
Output:
Enter the Number
5
1
1
2
3
5
8
Output:
1 2 3 4 5 6 7 8 9 10
14
Syntax:
case <expression> in
<pattern 1>) <execute commands>;;
<pattern 1>) <execute commands>;;
<pattern 1>) <execute commands>;;
<….>
<….>
esca
The keyword here are in and esac, and the symbols ;; are used as option terminator. The construct
also uses the ) to delimit the pattern form the action. It matches the expression first pattern1, and
is successful, execute the commands associated with it. If doesn’t, then it falls through and
matches pattern2, and so on. Each command list is terminated by a pair of semicolons, and the
entire construct is closed with an esac(reverse of case).
Consider a simple example using this construct. You can device a script, which accept
values from 1 to 5, and performs some action depending on the number keyed in. The code for
this menu is stored in the file menu.sh.
$ cat menu.sh
cat <<END
MENU
1. List of files
2. Processes of user
3. Today’s date
4. Users of system
5. Quit to UNIX
END
echo “Enter Your option: \c”
read choice
echo
case “$choice” in
1) ls –l;;
2) ps –f;;
3) date ;;
4) who;;
5) exit;;
esac
$_
# Write a shell script program to check input character is digit, character of special symbol.
clear
echo “Enter any character :”
read ch
case $ch in
[a-z])
echo “The character is in lower case”
break;;
[A-Z])
15
echo “The character is in UPPER case”
break;;
[0-9])
echo “The character is in digit”
break;;
*)
echo “The character is in Special Character”
break;;
esac
Output:
Enter any character :
A
The character is in UPPER case
Enter any character :
a
The character is in lower case
Enter any character :
5
The character is in digit
Enter any character :
^
The character is in Special Character
16
string, the default statement is executed. When a case or default statement is executed, it
executes a sequence, every statement between it and the following breaksw statement.
foreach:-
foreach statement is used to control the flow of commands. foreach successively execute
a group of statements, reading a new word from the specified variable at each execution. The
format of the foreach shell command is:
Syntax:
foreach variable (wordlist)
.
.
.
.
End
The specified variable will be set to a word from “wordlist” on each execution of foreach.
The foreach loop is terminated by an end command. When the last word has been used the shell
executes the next command after the end command.
Korn Shell
Number Comment
Name
This number does not correspond to a real signal, but the corresponding trap
0 EXIT is executed before script termination.
2 INT The interrupt signal typically is generated using the DEL or the ^C key
The quit signal is typically generated using the ^\ key. It is used like the INT
3 QUIT signal but explicitly requests a core dump.
17
signal.list include the signal number (Separated by space) of the signals which we want to trap.
The trap command is normally placed at the beginning of a program source-code.
For example, to mention that execute the ls command whenever Ctrl +D signal (signal
number 2) is received, we need to place the following command at the beginning of shell
program.
trap “ls” 2
The unix utility awk is a pattern matching. The UNIX utility awk is a pattern matching
and processing language. It is used to search one or more specified files for record that match a
specified pattern. If awk finds a match, the corresponding action is performed awk is a powerful
search tool. It also performs such operations as omitting parts of a file, counting number of
occurrence of a pattern in a file and writing reports.
It is an excellent filter and report writer. Many UNIX utilities generates rows and
columns of information. AWK is an excellent tool for processing these rows and columns, and is
easier to use AWK than most conventional programming languages. It can be considered to be a
pseudo-C interpretor, as it understands the same arithmatic operators as C. AWK also has string
manipulation functions, so it can search for particular strings and modify the output. AWK also
has associative arrays, which are incredible useful, and is a feature most computing languages
lack. Associative arrays can make a complex problem a trivial exercise.
A awk program is only a few lines long. This makes it more useful than users attaining
the same purpose through traditional programming languages such as Pascal or C. The latter
requires more though, more lines of code and hence more time. The awk programs are short
because a large amount of detail is handled by the language itself automatically. The awk can
manipulate large data-files in short (often-single line) program. Thus increasing productivity and
efficiency. awk syntaxes are similar to those of ‘c’ language.
Basic Structure
pattern { action }
18
The pattern specifies when the action is performed. Like most UNIX utilities, AWK is line
oriented. That is, the pattern specifies a test that is performed with each line read as input. If the
condition is true, then the action is taken. The default pattern is something that matches every
line. This is the blank or null pattern. Two other important patterns are specified by the keywords
"BEGIN" and "END." As you might expect, these two words specify actions to be taken before
any lines are read, and after the last line is read. The AWK program below:
adds one line before and one line after the input file. This isn't very useful, but with a simple
change, we can make this into a typical AWK program:
The file is call as "FileOwner." But let's not put it into a script or file yet. I will cover that part in
a bit. Hang on and follow with me so you get the flavor of AWK.
The characters "\t" Indicates a tab character so the output lines up on even boundries. The "$8"
and "$3" have a meaning similar to a shell script. Instead of the eighth and third argument, they
mean the eighth and third field of the input line. You can think of a field as a column, and the
action you specify operates on each line or row read in.
There are two differences between AWK and a shell processing the characters within double
quotes. AWK understands special characters follow the "\" character like "t". The Bourne and C
UNIX shells do not. Also, unlike the shell (and PERL) AWK does not evaluate variables within
strings. To explain, the second line could not be written like this:
{print "$8\t$3" }
That example would print "$8 $3." Inside the quotes, the dollar sign is not a special character.
Outside, it corresponds to a field. What do I mean by the third and eight field? Consider the
Solaris "/usr/bin/ls -l" command, which has eight columns of information. The System V version
(Similar to the Linux version), "/usr/5bin/ls -l," has 9 columns. The third column is the owner,
and the eighth (or nineth) column in the name of the file. This AWK program can be used to
process the output of the "ls -l" command, printing out the filename, then the owner, for each
file. I'll show you how.
One more point about the use of a dollar sign. In scripting languages like Perl and the various
shells, a dollar sign means the word following is the name of the variable. Awk is different. The
dollar sign means that we are refering to a field or column in the current line. When switching
19
between Perl and AWK you must remener that "$" has a different meaning. So the following
piece of code prints two "fields" to standard out. The first field printed is the number "5", the
second is the fifth field (or column) on the input line.
BEGIN { x=5 }
{ print x, $x}
So let's start writing our first AWK script. There are a couple of ways to do this.
ls -l | FileOwner
This might generate the following if there were only two files in the current directory:
File Owner
a.file barnett
another.file barnett
- DONE -
There are two problems with this script. Both problems are easy to fix, but I'll hold off on this
until I cover the basics.
20