0% found this document useful (0 votes)
95 views27 pages

FALLSEM2019-20 CSE2005 ELA VL2019201002569 Reference Material I 25-Jul-2019 Shell Programming

The document discusses Linux shell programming and various shell scripting concepts like shell scripts, variables, arrays, expressions, decision making, loops, and special characters. A shell script is a program designed to be run by the shell/command line interpreter. It can perform tasks like file manipulation, executing other programs, and printing text. Comments, variables, arrays, conditional statements, loops, and other programming constructs allow shell scripts to perform complex automation tasks.

Uploaded by

nidhi patel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
95 views27 pages

FALLSEM2019-20 CSE2005 ELA VL2019201002569 Reference Material I 25-Jul-2019 Shell Programming

The document discusses Linux shell programming and various shell scripting concepts like shell scripts, variables, arrays, expressions, decision making, loops, and special characters. A shell script is a program designed to be run by the shell/command line interpreter. It can perform tasks like file manipulation, executing other programs, and printing text. Comments, variables, arrays, conditional statements, loops, and other programming constructs allow shell scripts to perform complex automation tasks.

Uploaded by

nidhi patel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 27

Linux 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

# Author : Gladys Gnana Kiruba


# The program is about to find the greatest of 2 numbers
First program
• $ vi first.sh
pwd
ls
• Save the above content and make the script
executable −
• $chmod +x first.sh
• The shell script is now ready to be executed −
• $./first.sh (or) $sh first.ah
read command
• the read command which takes the input from
the keyboard and assigns it as the value of the
variable NAME and finally prints it on STDOUT

echo "What is your name?"


read NAME
echo "Hello, $NAME"
Variable Names
• The name of a variable can contain only letters (a to z
or A to Z), numbers ( 0 to 9) or the underscore
character ( _).
• By convention, Unix shell variables will have their
names in UPPERCASE.

• The following examples are • Following are the examples


valid variable names − of invalid variable names −

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 ($)

NAME=" VIT University "


echo $NAME
Deleting a variable
• Unsetting or deleting a variable directs the shell
to remove the variable from the list of variables
that it tracks. Once you unset a variable, you
cannot access the stored value in the variable.
• Syntax
unset variable_name

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.

-ne Checks if the value of two operands are equal or


not; if values are not equal, then the condition [ $a -ne $b ] is true.
becomes true.
-gt Checks if the value of left operand is greater than
the value of right operand; if yes, then the [ $a -gt $b ] is not true.
condition becomes true.
-lt Checks if the value of left operand is less than the
value of right operand; if yes, then the condition [ $a -lt $b ] is true.
becomes true.
-ge Checks if the value of left operand is greater than
or equal to the value of right operand; if yes, [ $a -ge $b ] is not
then the condition becomes true. true.

-le Checks if the value of left operand is less than or


equal to the value of right operand; if yes, then [ $a -le $b ] is true.
the condition becomes true.
Boolean Operators
Operator Description Example
! This is logical negation. This
inverts a true condition into false [ ! false ] is
and vice versa. true.

-o This is logical OR. If one of the [ $a -lt 20 -o $b


operands is true, then the -gt 100 ] is true.
condition becomes true.

-a This is logical AND. If both the [ $a -lt 20 -a $b


operands are true, then the -gt 100 ] is
condition becomes true false.
otherwise false.
String Operators
Operator Description Example
= Checks if the value of two operands are [ $a = $b ] is not true.
equal or not; if yes, then the condition
becomes true.

!= Checks if the value of two operands are [ $a != $b ] is true.


equal or not; if values are not equal
then the condition becomes true.

-z Checks if the given string operand size is [ -z $a ] is not true.


zero; if it is zero length, then it returns
true.
-n Checks if the given string operand size is [ -n $a ] is not false.
non-zero; if it is nonzero length, then it
returns true.
str Checks if str is not the empty string; if it [ $a ] is not false.
is empty, then it returns false.
• if...fi statement • if...else...fi statement
a=10 a=10
b=20 b=20

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

You might also like