Shell Programming
Introduction
● Shell Command Line Interpreter (CLI)
● A program that runs another program.
● Used to run different commands, tools, utilities.
● We can use these commands along with some programming
construct to make a shell script.
● Shell scripts are interpreted and executed by the OS shell itself
● Benefits of combining system commands with programming
logic.
● Used mainly in automating system maintenance activities.
<date/time> <footer> 2
Shells
● Sh – Bourne Shell
● Bash – Bourne Again Shell
− Default shell where you land after login.
− We will do shell scripting here.
● Csh - C Shell
− Syntax are more like C program.
● Ksh – Korn Shell
− Derived from and backward compatible with Bourne shell
● Tcsh
− Derived from and backward compatible with C shell.
<date/time> <footer> 3
Variable
● echo – to display something No Space
● Declaring a variable: <variable-name>=<value>
− Space separated values should be within single/double quotes.
− Commands can be assigned in a variable.
− <value> is always string
● To get the value of a variable: $<variable-name>
● Difference between echo name and echo $name.
● unset – to remove a variable
● Multiple variable assignment using ;
<date/time> <footer> 4
Concatenation
● Concatenating two variables: $<variable1>$<variable2>
− $fname$sname
● Concatenation with assignment: <var-1>=$<var-2>$<var-3>
− name=$fname$sname
● Concatenation with other variables and strings
− echo $fname Kumar $sname
● Assign the above concatenation into a variable.
− name=$fname” Kumar “$sname
<date/time> <footer> 5
User Input
● read – to take user input in a variable
− read <variable-name>
− Allows space separated value
− Reads everything as a string
<date/time> <footer> 6
Evaluating Expressions
● Let, x=3; y=5
● What will be the output of the following expressions?
Now try these -
−
●
echo x+y
● expr $x+$y (both side without space)
− echo $x+$y ● expr $x+ $y (No space before +)
● expr $x +$y (No space after +)
− echo 3+5
● expr – to evaluate expression ● Space is required both side of the
operator
− expr 3 + 5 ● Now try this -
● echo expr 3 + 5
− expr $x + $y ● echo expr $x + $y
<date/time> <footer> 7
Evaluating Expressions
● Try to find out multiplication of x and y
− Multiplication should be escaped with a \
− expr $x \* $y
● Try to assign the evaluated value of an expression in a variable.
− z=`expr $x + $y`
− This is backquote; not single quote.
● expr is also used for string manipulation using regular
expression.
<date/time> <footer> 8
String Manipulation
● To evaluate a regular expression : expr <string> : <regex>
− To locate first occurrence of a character in a string
− expr <string> : [^<character>]*<character>
− expr “hello world” : [^o]*o
● To locate the first occurrence of a character in a string
− expr index <string> <character>
● To find the length of a string : expr length <string>
− expr <string> : ‘.*’
− Determine the count of single characters in the <string>.
<date/time> <footer> 9
String Manipulation
● To get a sub-string of a string :
− expr substr <string> <start position> <number of characters>
− expr substr “Hello World” 1 4
● Hell
<date/time> <footer> 10
Shell Program
● Writing such commands one by one to create a program.
● # denotes a single line comment
● #! /bin/bash
− Loads bash to run the script
● Hello world script
● Write a script to get the first name and last name from user and
show the full name.
● Write a script to get two numbers and provide sum of them.
<date/time> <footer> 11
Command Line Arguments
● Arguments that are passed along with the command.
● $# : Gives the number of arguments.
● $* : Gives complete set of arguments in a single string.
● $0 : Gives the name of the script.
● $1 : Gives the first arguments.
● Upto $9. Shift command is use if there are more.
● $@ : Same as $* but as an array.
● $$ : PID of current shell
● $! : PID of last background job
<date/time> <footer> 12
Exit Status
● Every command returns a value after execution called exit status or
return value.
● $? is used to get the exit status.
● $? stores the exit status of last executed command
● If $? = 0, then the last executed command was successful.
● Otherwise, the last executed command failed.
● That’s the reason we return 0 from main.
● You can exit from a script with some status using: exit <status>
<date/time> <footer> 13
Logical Operator
● AND operator (&&)
− <command-1> && <command-2>
− <command-2> is executed only if <command-1> is executed properly
● OR operator (II)
− <command-1> || <command-2>
− <command-2> is executed only if <command-1> failed.
<date/time> <footer> 14
Conditional Statement
if <command is successful> if <command is successful>
then then
<execute commands> <execute commands>
fi elif <command is successful>
if <command is successful> then
then <execute commands>
<execute commands> elif <command is successful>
else …
<execute commands> else <execute command>
fi fi
<date/time> <footer> 15
Comparison
● Comparison is done using ‘test’ command or [ ]
● Numerical comparisons if test $x -gt $y; if [$x -gt $y]
− -eq : equal to then then
− -ne : not equal to echo $x echo $x
− -gt : greater than else else
− -ge : greater than equal to echo $y echo $y
− -lt : less than fi fi
− -le : less than equal to
<date/time> <footer> 16
Assignment – 1
Write a script that will take a file path and a folder path in
interactive mode (Means, it will prompt messages asking to provide
the inputs one by one) and take a backup of the fileby copying it
into the folder path. If the folder does not exist, the script should
create the folder and take the backup then.
[Hint: use -p option while creating a directory using the command
to easily create sub-directories. See the manual page of the
command for details]
<date/time> <footer> 17
Assignment – 2
Write a script that will take three numbers which represent three
sides of a triangle as command line argument inputs. Your script
should check the validity of the inputs given by the user. The script
should exit with following exit status -
a) Exit status = 1, when number of parameters are not three.
b) Exit status = 2, when three sides does not form a valid triangle
Otherwise, the script sold terminate normally. It should not show
any kind of output message in any of these scenarios.
<date/time> <footer> 18
Assignment – 3
● Write a script that should take three sides of a triangle as
command line arguments. It will then invoke the script you write
for Assignment – 2 to check the validity of the inputs. If the
second script terminates with exit status 1, you should show a
message “A triangle has only three sides!”. If the second script
terminates with exit status 2, you should show a message
“These three sides cannot form a triangle!”. Otherwise your
program should check whether the three sides form a
right-angled triangle or not. Then, prompt the user whether the
sides form a right-angled triangle or not with an appropriate
message.
<date/time> <footer> 19
Thank You
<date/time> <footer> 20