L16 Shell Script
L16 Shell Script
– Shell scripts.
• Definition.
• Uses of shell scripts.
• Writing shell scripts.
Shell scripts
A shell script is a text file with Unix
commands in it.
Shell scripts usually begin with a #! and a
shell name (complete pathname of shell).
– Pathname of shell be found using the which
command.
– The shell name is the shell that will execute
this script.
• E.g: #!/bin/bash
Shell scripts (contd.)
If no shell is specified in the script file, the
default is chosen to be the currently
executing shell.
Shell scripts (contd.)
Any Unix command can go in a shell script
– Commands are executed in order or in the
flow determined by control statements.
Different shells have different control
structures
– The #! line is very important.
– We will write shell scripts with the Bourne
shell (bash).
Shell scripts (contd.)
A shell script as a standalone is an
executable program:
– Must use chmod to change the permissions of
the script to be executable also.
Can run script explicitly also, by
specifying the shell name.
– E.g: $ bash myscript
– E.g: $ csh myscript
Shell scripts (contd.)
Consider the example
– $ bash myscript
– Invokes the bash shell and then runs the script
using it.
– Its almost as if we had the line #! /bin/bash in
the file myscript.
– Similarly with the second example using csh.
– myscript need not be an executable as bash is
running the script on its behalf.
Shell scripts (contd.)
Why write shell scripts ?
– To avoid repetition:
• If you do a sequence of steps with standard Unix
commands over and over, why not do it all with
just one command?
• Or in other words, store all these commands in a
file and execute them one by one.
Shell scripts (contd.)
Why write shell scripts ?
– To automate difficult tasks:
• Many commands have subtle and difficult options
that you don’t want to figure out or remember
every time .
Simple Example
Assume that I need to execute the
following commands once in a while when
I run out of disk space:
rm -rf $HOME/.netscape/cache
rm -f $HOME/.netscape/his*
rm -f $HOME/.netscape/cookies
rm -f $HOME/.netscape/lock
rm -f $HOME/.netscape/.nfs*
rm -f $HOME/.pine-debug*
rm -fr $HOME/nsmail
Simple Example (contd.)
We can put all those commands into a shell
script, called myscript.
[axgopala@nitrogen axgopala]$ cat myscript
#! /bin/bash
rm -rf $HOME/.netscape/cache
rm -f $HOME/.netscape/his*
rm -f $HOME/.netscape/cookies
rm -f $HOME/.netscape/lock
rm -f $HOME/.netscape/.nfs*
rm -f $HOME/.pine-debug*
rm -fr $HOME/nsmail
Sample Example (contd.)
To run the script:
– Step 1:
• $ chmod u+x myscript
– Step 2:
• Run the script:
• $ ./myscript
Each line of the script is processed in
order.
Shell scripts (contd.)
Shell variables:
– Declared by:
– varname=varvalue
– To make them an environment variable, we
export it.
– export varname=varvalue
Shell scripts (contd.)
Assigning the output of a command to a
variable:
– Using backquotes, we can assign the output of
a command to a variable:
#! /bin/bash
filelist=`ls`
echo $filelist
Shell scripts (contd.)
Example:
[axgopala@nitrogen public]$ ls
a b c html/
[axgopala@nitrogen public]$ filelist=`ls`
[axgopala@nitrogen public]$ echo $filelist
a b c html/
[axgopala@nitrogen public]$
Shell scripts (contd.)
The expr command:
– Calculates the value of an expression.
– E.g:
for i in *
do
echo $i
done
#! /bin/bash
if grep "UNIX" myfile >/dev/null
then
echo UNIX occurs in myfile
else
echo No!
echo UNIX does not occur in myfile
fi
Using elif with if
#! /bin/bash
if grep "UNIX" myfile >/dev/null
then
echo UNIX occurs in myfile
elif grep “DOS” myfile > /dev/null
then
echo DOS appears in myfile not UNIX
else
echo nobody is here in myfile
fi
Using colon in shell scripts
Sometimes, we do not want a statement to
do anything.
– In that case, use a colon ‘:’
• if grep UNIX myfile > /dev/null
• then
• :
• fi
• Does not do anything when UNIX is found in
myfile .
The test command
Use for checking validity.
Three kinds:
– Check on files.
– Check on strings.
– Check on integers
Notes on test
Testing on files.
– test –f file: does file exist and is not a
directory?
– test -d file: does file exist and is a directory?
– test –x file: does file exist and is executable?
– test –s file: does file exist and is longer than 0
bytes?
Example
#!/bin/bash
count=0
for i in *; do
if test –x $i
then
count=`expr $count + 1`
fi
done
echo Total of $count files executable
NOTE: expr $count + 1 serves the purpose
of count++
Notes on test
Testing on strings.
– test –z string: is string of length 0?
– test string1 = string2: does string1 equal
string2?
– test string1 != string2: not equal?
Example
#! /bin/bash
if test -z $REMOTEHOST
then
:
else
DISPLAY="$REMOTEHOST:0“
export DISPLAY
fi
#! /bin/bash
x=1
until [ $x -gt 3 ]
do
echo x = $x
x=`expr $x + 1`
done