FALLSEM2019-20 CSE2005 ELA VL2019201002569 Reference Material I 25-Jul-2019 Shell Programming
FALLSEM2019-20 CSE2005 ELA VL2019201002569 Reference Material I 25-Jul-2019 Shell Programming
Shell script
• A shell script is a computer program designed to be run by
the Unix/Linux shell which could be one of the following:
The Bourne Shell
The C Shell
The Korn Shell
The GNU Bourne-Again Shell
• A shell is a command-line interpreter and typical
operations performed by shell scripts include file
manipulation, program execution, and printing text.
Comment
• A good shell script will have comments, preceded
by # sign
1. _ALI 1. 2_VAR
2. TOKEN_A 2. -VARIABLE
3. VAR_1 3. VAR1-VAR2
4. VAR_2 4. VAR_A!
Defining Variables & Accessing Values
• Defining Variables Syntax
variable_name=variable_value
VAR1=“VIT University"
VAR2=100
• Accessing Values
To access the value stored in a variable, prefix its name with the
dollar sign ($)
unset NAME
Shell Arrays
• Shell supports a different type of variable called an array variable. This can
hold multiple values at the same time.
array_name[index]=value
NAME[0]=“Mango"
NAME[1]=“Orange"
NAME[2]=“Grape"
NAME[3]="Apple"
NAME[4]=“Banana"
echo "First Index: ${NAME[0]}"
echo "Second Index: ${NAME[1]}“
echo {array_name[*]} # Print all items of the array
Echo {array_name[@]} # Print all items of the array
Basic Expression
• Bourne shell didn't originally have any
mechanism to perform simple arithmetic
operations but it uses external programs,
either awk or expr.
val=`expr 2 + 2`
echo "Total value : $val"
Arithmetic Operators
Operator Description Example
+ (Addition) Adds values on either side of the `expr $a + $b` will
operator give 30
- (Subtraction) Subtracts right hand operand from left `expr $a - $b` will give
hand operand -10
* (Multiplication) Multiplies values on either side of the `expr $a \* $b` will
operator give 200
/ (Division) Divides left hand operand by right hand `expr $b / $a` will give
operand 2
% (Modulus) Divides left hand operand by right hand `expr $b % $a` will
operand and returns remainder give 0
= (Assignment) a = $b would assign
Assigns right operand in left operand value of b into a
== (Equality) Compares two numbers, if both are same [ $a == $b ] would
then returns true. return false.
!= (Not Equality) Compares two numbers, if both are [ $a != $b ] would
different then returns true. return true.
Relational Operators
Operator Description Example
-eq Checks if the value of two operands are equal or [ $a -eq $b ] is not
not; if yes, then the condition becomes true. true.
if [ $a == $b ] if [ $a == $b ]
then
then
echo "a is equal to b"
echo "a is equal to b" else
fi echo "a is not equal to b"
fi
Decision Making
• if...elif elif [ $a -lt $b ]
...else...fi statement then
a=10 echo "a is less than b"
b=20 else
echo "None of the
if [ $a == $b ] condition met"
then fi
echo "a is equal to b"
elif [ $a -gt $b ]
then
echo "a is greater than b"
case...esac Statement
case word in FRUIT="kiwi"
pattern1)
Statement(s) to be executed if
pattern1 matches case "$FRUIT" in
;; "apple") echo "Apple pie is
pattern2) quite tasty."
Statement(s) to be executed if ;;
pattern2 matches
;; "banana") echo "I like
pattern3) banana nut bread."
Statement(s) to be executed if ;;
pattern3 matches "kiwi") echo "New Zealand
;; is famous for kiwi."
*)
Default condition to be executed ;;
;; esac
esac
Loop Types
• The while loop
• The for loop
• The until loop
• The select loop
while Loop
Syntax a=0
while [ $a -lt 10 ]
do do
Statement(s) to be echo $a
executed if command is a=`expr $a + 1`
true done
done
for Loop
Syntax for var in 0 1 2 3 4 5 6 7 8 9
do
for var in word1 word2 ... echo $var
wordN done
do
Statement(s) to be
executed for every word.
done
until Loop
• Syntax a=0
until [ ! $a -lt 10 ]
until command do
do echo $a
Statement(s) to be a=`expr $a + 1`
executed until command done
is true
done
Loop Control
• Sometimes you need to stop a loop or skip
iterations of the loop.
• Two statements that are used to control shell
loops−
• The break statement
• The continue statement
Shell Substitution
1 \\ 6 \n
backslash new line • The following
2 \a 7 \r escape sequences
alert (BEL) carriage which can be used
return in echo command
3 \b 8 \t
backspace horizonta
l tab
4 \c 9 \v
suppress vertical
trailing tab
newline
5 \f
form feed
Special Variables
Sr.No Variable & Description
.
1 $0
The filename of the current script.
2 $n
These variables correspond to the arguments with which a
script was invoked. Here n is a positive decimal number
corresponding to the position of an argument (the first
argument is $1, the second argument is $2, and so on).
3 $#
The number of arguments supplied to a script.
4 $*
All the arguments are double quoted. If a script receives two
arguments, $* is equivalent to $1 $2.
Special Variables
5 $@
All the arguments are individually double quoted. If a script
receives two arguments, $@ is equivalent to $1 $2.
6 $?
The exit status of the last command executed.
7 $$
The process number of the current shell. For shell scripts,
this is the process ID under which they are executing.
8 $!
The process number of the last background command.
Example1
# special.sh $ ./special.sh arg1 arg2 arg3
Process ID of shell = 20779
Program name = ./special.sh
echo "Process ID of shell = $$"
Number of args = 3
echo "Program name = $0" Argument 1 = arg1
echo "Number of args = $#" Argument 2 = arg2
echo "Argument 1 = $1" Complete list of arguments = arg1
echo "Argument 2 = $2" arg2 arg3
Complete list of arguments = arg1
echo "Complete list of arg2 arg3
arguments = $*"
echo "Complete list of $ echo $?
arguments = $@" 0
Example2
for args in $* $ ./unknown_args.sh 1 2 3 4
do 1
echo $args 2
done 3
4