Wednesday, May 23, 2018

Trap in shell script,invoking subroutine using a key combination,Linux Teacher Sourav,Kolkata 09748184075

#!/bin/sh
#
# 23/05/2018
#
echo "Usage of trap in Shell Script,for example a key combination can invoke a subroutine"
trap catchCtrlC INT
# Initialize the trap
# Subroutines
catchCtrlC()
{
echo "Entering catchCtrlC routine."
}
# Code starts here
echo "Press Ctrl-C to trigger the trap, 'Q' to exit."
loop=0
while [ $loop -eq 0 ]
do
read -t 1 -n 1 str
rc=$?
if [ $rc -gt 128 ] ; then
echo "Timeout exceeded."
fi
if [[ "$str" = "q" || "$str" = "Q" ]] ; then
echo "Exiting the script."
# wait 1 sec for input or for 1 char
loop=1
fi
done
exit 0

No comments:

Post a Comment