Shell Script Programming : November 2006
ISTS Confidential Shell Script Programming By
Surender Kumar
What is a Shell : Create | Enable | Optimize : Retail and Payment Technology 2 What is a Shell A shell is a program that acts as the interface between the user and UNIX system
It acts as an interpreter or translator
It allows user to enter commands for the operation system to execute
It hides the details of the kernel’s operation from the user. So, < and > are used to redirect files, | to pipe up jobs, $(…) to output from a subprocess, etc.
Shells can be command-line-driven or graphical
Slide 3 : Create | Enable | Optimize : Retail and Payment Technology 3 Shell Model
Slide 4 : Create | Enable | Optimize : Retail and Payment Technology 4 Because UNIX is modular, there are many different shells in use. They are
Bourne shell (sh)
C shell (csh, tcsh and zsh)
Korn shell (ksh, pdksh)
Bourne Again shell (bash)
Rc
Except for the C shell, all of these are very similar
To identify which shell is using:
ps (process status) or echo $SHELL SHELLS DESCRIPTION
What are they for? : Create | Enable | Optimize : Retail and Payment Technology 5 What are they for? To automate certain common activities an user performs routinely.
They serve the same purpose as batch files in DOS/Windows.
Example:
rename 1000 files from upper case to lowercase
Why not use C/C++ for that? : Create | Enable | Optimize : Retail and Payment Technology 6 Why not use C/C++ for that? C/C++ programming requires compilation and linkage, maybe libraries, which may not be available (production servers).
For the typical tasks much faster in development, debugging, and maintenance (because they are interpreted and do not require compilation).
Writing Shell Programs : Create | Enable | Optimize : Retail and Payment Technology 7 Writing Shell Programs There are two ways of writing shell programs.
Either type in a sequence of commands and allow the shell to execute them interactively, or store those commands in a file which can then be invoked as a program
Slide 8 : Create | Enable | Optimize : Retail and Payment Technology 8 Interactive Programs
Just typing in the shell script on the command line is a quick and easy way of trying out small code fragments
E.g.
$ for file in *
> do
> if grep -l POSIX $file
> then
> more $file
> fi
Done Interactive Programs
Slide 9 : Create | Enable | Optimize : Retail and Payment Technology 9 We can use any text editor to write scripts #!/bin/sh
# first.sh
# This script displays all text files which contains string “POSIX” in current director
for file in *
do
if grep –q POSIX $file
then
more $file
fi
done
exit 0 Writing Shell Scripts
Slide 10 : Create | Enable | Optimize : Retail and Payment Technology 10 Comments start with a #
The #! Characters tell the system that the one argument that follows on the line is the program to be used to execute this file. In this case /bin/sh is the default shell program
The exit command ensures that the script returns a sensible exit code. A zero denotes success in shell programming
To execute a script
$ /bin/sh first.sh
or
$ chmod +x first.sh
$ ./first.sh Writing Shell Scripts
Variables : Create | Enable | Optimize : Retail and Payment Technology 11 Variables No declaration. Variables is created when it is first assign an initial value to it.
By default, all variables are stored as strings, even when they are assigned numeric values
UNIX is a case-sensitive system, so the variable foo is different from Foo
Slide 12 : Create | Enable | Optimize : Retail and Payment Technology 12 Example
$ salutation="Yes Dear"
$ echo $salutation
Yes Dear
$ salutation=7+5
$ echo $salutation
7+5 Notice, there is no space between the "=" when assigning a variable Example
Quoting : Create | Enable | Optimize : Retail and Payment Technology 13 Quote ("…") the parameter when it contains one or more whitespace characters
When the $variable expression is enclosed in double quotes("…"), it will be replaced with its value
Preface the $variable expression with a \ to remove the special meaning of the above expression.
Normally, strings are enclosed in double quotes, which protects variables from being separated by white space, but allows $ expansion to take place Quoting
Environment Variables : Create | Enable | Optimize : Retail and Payment Technology 14 Environment Variables They are initialized when the shell script starts and normally capitalized to distinguish them from user-defined variables in scripts
To display all variables in the local shell and their values, type the set command
The unset command removes the variable from the current shell and subshell
Parameter Variables : Create | Enable | Optimize : Retail and Payment Technology 15 Parameter Variables
Slide 16 : Create | Enable | Optimize : Retail and Payment Technology 16 Given the following script and saved as myscript
#!/bin/sh
salutation="Hello"
echo $salutation
echo "The program $0 is now runnning"
echo "The 1st & the 2nd parameters were $1 & $2"
echo $*
exit 0
If we run this script as
$ ./myscript foo bar baz
what will the output be? Example
Slide 17 : Create | Enable | Optimize : Retail and Payment Technology 17 #!/bin/sh
echo "I was called with $# parameters"
echo "My name is $0"
echo "My first parameter is $1"
echo "My second parameter is $2"
echo "All parameters are $@"
Run the code and see the output:
$ sh ./var3.sh hello world earth Example
Conditions - test or [ ] command : Create | Enable | Optimize : Retail and Payment Technology 18 Conditions - test or [ ] command Check whether a file exists
if test -f fred.c
then
......
fi
It could be written in other forms:
if [ -f fred.c ] if [ -f fred.c]; then
then .....
..... or fi
fi Notices that spaces are required around the '[' and ']' characters
Slide 19 : Create | Enable | Optimize : Retail and Payment Technology 19 Spaces before and after the = sign is required. Operators for Strings
Control Structures : Create | Enable | Optimize : Retail and Payment Technology 20 if condition
then
...
else
...
fi Control Structures Example
#!/bin/sh
echo "Is it morning? (Answer yes or no)"
read timeofday
if [ $timeofday = "yes" ]; then
echo "Good Morning"
else
echo "Good afternoon"
fi
exit 0
Slide 21 : Create | Enable | Optimize : Retail and Payment Technology 21 elif - Doing further Checks #!/bin/sh
echo “Enter Your name:”
read YOURNAME
if test YOURNAME = “good”
then
echo “you are good”
elif test YOURNAME = “bad”
then
echo “you are bad”
fi
exit 0
Slide 22 : Create | Enable | Optimize : Retail and Payment Technology 22 ep1 -eq ep2 True if ep1 = ep2
ep1 -ne ep2 True if ep1 ? ep2
ep1 -gt ep2 True if ep1 > ep2
ep1 -ge ep2 True if ep1 >= ep2
ep1 -lt ep2 True if ep1 < ep2
ep1 -le ep2 True if ep1 <= ep2
! ep True if ep is false
Example
$ x=5; y=7
$ if [ $x -lt $y ]; then
> echo "x is less than y"
> fi Conditional operators
Slide 23 : Create | Enable | Optimize : Retail and Payment Technology 23 -d file True if the file is a directory
-e file True if the file exists
-f file True if the file is a regular file
-g file True if set-group-id is set on file
-r file True if the file is readable
-s file True if the file has non-zero size
-u file True if set-user-id is set on file
-w file True if the file is writeable
-x file True if the file is executable
Example
$ mkdir temp
$ if [ -d temp ]; then
> echo "temp is a directory"
> fi File Conditions
Slide 24 : Create | Enable | Optimize : Retail and Payment Technology 24 #!/bin/sh
for foo in bar fud 43
do
echo $foo
done
exit 0 #!/bin/sh
for i in 1 2 3 4 5
do
echo "Number: $i"
done for variable in values
do
....
done Looping -- for
Slide 25 : Create | Enable | Optimize : Retail and Payment Technology 25 #!/bin/sh
foo=1
while [ "$foo" -le 5 ]; do
echo "Here we go again"
foo=$(($foo+1))
done
exit 0 while condition
do
....
done What are the results? Looping -- while
Slide 26 : Create | Enable | Optimize : Retail and Payment Technology 26 until condition
do
....
done #!/bin/sh
x=1
until [ "$x" -ge 20 ]
do
echo $x
x=$(($x+1))
done
exit 0 Looping -- until
Slide 27 : Create | Enable | Optimize : Retail and Payment Technology 27 case
case variable in
pattern [ | pattern] ...) statements;;
pattern [ | pattern] ...) statements;;
....
esac
#!/bin/sh
echo "Is it morning? Enter yes or no";read timeofday
case "$timeofday" in
yes | y | Yes | YES) echo "Good Morning";;
n* | N* ) echo "Good Afternoon";;
* ) echo "Sorry, answer not recognized"
echo "Please answer yes or no"
exit 1;;
esac Case construct
Slide 28 : Create | Enable | Optimize : Retail and Payment Technology 28 statement1 && statement2 && statement3 && ...
Starting at the left, each statement is executed and, if it returns true, the next statement to the right is executed. This continues until a statement return false
$ test1=yes
$ test2=no
$ [ $test1 = yes ] && echo 1 && [ $test2 = yes ] && echo 2
1
$ AND List
Slide 29 : Create | Enable | Optimize : Retail and Payment Technology 29 statement1 || statement2 || statement3 || ....
Starting at the left, each statement is executed. If it returns false, the next statement to the right is executed. This continues until a statement returns true
#!/bin/sh
rm -f file_one
if [ -f file_one ] || echo "1" || echo "2"
then
echo "in if"
else
echo "in else"
fi
exit 0 OR List
Functions : Create | Enable | Optimize : Retail and Payment Technology 30 Functions To define a shell function, we simply write its name, followed by empty ( ) and enclose the statements in { } braces:
function_name() {
.....
.....
}
Slide 31 : Create | Enable | Optimize : Retail and Payment Technology 31 #!/bin/sh
yes_or_no(){
echo "Is your name $* ?"
while true; do
echo -n "Enter yes or no: “; read x
case "$x" in
y | yes ) return 0;;
n | no ) return 1;;
* ) echo "Answser yes or no"
esac
done
}
if yes_or_no "$1“;then
echo "Hi $1, nice name"
else
echo "Never mind"
fi
exit 0 Explain what does this script do. How to run this script if its file name is "verify"? Example of Function
Slide 32 : Create | Enable | Optimize : Retail and Payment Technology 32 Basically, shell functions have no scoping, other than the parameters ($1, $2, $@, etc)
#!/bin/sh
myfunc() {
VARCHECK=1
echo "myfunc() VARCHECK:$VARCHECK "
}
### Value of VARCHECK persists
myfunc
echo “AGAIN VARCHECK=$VARCHECK" Scope of Variables
Slide 33 : Create | Enable | Optimize : Retail and Payment Technology 33 break is used to escape from an enclosing for-loop, while-loop or until-loop. #!/bin/sh
for file in *
do
if [ -d "$file" ]; then
echo “$file is a directory”
break;
fi
done Break Statement
Slide 34 : Create | Enable | Optimize : Retail and Payment Technology 34 continue makes the enclosing for-loop, while-loop or until-loop continues at the next iteration. #!/bin/sh
for file
do
if [ -d "$file" ]; then
continue
fi
mv "$file" "$(file)_file"
done
exit 0 Continue statement
Slide 35 : Create | Enable | Optimize : Retail and Payment Technology 35 The expr command evaluates its arguments as an expression:
$ expr 8 + 6
14
$ x=`expr 12 / 4 `
$ echo $x
3
The back quotes (`…`) characters make x take the result of expr $x + 1 expr Command
THE END : Create | Enable | Optimize : Retail and Payment Technology 36 THE END HAVE A NICE DAY