Vous êtes sur la page 1sur 24

Basics of UNIX SHELL SCRIPTING

Version 1.0

January, 2013

Sahil Dhuria
sahil.dhuria@tcs.com

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.

Basics of UNIX SHELL SCRIPTING-1.0

Document Release Note


Notice No.:

Document details
Name
Basics of UNIX SHELL SCRIPTING

Version no.
1.0

Description
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:

TCS Public

Basics of UNIX SHELL SCRIPTING-1.0

About this Document


Purpose
This 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.

TCS Public

Basics of UNIX SHELL SCRIPTING-1.0

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

TCS Public

Basics of UNIX SHELL SCRIPTING-1.0

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
# samplescript.sh Name of the script
echo My first shell program.

(interpreter line)
(comment starting from #)

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

TCS Public

Basics of UNIX SHELL SCRIPTING-1.0

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:
read pname 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.

TCS Public

Basics of UNIX SHELL SCRIPTING-1.0

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
Output:

# two arguments are passed as


command line arguments

---------------------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------------------You can clearly see that the arguments mentioned in the command line are stored in the
positional parameters $1,$2 respectively.

TCS Public

Basics of UNIX SHELL SCRIPTING-1.0

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 run
successfully 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.

TCS Public

Basics of UNIX SHELL SCRIPTING-1.0

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.

TCS Public

Basics of UNIX SHELL SCRIPTING-1.0

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:
The if CONDITIONAL
The generic forms of this control construct is as:
Form1
Form2
if command is successful
then
execute this command
else
execute this command
fi

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:
---------------------------------------------------------------------------------------------------------------------------

TCS Public

Basics of UNIX SHELL SCRIPTING-1.0

--------------------------------------------------------------------------------------------------------------------------As we know grep will return an exit status, the if construct use this exit status to proceed
further 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 will
see 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
-eq
-ne
-gt
-ge
-lt
-le

MEANING
Equal to
Not equal to
Greater than
Greater than or equal to
Less than
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

TCS Public

10

Basics of UNIX SHELL SCRIPTING-1.0

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,
---------------------------------------------------------------------------------------------------------------------------

TCS Public

11

Basics of UNIX SHELL SCRIPTING-1.0

--------------------------------------------------------------------------------------------------------------------------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.
---------------------------------------------------------------------------------------------------------------------------

TCS Public

12

Basics of UNIX SHELL SCRIPTING-1.0

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------------------for loop 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 conditional
The syntax of the case conditional is:
case expression in
pattern1) command1 ;;
pattern2) command2 ;;
pattern3) command3 ;;
..
Esac
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 case conditional with an example.

TCS Public

13

Basics of UNIX SHELL SCRIPTING-1.0

---------------------------------------------------------------------------------------------------------------------------

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

---------------------------------------------------------------------------------------------------------------------------

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:
---------------------------------------------------------------------------------------------------------------------------

TCS Public

14

Basics of UNIX SHELL SCRIPTING-1.0

---------------------------------------------------------------------------------------------------------------------------

---------------------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------------------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 but
with 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.

TCS Public

15

Basics of UNIX SHELL SCRIPTING-1.0

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.

TCS Public

16

Basics of UNIX SHELL SCRIPTING-1.0

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.
---------------------------------------------------------------------------------------------------------------------------

---------------------------------------------------------------------------------------------------------------------------

TCS Public

17

Basics of UNIX SHELL SCRIPTING-1.0

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. Using x 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 +x in the output. It is because set x is executed before
set +x, so first set +x is displayed and then it gets executed and nullify the effect of set x.
Lets demonstrate the usage of the set x command using the same example as we used in
the command line arguments topic above. There we executed the script as :

---------------------------------------------------------------------------------------------------------------------------

TCS Public

18

Basics of UNIX SHELL SCRIPTING-1.0

--------------------------------------------------------------------------------------------------------------------------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.

TCS Public

19

Basics of UNIX SHELL SCRIPTING-1.0

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:
---------------------------------------------------------------------------------------------------------------------------

---------------------------------------------------------------------------------------------------------------------------

TCS Public

20

Basics of UNIX SHELL SCRIPTING-1.0

In the above code there is a while loop in which there is a case statement that performs
arithmetic operations based on the users choice. Initially a true condition is set to enter in
the while loop 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:
---------------------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------------------TCS Public

21

Basics of UNIX SHELL SCRIPTING-1.0

THANK YOU

TCS Public

22

Vous aimerez peut-être aussi