ESSENTIAL SHELL PROGRAMMING
• Shell can be used for programming language
• Shell programs runs in interpretive mode.
• Shell programs run slower than programs written in high-level
languages but powerful.
SHELL SCRIPTS
• Shell script or shell program is a group of commands that are stored
in a file and have to be executed regularly.
• It is not mandatory to have .sh extension for shell
#!/bin/sh
#script.sh : sample shell script
echo “Today’s date: `date`”
echo “This months calendar: “
cal `date “+ % m 20 % y” `
echo “My shell : $SHELL”
read: MAKING SCRIPTS INTERACTIVE
USING COMMAND LINE ARGUMENTS
exit AND EXIT STATUS OF COMMANDS
• exit 0 – used when everything went fine
• exit 1 – used when something went wrong
• $? – the parameter $? Stores the exit status of the last command.
• A nonzero value means the command failed.
&& AND || OPERATORS
THE if CONDITIONAL
• Form1
if command is successful
then
execute commands
else
execute commands
fi
• Form2
if command is successful
then
execute commands
fi
• Form3
if command is successful
then
execute commands
elif command is successful
then …
else
fi
#!/bin/sh
#emp3.sh:Using if and else
#
if grep “^$1” /etc/passwd 2>/dev/null
then
echo “Pattern found – Job over”
else
echo “Pattern not found”
fi
$emp3.sh ftp
ftp:*:325:15:FTP User:/users1/home/ftp:/bin/true
Pattern found – Job over
$emp3.sh mail
Pattern not found
USING test AND [] TO EVALUATE EXPRESSIONS
• test works in three ways –
compares two numbers
compares two strings or a single one for a null value
checks a files attributes
• Numeric Comparison
$emp4.sh
Enter the string to be searched: [Enter]
You have not entered the string
$emp4.sh
Enter the string to be searched : root
Enter the filename to be used: /etc/passwd
Root:x:0:1:Super-User:/:/usr/bin/bash
String Tests Used by test
s1 = s2 String s1 = s2
s1 != s2 String s1 is not equal to s2
-n stg String stg is not a null string
-z stg String stg is a null string
stg String stg is assigned and not null
s1 == s2 String s1==s2
File Tests
$ filetest.sh emp3.lst
File does not exist
$ filetest.sh emp.lst
File is both readable and writable
THE case CONDITIONAL
expr
• Expr can perform the basic four arithmetic operations
• $ x=3 y=5
• $expr $x - $y
-2
• z = `expr $x + $y `
echo $z
$0:CALLING A SCRIPT BY DIFFERENT NAMES
while : LOOPING
while condition is true
do
execute commands
done
answer = y
while [ “$answer” -eq “y”]
do
---
done
for : LOOPING WITH A LIST
Syntax -
for variable in list
do
execute commands
done
$ for x in 1 2 4 5
do
echo “ the value of x is $x”
done
set – assigning values to positional parameters
shift – shifting arguments left
• set converts arguments into positional parameters
• set 9876 2345 6213
• echo “\$1 is $1, \$2 is $2, \$3 is $3”
$1 is 9876, $2 is 2345, $3 is 6213
• set `date`
$ echo $*
Sat sep 13 21:12:39 IST 1997
$echo $*
Sat sep 13 21:12:39 IST 1997
$ echo $1 $2 $3
Sat sep 13
$shift
$ echo $1 $2 $3
Sep 13 21:12:39
$shift 2
$echo $1 $2 $3
21:12:39 IST 1997
THE HERE DOCUMENT(<<)
• Placing the data in the shell script itself.
• Here document specifies that the data is here rather than in a separate file.
$ grep “03” << the_end
>01:accounts:6203
>02:admin:3232
>03:market:111
>the_end
01:accounts:6203
03:market:111
trap: interrupting a program
• Syntax
trap ‘command_list’ signal_list
When a script is sent any of the signals that are in the signal_list, trap
executes the commands in command_list.
trap ‘echo “Program interrupted”; exit’ 1 2 15
trap ‘echo “Program interrupted”; exit’ HUP INT TERM
Shell variables
• Local variable
• Global variable
• Set
• env