12.essential Shell Programming
12.essential Shell Programming
Names of Sub-Units
Shell Variables, Environment Variables, Shell Scripts, Read: Making Scripts Interactive, Using Command
Line Arguments, Exit and Exit status of Command, The Logical Operators and Conditional Execution,
Condition Control Structures & Looping Control Structure, Handling Using the expr command, Looping
Using the while Loop, Looping with a List Using the for Loop, Manipulating the Positional Parameters
using set and shift Command, Interrupting a Program Using the trap Command, Debugging Shell
Scripts with the set –x Command
Overview
In this unit, you will learn about Shell Variables, Environment Variables, Shell Scripts, Read: Making
Scripts Interactive, Using Command Line Arguments as well as Exit and Exit status of Command. Also,
the unit describes the Logical Operators and Conditional Execution, Condition Control Structures &
Looping Control Structure, Handling Using the expr command, Looping Using the while Loop and
Looping with a List Using the for Loop. Further, it covers the concept of manipulating the Positional
Parameters Using set and shift Command and interrupting a Program Using the trap Command.
Towards the end, you will read about debugging Shell Scripts with the set –x Command.
Learning Objectives
Learning Outcomes
[Link]
12.1 INTRODUCTION
A shell program, often known as a shell script, is a program made up entirely of shell commands. Every
time a shell program is executed, it is interpreted. This means that the shell processes (i.e., executes)
each command one line at a time. This differs from languages such as C or C++, which are completely
converted into binary images by compiler software. A shell program might be basic, including only a
few shell commands, or complicated, containing thousands2 of shell instructions. The programmer is in
charge of the shell program’s complexity. In general, a shell programmer may be described as follows:
Shell programmes, such as any other file, have permission modes and must have the right
permissions configured to run. As other programming languages, the shell language supports input
and output, repetition, logical decision making, file creation and deletion and system calls.
Shell programmes can be written in any format as long as each shell command’s syntax is valid.
This allows for the usage of blank lines, indentation and a lot of white spaces.
An interface to the Unix system is provided via a Shell. It takes your input and uses it to run applications.
It shows the result of a programme once it has completed its execution. The shell is a command,
programme and shell script execution environment. Shells come in a variety of flavours, similar to
how operating systems come in a variety of flavours. Each shell type has its own set of commands and
functionalities that are well-known.
2
UNIT 12: Essential Shell Programming JGI JAIN
DEEMED-TO-BE UN IV E RSI TY
3
JGI JAIN
DEEMED-TO-BE UN IV E RSI TY
Operating System and Unix Shell Programming
Global
A globally scoped ENV that is defined in a terminal can be accessed from any location inside that
terminal’s environment. That implies it may be used in any scripts, programmes, or processes that
execute in the terminal’s environment.
4
UNIT 12: Essential Shell Programming JGI JAIN
DEEMED-TO-BE UN IV E RSI TY
Local
Any application or process executing in the terminal cannot access a locally scoped ENV established in
the terminal. It can only be accessed from the terminal where it was defined.
The syntax to access the ENVs is:
How to access ENVs?
$NAME
Note that the same method is used to access both local and global environment variables.
The syntax to display the ENVs are as follows:
$ echo $NAME
The syntax to display all the Linux ENVs
$ printenv //displays all the global ENVs
or
$ set //display all the ENVs (global as well as local)
or
$ env //display all the global ENVs
5
JGI JAIN
DEEMED-TO-BE UN IV E RSI TY
Operating System and Unix Shell Programming
12.5.1 Bash
The up and down arrow keys will scroll through the history of previous commands in bash; the up and
down arrow keys will scroll through the history of previous commands in bash. Ctrl+r, on the other hand,
does a reverse search, matching any portion of the command line. When you press ESC, the selected
command will be put into the current shell, ready for you to edit. If you wish to run the command again
and know what characters it started with do it as follow:
bash$ ls /tmp
(list of files in /tmp)
bash$ touch /tmp/foo
bash$ !l
ls /tmp
(list of files in /tmp, now including /tmp/foo)
PageUp and PageDn, in addition to the arrow keys, can be used to travel to the beginning and end of the
command line.
12.5.2 ksh
You may improve the usability of ksh by adding history commands in either vi or emacs mode. Depending
on the specific conditions, there are a variety of options. exec ksh -o vi, set -o vi, or ksh -o vi (where “vi”
could be replaced by “emacs” if you prefer emacs mode).
If you wish to start a ksh session from another interactive shell, then simply type ksh in the command
prompt:
csh% # oh no, it's csh!
csh% ksh
ksh$ # phew, that's better
ksh$ # do some stuff under ksh
ksh$ # then leave it back at the csh prompt:
ksh$ exit
csh%
This will launch a new ksh session, from which you can quit and return to your prior shell. You could also
use the exec command to replace the csh (or whatever shell) with a ksh shell:
csh% # oh no, it's csh!
csh% exec ksh
6
UNIT 12: Essential Shell Programming JGI JAIN
DEEMED-TO-BE UN IV E RSI TY
login:
The only difference is that you do not receive your csh session back this time.
The history is the good stuff:
csh% ksh
ksh$ set -o vi
ksh$
# You can now edit the history with vi-like commands,
# and use ESC-k to access the history.
If you press ESC then k, you may scroll backwards through the command history by continuously
pressing k. To alter the commands, use vi command-mode and entry-mode commands, such as this:
ksh$ touch foo
ESC-k (enter vi mode, brings up the previous command)
w (skip to the next word, to go from "touch" to "foo"
cw (change word) bar (change "foo" to "bar")
ksh$ touch bar
Command arg1 arg2 arg3 arg4 arg5 arg6 arg7 arg8 arg9 arg10.....
$0 $1 $2 $3 $4 $5 $6 $7 $8 $9 ${10}
$# $*
Variable Description
%0 Represents the command or script.
$1 to $9 Represents arguments 1 through 9.
${10} and so on Represents arguments 10 and further.
$# Represents the total number of arguments.
$* Represent all arguments.
$$ Represents the PID of a running script.
7
JGI JAIN DEEMED-TO-BE UN IV E RSI TY
Operating System and Unix Shell Programming
The possible end results of testing multiple Boolean expressions with logical operators are listed in
Table 2:
Table 2: Listing the Possible End Results When Using Logical Operators
Result of First Logical Operator Result of Second Boolean End Boolean Result
Boolean Expression Expression
True && True True
True && False False
False && True False
False && False False
True || True True
True || False True
False || True True
False || False False
8
UNIT 12: Essential Shell Programming JGI JAIN
DEEMED-TO-BE UN IV E RSI TY
9
JGI JAIN
DEEMED-TO-BE UN IV E RSI TY
Operating System and Unix Shell Programming
Figure 3: Showing the Usage of the if-then-else-fi Control Structure if a Shell Script Itef
10
UNIT 12: Essential Shell Programming JGI JAIN
DEEMED-TO-BE UN IV E RSI TY
In the example shown Figure 3, the test condition evaluates to true. Therefore, the control flows to
then, and therefore, the statements following then are executed. Only one of the test conditions needs
to evaluate to true for the control to move to the then clause because of the OR logical operator used
between multiple test conditions. The test condition evaluates to true, therefore, the statements in the
else clause are not executed.
This control structure is the most advanced of the if-fi control structures. It evaluates the first test
condition and if the result is false, then the control moves to the next condition, and so on. If all the
conditions specified are false then the statements in the else clause are executed if it exists. If the result
of any of the test conditions evaluates to true, the rest of the conditions and the else clause are skipped.
The syntax of this control structure is as follows:
if test bool_expr1
then
statements1
elif test bool_expr2
then
statements2
elif test bool_expr3
then
statements3
else
statements_else
fi
where,
if is the keyword to start the if control structure
test is the keyword to test a Boolean expression
bool_expr1 is the first Boolean expression
statements1 are the statements that are executed if bool_expr1 evaluates to true
elif is the keyword to specify another Boolean expression that needs to be tested if the previous
evaluates to false
bool_expr2 is the second Boolean expression that is evaluated if bool_expr1 evaluates to false
staements2 are executed if bool_expr2 evaluates to true
else is the keyword to specify the statements to be executed if all the previous Boolean expressions
evaluate to false
statements_else are executed if the control flows to else
fi is the keyword to notify the interpreter of the end of the if control structure
11
JGI JAIN
DEEMED-TO-BE UN IV E RSI TY
Operating System and Unix Shell Programming
The usage of this control structure in the shell script itetef is shown Figure 4:
Figure 4: Showing the Usage of Multiple test Conditions in One if-elif-else-fi Control Structure
In the example shown in Figure 4, multiple test conditions are included in the shell script itetef. In this
script, at most, one of the conditions can evaluate to true. Only the statements written in its then clause
will be executed after which the control flows to fi, i.e., the end of the if structure. Let us now study the
case-esac control structure, which is another type of conditional control structure to test multiple test
conditions.
12
UNIT 12: Essential Shell Programming JGI JAIN
DEEMED-TO-BE UN IV E RSI TY
statement4;;
………
………
valueN) statemenN;;
*) statement_else;;
esac
where,
case is the keyword that notifies the interpreter of the start of a case control structure
var is the variable that will be compared to multiple values
value1, value2, valueN, etc. are the value that will be compared to the value held in var variable
) (right parantheses) are interpreted as the end of the test criteria and the beginning of the statements
that need to be executed if the value matches to the var variable
* signifies the statements that will be executed if none of the values mentioned match with the value
held in var variable; this is similar to the else in if-then-fi control structure
; marks the end of every statement
;; marks the end of every group of statements in a test condition
esac is interpreted as the end of the case control structure
The example of case-esac control structure and its usage to create the same program as illustrated in
Figure 4 in the shell script casedemo is shown in Figure 5:
13
JGI JAINDEEMED-TO-BE UN IV E RSI TY
Operating System and Unix Shell Programming
until loop
for loop
The flow of control starts from the while keyword to the test condition. If the condition evaluates to
true, the statements enclosed in the do and done keywords are executed. The control flows from done
keyword back to the test condition which is again checked. Depending upon the result of the Boolean
expression, the execution of the statements in do-done are executed. The cycle continues for as long as
the Boolean expression evaluates to true.
14
UNIT 12: Essential Shell Programming JGI JAIN
DEEMED-TO-BE UN IV E RSI TY
The usage of the while loop in the shell script whiledemo is shown in Figure 7:
Figure 7: Showing the Usage of while Loop in the Shell Script whiledemo
Figure 8: Showing the Usage of the until Loop in the Shell Script untildemo
In the while and until loop, it takes a separate line in the shell script to mention each of these values. In
the for loop, all these values can be declared in one line, which makes the code more manageable.
The syntax for the for loop is as follows:
for var in {initial_value..termination_value..increment}
do
statement1
statement2
……….
statementN
done
where,
for is the keyword to start the for loop
var is the variable that will be incremented or decremented to meet the termination value
16
UNIT 12: Essential Shell Programming JGI JAIN
DEEMED-TO-BE UN IV E RSI TY
{ } (curly brackets) contain the values for instantiation, termination, and increment or decrement
initial_value is the starting value of the var variable
termination_value is the value that when reached by the var variable, the loop is terminated
increment is the value with which the var variable is incremented or decremented
.. (double dots) separate the initial value, termination value, and the increment
do-done enclose the statements that will be executed for as long as the var does not equal the
termination_value
The usage of the for loop in the shell script fordemo to print a string entered by the user five times is
shown in Figure 9:
Figure 9: Showing the Usage of for Loop in the Shell Script fordemo
In the above example, the variable a is not declared explicitly but the value it is assigned is 1. The
termination value and the test condition was not required separately and was mentioned in the for loop
itself as 10. The increment was also not required to be done by the programmer separately. In the for
loop itself, the increment was mentioned as 2. This value is added to the variable a every time the loop
is executed.
17
JGI JAIN
DEEMED-TO-BE UN IV E RSI TY
Operating System and Unix Shell Programming
Here’s an example of a while loop that is nested. In a similar approach, the additional loops might be
layered dependent on the programming need. The syntax of nested loop is as follows:
while command1 ; # this is loop1, the outer loop
do
Statement(s) to be executed if command1 is true
while command2 ; # this is loop2, the inner loop
do
Statement(s) to be executed if command2 is true
done
Statement(s) to be executed if command1 is true
done
The following script shows the concept of nested loop:
#!/bin/sh
a=0
while [ "$a" -lt 10 ] # this is loop1
do
b="$a"
while [ "$b" -ge 0 ] # this is loop2
do
echo -n "$b "
b='expr $b - 1'
done
echo
a='expr $a + 1'
done
As a consequence, you will get the following outcome. It is crucial to understand how echo -n works in
this context. The -n option prevents echo from printing a newline character in this case. The output of
the given script is as follows:
0
1 0
2 1 0
3 2 1 0
4 3 2 1 0
5 4 3 2 1 0
6 5 4 3 2 1 0
7 6 5 4 3 2 1 0
8 7 6 5 4 3 2 1 0
9 8 7 6 5 4 3 2 1 0
18
UNIT 12: Essential Shell Programming JGI JAIN
DEEMED-TO-BE UN IV E RSI TY
19
JGI JAIN
DEEMED-TO-BE UN IV E RSI TY
Operating System and Unix Shell Programming
20
UNIT 12: Essential Shell Programming JGI JAIN
DEEMED-TO-BE UN IV E RSI TY
Iteration no 4
Iteration no 6
Iteration no 7
Iteration no 8
Iteration no 9
Iteration no 10
12.13 MANIPULATING THE POSITIONAL PARAMETERS USING SET AND SHIFT COMMAND
Positional parameters are automatically created like the variable REPLY, and their values cannot be
modified. These variables are therefore called automatic variables or read-only variables. Passing
arguments to a shell script when executing them are automatically stored in positional parameters.
The values delimited by a null character are considered separate values and are stored in different
variables.
The first value is stored in the variable 1, the second in variable 2, third in 3, and so on. The name of the
file acts as an argument to sh command and its name is stored in the variable 0. Similarly, the variable
* stores all the values entered by the user. The variable # stores the number of positional parameters
created to save the values entered by the user. As explained in previous chapters, the values held
inthese variables can be accessed using a $ followed by the variable name.
The usage of positional parameters and variables * and # in the shell script posparam and its execution
is shown in Figure 12:
21
JGI JAIN
DEEMED-TO-BE UN IV E RSI TY
Operating System and Unix Shell Programming
The implementation of the above example in the shell script posparam1 and its execution is shown in
Figure 13:
Figure 13: Showing the Usage of the Shift Command in the Script posparam1
The set command sets the value passed to it as an argument in the positional parameters. These
values held in the positional parameters can then be referred to using their variable names. The set
command can also be used to set the output of any command in the positional parameters by enclosing
the command in backticks and passing it as an argument to the set command. The usage of the set
command in a shell script is shown in Figure 14:
Figure 14: Showing the Usage of the Set Command and Positional Parameters
22
UNIT 12: Essential Shell Programming JGI JAIN
DEEMED-TO-BE UN IV E RSI TY
If you just omit the first parameter after changing the default action to be done on receipt of a signal,
then you may modify it back with the trap.
$ trap 1 2
This returns the default action to be executed when signals 1 or 2 are received.
23
JGI JAINDEEMED-TO-BE UN IV E RSI TY
Operating System and Unix Shell Programming
A shell program, often known as a shell script, is a program made up entirely of shell commands.
Every time a shell program is executed, it is interpreted
Shell programmes can be written in any format as long as each shell command’s syntax is valid.
This allows for the usage of blank lines, indentation and a lot of whitespaces.
An interface to the Unix system is provided via a Shell. It takes your input and uses it to run
applications. It shows the result of a programme once it has completed its execution.
A variable is a character string that has a value attached to it. A number, text, filename, device, or
any other sort of data might be assigned as the value.
The read-only command in Shell can be used to mark variables as read only.
When you unset or delete a variable, shell removes it from the list of variables it keeps track of.
A local variable is a variable that exists just in the current shell instance.
Variables in the environment any shell child process has access to an environment variable. Some
applications require environment variables to work correctly.
A shell variable is a specific variable that the shell creates and that the shell needs to function
properly.
A globally scoped ENV that is defined in a terminal can be accessed from any location inside that
terminal’s environment.
A shell script is a computer programme that runs through the Unix/Linux.
A shell is a command-line interpreter, and shell scripts commonly execute file manipulation,
programme execution and text output.
The arguments supplied at the command prompt with a command or script to be run are known as
command line arguments (also known as positional parameters).
Testing a Boolean expression results in a value, this is either True or False. In some cases, when
you need to test multiple Boolean expressions, you can use logical operators to combine multiple
Boolean expressions.
Condition based control structure refers to the sequential flow of control in a program, where, the
execution of statements depends upon a condition.
The if-fi control structure executes statements depending upon the result of the test condition.
In most cases, the case-esac conditional control structure can be used to make such a program very
short and still perform the same functions.
Iteration based control structures are those that involve repetition of execution of statements
depending upon the result of a Boolean condition that is checked repetitively.
24
UNIT 12: Essential Shell Programming JGI JAIN
DEEMED-TO-BE UN IV E RSI TY
Nesting is supported by all loops, which means you may put one loop within another similar or
different loop.
In Unix, the expr command evaluates a provided expression and shows the result.
Positional parameters are automatically created like the variable REPLY, and their values cannot
be modified.
The shift command shifts the values stored in the positional parameters deleting the first.
The set command sets the value passed to it as an argument in the positional parameters. These
values held in the positional parameters can then be referred to using their variable names.
12.18 GLOSSARY
Shell script: It is a program made up entirely of shell commands. Every time a shell program is
executed, it is interpreted.
Variable: It is a character string that has a value attached to it. A number, text, filename, device, or
any other sort of data might be assigned as the value.
Shell variable: A specific variable that the shell creates and that the shell needs to function properly.
Read-only variables: The read-only command in Shell can be used to mark variables as read-only.
A variable’s value cannot be modified after it has been designated as read-only.
Resetting traps: If you just omit the first parameter after changing the default action to be done on
receipt of a signal, you may modify it back with the trap.
List of signal: There is a simple technique to compile a list of all the signals that your system supports.
Simply use the kill -l command to get a list of all available signals.
Until statement: The till loop is run until the condition/command evaluates to false again. When the
condition/command becomes true, the loop ends.
[Link]
[Link]
Discuss with your friends the essential shell programming and their real-time scenario where they
are used.
26