7/23/2019 Basics of Unix Shell Scripting
1/24
Basics of UNIX SHELL SCRIPTING
Version 1.0
January, 2013
Sahil [email protected]
mailto:[email protected]:[email protected]:[email protected]7/23/2019 Basics of Unix Shell Scripting
2/24
Basics of UNIX SHELL SCRIPTING-1.0
Notice
This is a controlled document. Unauthorised access, copying, replication or usage for a
purpose other than for which it is intended, are prohibited.
All trademarks that appear in the document have been used for identification purposes only
and belong to their respective companies.
7/23/2019 Basics of Unix Shell Scripting
3/24
Basics of UNIX SHELL SCRIPTING-1.0
TCS Public 1
Document Release Note
Notice No.:
Document details
Name Version no. Description
Basics of UNIX SHELL SCRIPTING 1.0 This document is intended for beginners in UNIX
shell scripting.
Revision details
Action taken
(add/del/change)
Previous
page no.
New page no. Revision description
The documents or revised pages are subject to document control.
Please keep them up-to-date using the release notices from the distributor of the document.
Approved by: Authorised by:
Date: Date:
7/23/2019 Basics of Unix Shell Scripting
4/24
Basics of UNIX SHELL SCRIPTING-1.0
TCS Public 2
About this Document
PurposeThis document helps to learn the basics of UNIX shell scripting.
Pre-Requisite
Participant must be a TCS employee.
Qualifications Criteria
Must be well versed with Basic UNIX commands.
7/23/2019 Basics of Unix Shell Scripting
5/24
Basics of UNIX SHELL SCRIPTING-1.0
TCS Public 3
CONTENTS
BASICS OF SHELL SCRIPTING - INTRODUCTION ............................................................................................. 4
READ STATEMENT .......................................................................................................................................... 5
COMMAND LINE ARGUMENTS ...................................................................................................................... 6
USE OF && AND || LOGICAL OPERATORS .................................................................................................... 8
CONTROL CONSTRUCTS ................................................................................................................................. 9
COMPUTATIONS USING EXPR ..................................................................................................................... 16
SET AND SHIFT : MANIPULATION OF POSITIONAL PARAMETERS .............................................................. 17
DEBUGGING SHELL SCRIPTS USING SET -X ............................................................................................... 18
EXAMPLE : SCRIPT FOR MAKING A BASIC CALCULATOR ............................................................................ 20
7/23/2019 Basics of Unix Shell Scripting
6/24
Basics of UNIX SHELL SCRIPTING-1.0
TCS Public 4
BASICS OF SHELL SCRIPTING - INTRODUCTION
The function of the shell in unix is not restricted to the command interpretation only. There
are many internal commands in unix that when put together with some external
commands can be used as a language. This language is called SHELL SCRIPTING in unix. In
this guide we will discuss the scripting in reference to the lowest Bourne shell. The features
of this shell scripting also applies to Korn and Bash shell but they have some additional
features also. The C shell programming is totally different from these.
A shell program is not compiled before execution as in a very common C language. It runs in
an interpretive mode. It means shell interprets the commands in script in a sequential
manner and then execute them. Each statement is loaded into the memory when it is to be
executed. Shell scripts run slower than those written in high level languages. But speed is
not always the concern as compared to functionality. Moreover a System Administrator
must be a good shell programmer.
(Note-It is possible to use different type of shell to run a script from your login shell using a
interpreter line.)
Suppose there are some commands that we use regularly. Rather than typing each and
every command daily and executing them one by one, we should make a file of these
commands and store this file. So from now whenever we want all those commands to be
executed in the same sequence we will just execute that file. Thus this file itself is a shell
program. Normally we use .sh extension for shell scripts.
Lets write a very basic script.
#!/bin/sh (interpreter line)
# samplescript.sh Name of the script (comment starting from #)
echo My first shell program.
To execute it we just need to write script name with shon the command prompt as:
$ sh samplescript.sh
Here it is to be noted that the comments always start with # sign except for the first line
where it is used with ! sign as an interpreter line. The interpreter line above tell the system
that in what shell a user wants to execute his script. Here sh indicates a bourne shell.
One thing to be noted here is that if you dont have the execute permissions for the file you
wont be able to execute the script. So you have to change the permissions of the file as
mentioned below:
$ chmod +x samplescript.sh
7/23/2019 Basics of Unix Shell Scripting
7/24
Basics of UNIX SHELL SCRIPTING-1.0
TCS Public 5
read STATEMENT
The sample script written above dont ask for any input from the user when we execute this.
In some cases like when a script performs calculations, it will ask for some input from the
user on which it will perform calculations. For such cases read statement is used in the
script. In other words, a read statement is used to make a script interactive.
Here is the example of usage of read:
---------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------------
The code above will display the lines of file flname containing the word pname.
Output:
---------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------------
You can use a single read statement to enter multiple arguments as:
readpname flname
But if the number of arguments entered by the user are more than the number of
arguments mentioned in the command line then the extra arguments will be assigned to the
last variable mentioned in the command line. On the contrary if they are less than the
number of mentioned variable then the leftover variables will remain unassigned.
7/23/2019 Basics of Unix Shell Scripting
8/24
Basics of UNIX SHELL SCRIPTING-1.0
TCS Public 6
Command line arguments
Unix shell scripts can also be run non-intractively which means once you have written a
command the script will not need the attention from user anymore to complete the task.
This can be done using command line arguments. As the name suggests, these are the
arguments which will be passed to the script by writing them in the command line. If you
have read the above section of read statement carefully there will be a question in your
mind that how and what will replace the function of pname and flname then. So here is
the answer, variables called positional parameters are there for this work. In other words
we can say that the command line arguments will be automatically assigned to the
positional parameters in the sequence they are mentioned in the command line. These
parameters are denoted as $1,$2,$3 and so on. One thing to be noted here is that there
is $0 also which will contain the program name or command name always. A few special
parameters are:-
$* - It stores the complete set of positional parameters as a single string.
$#- It stores the total number of arguments passed.
Lets write the same example as above using the command line arguments:
---------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------------
To run the above script a user will write command like this:
$ sh demo1.sh sahildhuria employees.txt # two arguments are passed as
command line arguments
Output:
---------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------------
You can clearly see that the arguments mentioned in the command line are stored in the
positional parameters $1,$2 respectively.
7/23/2019 Basics of Unix Shell Scripting
9/24
Basics of UNIX SHELL SCRIPTING-1.0
TCS Public 7
exit
To see whether a command has run successfully or not we can use a shell parameter $?.
The value returned by this parameter tell the user whether the command has been runsuccessfully or not.
Lets check this out:
------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------
1 in the output shows the failure to open the file.
If the file would have been opened, it would have returned 0.
7/23/2019 Basics of Unix Shell Scripting
10/24
Basics of UNIX SHELL SCRIPTING-1.0
TCS Public 8
Use of && and || logical operators
These are used to run two commands when the execution of one of the command depends
upon the result of other command. For example:
cmd1 && cmd2 (cmd2 will execute only if cmd1 has successfully run)
cmd1 || cmd2 (cmd2 will execute only if cmd1 has failed to run)
The || operator is very useful when we want to terminate a script if the execution of the
desired command fails, e.g,
grep $1 $2 || exit 1
Here if there is no match of $1(sahildhuria) in $2(employees.txt) the script will be
terminated because then exit 1 will get executed.
These operators are very useful when making very simple decisions. When complex
decisions are to be made then if statement have to be used as explained ahead.
Command Substitution
Command substitution allows the output of a command to be substituted in place of the
command name itself. This can be done in two ways as shown below:
$(Command) or Command
The command will be executed in a sub-shell environment and the standard output of the
shell will replace the command substitution when the command completes.
7/23/2019 Basics of Unix Shell Scripting
11/24
Basics of UNIX SHELL SCRIPTING-1.0
TCS Public 9
Control Constructs
The flow of control within shell script is done via four main constructs viz.;
if...then...elif..else, do...while, for and case. These are discussed one by one as below:
Form1
if command is successful
then
execute this command
else
execute this command
fi
The if CONDITIONAL
The generic forms of this control construct is as:
Form2
if command is successful
then
execute this command
fi
Form3
if command is successful
then
execute this command
elif command is successful
then
execute this command
else
execute this command
fi
A few things that should be always keep in mind while using if construct are:
- The fi is necessary to end the if construct.
- Then is also necessary after every if or else to execute the following
statements.
Example:-
---------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------------
Output:
---------------------------------------------------------------------------------------------------------------------------
7/23/2019 Basics of Unix Shell Scripting
12/24
Basics of UNIX SHELL SCRIPTING-1.0
TCS Public 10
---------------------------------------------------------------------------------------------------------------------------
As we know grep will return an exit status, the if construct use this exit status to proceedfurther in the code block. In the above code if the returned status is 0,means grep executed
successfully. And then Pattern found will be displayed. If the returned status is other than
0 then Pattern not found will be displayed.
Use of test and []
In case of expressions if cant handle the true or false status returned by expressions.
Because if decides flow of execution on the basis of numerical (0 or 1) exit status of the
command. But in the case of expressions exit status is returned as either true or false. So
here test statement comes in operation. A test statement evaluates the expression and
returns 0 or 1 on the basis of true or false returned by the expression. test actually doesnt
display 0 or 1 after evaluation, but is sets a parameter $?. In the following example we willsee this.
Example:
$ set x=1; y=3 ; z=5
$ test $x eq $y ; echo $?
1 (means the result was false and these are not equal)
$ test $x -lt $y ; echo $?
0 (means x is less than y and result was true)
The numerical operators used for comparison are different from those used in other
languages. Here is the list of operators used by test:
OPERATORS MEANING
-eq Equal to
-ne Not equal to
-gt Greater than
-ge Greater than or equal to
-lt Less than
-le Less than or equal to
The [] is a shorthand for test. For example,
test $x -eq $y can be written as [ $x -eq $y ].
LOOPING
Do..While Loop
7/23/2019 Basics of Unix Shell Scripting
13/24
Basics of UNIX SHELL SCRIPTING-1.0
TCS Public 11
Loops are used to repeat a set of instructions. Previously we rectified a faulty response using
if but there was no provision of doing the same things until a certain condition remains true.
Loops are used for such situations. There are three types of loops: while,until and for.
While loop repeatedly performs a set of commands until the control command returns a
true exit status. The generic syntax is:
While condition is true
do
commands
done
Lets use a while loop along with a test statement,
---------------------------------------------------------------------------------------------------------------------------
-----
---------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------------
One of the good applications of the while loops is that we can make the system to pause
until certain condition is met. In the following example system goes to sleep (remains
paused) until a.lst file is created and becomes readable. And when it becomes readable it
displays a message,
---------------------------------------------------------------------------------------------------------------------------
7/23/2019 Basics of Unix Shell Scripting
14/24
Basics of UNIX SHELL SCRIPTING-1.0
TCS Public 12
---------------------------------------------------------------------------------------------------------------------------
This program will check after every 30 second if the file a.lst has been created and readable.
If its created and readable then it will display the message The file is created.
Until
It is just reverse of the while loop, means it will execute the body of the loop until the
condition remains false. As soon as the condition becomes true it comes out of the loop.
for loop
The syntax for the for loop is as follows:
for variable in list
do
commands
done
Its different from the while loop in the sense that its executed a specific number of times.
Whereas in while loop it waits for the condition to become false to stop the loop, in for loop
the same applies but with a predefined limitation as shown in the syntax. So, here the
commands get executed until the words from the list gets assigned to variable in turn. It will
be more clear from the following example.
---------------------------------------------------------------------------------------------------------------------------
7/23/2019 Basics of Unix Shell Scripting
15/24
Basics of UNIX SHELL SCRIPTING-1.0
TCS Public 13
---------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------------
forloop is the most commonly used loop in the unix system. The list can consist of different
kind of expressions. For example, list from variables, list from command substitution, list
from wild-cards, list from positional parameters are the few examples. These can be learned
during advanced shell programming.
case first matches the expression with pattern1. If the match is found then it executes
command1, if not then it continues in same manner to fine the match. Each command list is
terminated with a pair of semicolons except for the last command list where its optional.
Lets demonstrate caseconditional with an example.
case conditional
The syntax of the case conditional is:
case expression in
pattern1) command1 ;;
pattern2) command2 ;;
pattern3) command3 ;;
..
Esac
7/23/2019 Basics of Unix Shell Scripting
16/24
Basics of UNIX SHELL SCRIPTING-1.0
TCS Public 14
---------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------------
Matching Multiple Patterns
Case can also be used for matching multiple patterns. It is very usual when we have to check
for responses as y and Y both. In such cases case becomes useful as shown below:
---------------------------------------------------------------------------------------------------------------------------
7/23/2019 Basics of Unix Shell Scripting
17/24
Basics of UNIX SHELL SCRIPTING-1.0
TCS Public 15
---------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------------
Here if the user has entered n or N or anything else, the program will be stopped and control
will be returned to the prompt. Even if the user has entered y or Y. the same will happen butwith a little delay that is not noticeable here. You can check the script by echoing something
like your name then the script will exit but after displaying your name on the screen.
7/23/2019 Basics of Unix Shell Scripting
18/24
Basics of UNIX SHELL SCRIPTING-1.0
TCS Public 16
Computations using expr
The bourne shell itself cant perform numeric computations. It has to rely on the external
expr command these functions. Other than numeric computations expr can be used for
manipulating string.
Expr can perform basic four arithmetic operations and also the modulus(remainder) function
as well.
---------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------------
The operators should have whitespace on both of their side. expr also be used to assign a
value to a variable as:
$x = 5
$x = expr $x + 1
$ echo $x
Anwer is 6.
7/23/2019 Basics of Unix Shell Scripting
19/24
Basics of UNIX SHELL SCRIPTING-1.0
TCS Public 17
set and shift : Manipulation of positional parameters
set is used to set the value of positional parameters. The arguments given to the set
command gets assigned to $1,$2 and so on in the same order as they are mentioned in the
argument list. The value of $* and $# is also automatically get set. The following example
shows this.
---------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------------
shift command is used to shift the positional parameters to one position left. It will be more
clear after the following example.
---------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------------
7/23/2019 Basics of Unix Shell Scripting
20/24
Basics of UNIX SHELL SCRIPTING-1.0
TCS Public 18
Debugging shell scripts Using set -x
Apart from assigning values to the positional parameters, set also serves as a debugging tool
for the shell scripts. Usingx options with set in the beginning of the script or even at the
shell prompt displays every statement of the script when it gets executed.It is displayed with
a + sign in the beginning. Since now you can see the executed statement of the script, you
can make some modifications in the script if you want. To turn off the functionality of the
set x use set +x right after you you have written set x. It will nullify the usage of set x in
the script but it will display the set +xin the output. It is because set x is executed before
set +x, so first set +xis displayed and then it gets executed and nullify the effect of set x.
Lets demonstrate the usage of theset xcommand using the same example as we used in
the command line arguments topic above. There we executed the script as :
---------------------------------------------------------------------------------------------------------------------------
7/23/2019 Basics of Unix Shell Scripting
21/24
Basics of UNIX SHELL SCRIPTING-1.0
TCS Public 19
---------------------------------------------------------------------------------------------------------------------------
Using set +x
---------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------------
This is an ideal tool to use if you have problems finding why your scripts dont work in the
manner as they are expected.
7/23/2019 Basics of Unix Shell Scripting
22/24
Basics of UNIX SHELL SCRIPTING-1.0
TCS Public 20
Example : Script for making a basic CALCULATOR
In this example code, there is a script to make a basic calculator that can perform four basic
arithmetic operations viz, Addition, Subtraction, Multiplication and Division. A code snippet
for the same is as shown below:
---------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------------
7/23/2019 Basics of Unix Shell Scripting
23/24
Basics of UNIX SHELL SCRIPTING-1.0
TCS Public 21
In the above code there is a whileloop in which there is a casestatement that performs
arithmetic operations based on the users choice. Initially a true condition is set to enter in
the whileloop and then inside the loop there is an option whether the user wants to
continue with the more calculations or he wants to exit from the calculator. Based on his
choice further operations are performed. The Output of the above code is displayed as
below:
---------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------------
7/23/2019 Basics of Unix Shell Scripting
24/24
Basics of UNIX SHELL SCRIPTING-1.0
TCS Public 22
THANK YOU