How ERR trap works?

# Sample script that shows how ERR signal works, when a command returns a non-zero status it triggers to calls to print the message in double quotes.

trap ‘print “You gave me non-integer. Try again”‘ ERR
typeset -i number
while true
do
print -n “Enter an integer: ”
read number 2> /dev/null
if [ $? -eq 0 ]; then
break
fi
done

# reset trap else the grep command below would also generate the error message
trap – ERR
grep “blahblah” /etc/passwd > /dev/null 2>&1
echo “Number entered: $number

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s