Shell Programming
Shell Programming
Shell Programming
The shell provides you with an interface to the UNIX system. It gathers input from you
and executes programs based on that input. When a program finishes executing, it displays that
program's output. A shell is an environment in which we can run our commands, programs, and
shell scripts.
Shell Prompt
The prompt, $, which is called command prompt, is issued by the shell. While the prompt
is displayed, you can type a command. The shell reads your input after you press Enter.
A simple example of date command which displays current date and time:
$date
Shell Scripts
A set of commands that can be grouped together under a single filename and can be
executed repeatedly is called as shell scripts or shell programs. Naming a shell program has no
restriction, but the filename should end with an extension .sh .
Shell Types
The Bourne shell. If you are using a Bourne-type shell, the default prompt is the $
character.
The C shell. If you are using a C-type shell, the default prompt is the % character.
There are again various subcategories for Bourne Shell which are listed as follows:
C shell ( csh)
TENEX/TOPS C shell ( tcsh)
The original UNIX shell was written in the mid-1970s by Stephen R. Bourne while he
was at AT&T Bell Labs in New Jersey.
The Bourne shell was the first shell to appear on UNIX systems, thus it is referred to as
"the shell".
Shell Keywords
Keywords also referred, as reserved words are the words whose meaning has been
already defined in the shell. Keywords cannot be used, as shell variables. All keywords must be
written in lowercase. Some keywords are
Shell Comments
Comments in shell script start with #. It can be placed anywhere in a line. Shell ignores
all characters placed on its right in that line
Shell Variables
A variable is a character string to which we assign a value. The value assigned could be a
number, text, filename, device, or any other type of data.
Variable Names
Variable name is an arbitrary name used to refer to the area of memory in which a
particular value is stored.
The first character in the variable name must be an alphabet or an underscore, and can be
followed by any number of alphabets, or digits or underscores.
_ALI
TOKEN_A
VAR_1
VAR_2
2_VAR
-VARIABLE
VAR1-VAR2
VAR_A!
The reason you cannot use other characters such as!,*, or - is that these characters have a special
meaning for the shell.
Defining Variables
For example:
NAME="Nithya"
Above example defines the variable NAME and assigns it the value "Nithya". Variables of this
type are called scalar variables. A scalar variable can hold only one value at a time.
Accessing Values
To access the value stored in a variable, prefix its name with the dollar sign ( $).
For example, following script would access the value of defined variable NAME and
would print it on STDOUT:
NAME="Nithya"
echo $NAME
Nithya
Read-only Variables
The shell provides a way to mark variables as read-only by using the readonly command.
After a variable is marked read-only, its value cannot be changed.
For example, following script would give error while trying to change the value of NAME:
NAME="Nithya"
readonly NAME
NAME="Sri"
Positional Parameters
The parameter that represents their position in the command line is known as positional
parameters.
The command-line arguments $1, $2, $3,...$9 are positional parameters, with $0 pointing
to the actual command, program, shell script, or function and $1, $2, $3, ...$9 as the arguments to
the command.
Special Variables
The following table shows a number of special variables that you can use in your shell scripts
Variable Description
$0 The filename of the current script.
These variables correspond to the arguments with which a script was
invoked. Here n is a positive decimal number corresponding to the
$n
position of an argument (the first argument is $1, the second argument
is $2, and so on).
$# The number of arguments supplied to a script.
The process number of the current shell. For shell scripts, this is the
$$
process ID under which they are executing.
Arithmetic Operators
The expr command evaluates its arguments as arithmetic expressions and displays the result on
the standard output.
Example:
a=10
b=20
val=`expr $a + $b`
echo "a + b : $val"
val=`expr $a - $b`
echo "a - b : $val"
val=`expr $a \* $b`
echo "a * b : $val"
val=`expr $b / $a`
echo "b / a : $val"
val=`expr $b % $a`
echo "b % a : $val"
if [ $a == $b ]
then
echo "a is equal to b"
fi
if [ $a != $b ]
then
echo "a is not equal to b"
fi
a + b : 30
a - b : -10
a * b : 200
b/a:2
b%a:0
a is not equal to b
Relational Operators
About Quotes
` Back quote
`Back quote` - To execute command
Example:
a=10
b=20
if [ $a -eq $b ]
then
echo "$a -eq $b : a is equal to b"
else
echo "$a -eq $b: a is not equal to b"
fi
if [ $a -ne $b ]
then
echo "$a -ne $b: a is not equal to b"
else
echo "$a -ne $b : a is equal to b"
fi
if [ $a -gt $b ]
then
echo "$a -gt $b: a is greater than b"
else
echo "$a -gt $b: a is not greater than b"
fi
if [ $a -lt $b ]
then
echo "$a -lt $b: a is less than b"
else
echo "$a -lt $b: a is not less than b"
fi
if [ $a -ge $b ]
then
echo "$a -ge $b: a is greater or equal to b"
else
echo "$a -ge $b: a is not greater or equal to b"
fi
if [ $a -le $b ]
then
echo "$a -le $b: a is less or equal to b"
else
echo "$a -le $b: a is not less or equal to b"
fi
Boolean Operators
Example:
a=10
b=20
if [ $a != $b ]
then
echo "$a != $b : a is not equal to b"
else
echo "$a != $b: a is equal to b"
fi
fi
10 != 20 : a is not equal to b
10 -lt 100 -a 20 -gt 15 : returns true
10 -lt 100 -o 20 -gt 100 : returns true
10 -lt 5 -o 20 -gt 100 : returns false
Decision Making
if Statement
if– else Statement
if-elif-else-if Statement
case – esac Statement
if Statement
The if...fi statement is the control statement that allows to make decisions and execute
statements conditionally.
General Format
if [ expression ]
then
Command
fi
If the resulting value is true, given statement(s) are executed. If expression is false then no
statement would be not executed.
Example:
a=10
b=20
if [ $a == $b ]
then
echo "a is equal to b"
fi
if [ $a != $b ]
then
echo "a is not equal to b"
fi
a is not equal to b
if – else Statement
The if...else...fi statement is the next form of control statement that allows to execute
statements in more controlled way and making decision between two choices.
General Format
if [ expression ]
then
Command
else
Command
fi
Example:
a=10
b=20
if [ $a == $b ]
then
echo "a is equal to b"
else
echo "a is not equal to b"
fi
a is not equal to b
if-elif-else-if Statement
The if...elif…else...fi statement is the next form of control statement that allows to make
correct decision out of several conditions
General Format
if [ expression 1]
then
Command
elif [ expression 2]
Command
elif [ expression 3]
Command
else
Command
fi
Here statement(s) are executed based on the true condition, if non of the condition is true
then else block is executed.
Example:
a=10
b=20
if [ $a == $b ]
then
echo "a is equal to b"
elif [ $a -gt $b ]
then
echo "a is greater than b"
elif [ $a -lt $b ]
then
echo "a is less than b"
else
echo "None of the condition met"
fi
This will produce following result:
a is less than b
General Format
case expression in
pattern1 )
statements
;;
pattern 2 )
Statements
;;
patternN )
*)
statements
;;
esac
Here the expression is compared against every pattern until a match is found. The statement(s)
following the matching pattern executes. The pattern labeled * is executed if none of the other
patterns are satisfied.
Example:
FRUIT="kiwi"
case "$FRUIT" in
"apple") echo "Apple pie is quite tasty."
;;
"banana") echo "I like banana nut bread."
;;
"kiwi") echo "New Zealand is famous for kiwi."
;;
esac
This will produce following result:
Looping Statements
Types of looping
For loop
While Loop
Until loop
General Format:
for var in num1 num2…..num N
do
commands
done
Here var is the name of a variable and num1 to num N are sequences of numbers separated by
spaces. Each time the for loop executes, the value of the variable var is set to the next number in
the list of numbers, num1 to num N.
Example:
for var in 0 1 2 3 4 5 6 7 8 9
do
echo $var
done
The while loop enables to execute a set of commands repeatedly until some condition
occurs to false.
General format
while condition
do
commands
done
If the resulting value is true, given statement(s) are executed. If command is false then no
statement would be not executed and program would jump to the next line after done statement.
Example:
a=0
while [ $a -lt 10 ]
do
echo $a
a=`expr $a + 1`
done
0
1
2
3
4
5
6
7
8
9
Until Loop Statement:
The until loop enables to execute a set of commands until a condition is true.
General Format
until condition
do
commands
done
If the resulting value is false, given statement(s) are executed. If command is true then no
statement would be not executed and program would jump to the next line after done statement.
Example
a=0
until [ $a -lt 10 ]
do
echo $a
a=`expr $a + 1`
done
0
1
2
3
4
5
6
7
8
9