Saturday, April 18, 2009

Trap command on linux shell script

Before learning about trap command let's see an example about the necessity of the trap command. Below is a program while will take a filename from the user and put student information into the file.

To do that, with a while loop at first all records are inserted into a temporary file and then whenever user likes to add no more student information then from temporary file data will be saved to the desired file.

# cat trap_command1.sh
input()
{
count=0
echo "Student Information Entry"
echo -n "Enter filename to put the information: "
read filename
if [ -f $filename ]
then
echo "$filename does not exist. Creating $filename ...."
echo "Student Information Entry" > $filename
fi
cat /dev/null>/tmp/tmpfile
while :
do
echo -n "Enter StudentID: "; read stdid
echo -n "Enter Student Name: "; read name
echo -n "Enter Department: "; read dept
echo -n "Do you want to save (y/n) ? "
read yesno
if [ $yesno = y -o $yesno = Y ]
then
count=`expr $count + 1`
echo $count. $stdid $name $dept>>/tmp/tmpfile
fi
echo "Add another entry? (y/n)? "
read yesno
if [ $yesno = n -o $yesno = N ]
then
cat /tmp/tmpfile>>$filename
rm -f /tmp/tmpfile
return
fi
done
}
input

# ./trap_command1.sh
Student Information Entry
Enter filename to put the information: student
Enter StudentID: 074401
Enter Student Name: Arju
Enter Department: CIT
Do you want to save (y/n) ? y
Add another entry? (y/n)?
y
Enter StudentID: 074402
Enter Student Name: Momin
Enter Department: EEE
Do you want to save (y/n) ? y
Add another entry? (y/n)?
n

# cat student
1. 074401 Arju CIT
2. 074402 Momin EEE

Now if user in between press CTRL+C then inside tmp directory temp file(tmpfile here) will reside which we might not to keep. So, there must be a mechanism which can catch the CTRL+C (trap) single. One of the function of trap command is it can catch that single along with other singles.

The usage syntax of trap command is,

trap {commands} {signal number list}

where commands determine what to be done or executed. And single number can have value 0,1,2,3,9. Each of this number express different signals. Below is the meaning of these numbers.

0 = shell exit
1 = hangup
2 = interrupt (CTRL+C)
3 = quit
9 = kill (cannot be caught)

So signal 9 can be caught by trap. Rest four can be caught. An example of catching interrupt signal (CTRL+C) is shown below.


# cat trap_command2.sh
del_temp_file()
{
echo "You press CTRL+C and my program handle it!"
echo "Removing temporary files ..."
rm -f /tmp/tmpfile
exit 1
}
input()
{
count=0
echo "Student Information Entry"
echo -n "Enter filename to put the information: "
read filename
if [ -f $filename ]
then
echo "$filename does not exist. Creating $filename ...."
echo "Student Information Entry" > $filename
fi
cat /dev/null>/tmp/tmpfile
while :
do
echo -n "Enter StudentID: "; read stdid
echo -n "Enter Student Name: "; read name
echo -n "Enter Department: "; read dept
echo -n "Do you want to save (y/n) ? "
read yesno
if [ $yesno = y -o $yesno = Y ]
then
count=`expr $count + 1`
echo $count. $stdid $name $dept>>/tmp/tmpfile
fi
echo "Add another entry? (y/n)? "
read yesno
if [ $yesno = n -o $yesno = N ]
then
cat /tmp/tmpfile>>$filename
rm -f /tmp/tmpfile
return
fi
done
}
trap del_temp_file 2
input

# ./trap_command2.sh
Student Information Entry
Enter filename to put the information: student2.txt
Enter StudentID: 074401
Enter Student Name: You press CTRL+C and my program handle it!
Removing temporary files ...

Related Documents
http://arjudba.blogspot.com/2009/04/functions-in-shell-script.html

No comments:

Post a Comment