LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-30-2011, 09:52 AM   #1
jv2112
Member
 
Registered: Jan 2009
Location: New England
Distribution: Arch Linux
Posts: 719

Rep: Reputation: 103Reputation: 103
Question Trap --- I am confused .......


I am trying to understand how traps work so I can expand my knowledege. From what I read if the signals listed are recieved from a source outside the script then the command is run upon recieving the signal.

I have tried a few modifications of this but no dice. However the function does get placed in the environment of the shell so if I type fx I get the result.

I have included a screenshot of what I get upon CTRL Z / C and the script I have been trying to learn through.

Any direction would be appreciated.

Code:
#! /bin/bash
clear
figlet Endless Cyber Pit

fx ()
{
echo "You trying to leave"

}


echo -e "\e[107;34m Counting Forever --> \e[00m"
declare -i a=0
echo -e "\e[00;33m ######################################################## \e[00m"

trap "fx" 15 20


while true
do 
((a++))
sleep .05
printf "$a\r"
done
Attached Thumbnails
Click image for larger version

Name:	trapster.png
Views:	8
Size:	23.8 KB
ID:	8721  

Last edited by jv2112; 12-30-2011 at 09:54 AM.
 
Old 12-31-2011, 12:47 PM   #2
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,476

Rep: Reputation: 1020Reputation: 1020Reputation: 1020Reputation: 1020Reputation: 1020Reputation: 1020Reputation: 1020Reputation: 1020
The trap command is used for two purposes:
  1. execute a command or sequence of commands when the shell receives a signal.
  2. inhibit the shell from acting upon a signal.
The syntax is
Code:
trap [ arg ] [ sig ]
Now, the trap command can be used to "catch" signals and perform certain actions based on the signal value. In the format, shown above, arg is a command to be read and executed when the shell receives signal sig; note that arg is scanned once when the trap is set and once when the trap is taken. Each sig can be given a number or as the name of the signal. Trap commands are executed in order of signal number. Any attempt to set a trap on a signal that was ignored on entry to the current shell is ineffective; note that "the current shell" will be a running shell program (shell programs are forked and executed in a new shell.

These trap conditions are valid:
  • If arg is omitted or is -, then all trap(s) sig are reset to their original values.
  • If arg is the null string then this signal is ignored by the shell and by the commands it invokes.
  • If sig is ERR then arg will be executed whenever a command has a nonzero exit code -- this trap is not inherited by shell functions.
  • If sig is 0 (zero) or EXIT and the trap statement is executed inside the body of a function, then the command arg is executed after the function completes.
  • If sig is 0 (zero) or EXIT for a trap set outside any function arg is executed on exit from the shell.
Examples:
Code:
# Exit on signal 2
trap 'exit' 2

# Null trap
trap ':' 2 3

# Ignoring Signales
trap ' ' 1 2 3 15

# Trapping a trap
trap 'trap "" 1 2 3 15; rm -f tmp*' 1 2 3 15

# Resetting Trap for Signals 2 and 3
trap 2 3
Code:
Signal  Name            Meaning
  1     HUP     hangup -- the user has logged off
  2     INT     interrupt -- the user has pressed the intr key
  3     QUIT    Quit -- the user has pressed the quit key
  9     KILL    Kill (cannot be ignored)
 15     TERM    Software termination
 10     BUS     Bus error, usually a programming error
 11     SEGV    Segmentation programming error
 14     ALRM    Alarm timeout
 24     TSTP    Terminal stop -- user has pressed the susp key
Code:
Command         Purpose                 Example
trap cmd sig    Set trap for cmd on sig trap exit 2
trap sig        Reset trap for sig      trap 2
trap '' sig     Ignore signal sig       trap '' 2
The above are Bourne shell and KornShell syntax; I believe they're identical in BASH -- check your manual page(s).

Hope this helps some.
 
Old 12-31-2011, 01:19 PM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
What exactly are you confused about? It's not completely clear to me from your post or your screenshot.

I'm not really an expert on it, but if you're asking about how to trap the ctrl+z (SIGTSTP) signal, I believe your problem is mostly that the job control of the running shell is catching it before it gets to the script. When I use set +m to disable job control, I can trap that signal with no problem.

In any case the usual purpose of trap in a script is simply to run some kind of cleanup function if it should terminate irregularly. So I think SIGHUP, SIGINT and SIGTERM are usually the ones you mostly need to be worried about. You can also include the EXIT pseudo-signal to also execute it when the script exits.

Code:
trapit(){

	rm -f "$tempdir"
	echo "Removed the temporary directory before exiting."
	exit

}

trap "trapit" SIGHUP SIGINT SIGTERM EXIT
I think it's also better to use the signal names rather than the numbers. It's more readable and portable.

Last edited by David the H.; 12-31-2011 at 01:24 PM. Reason: my mistake
 
Old 01-01-2012, 08:36 AM   #4
jv2112
Member
 
Registered: Jan 2009
Location: New England
Distribution: Arch Linux
Posts: 719

Original Poster
Rep: Reputation: 103Reputation: 103
Thumbs up

Thank you all for the inisght and direction. It was a great help. My hang up in this test script was the placement as David the H. suggested.


--> Back to studying --->

Code:
#! /bin/bash
clear
figlet Endless Cyber Pit

fx ()
{
echo "You trying to leave the Abyss..."
sleep 3
echo
echo -e "\e[107;31m The only way out is down.................... \e[00m"
sleep 2
echo
echo -e "\e[00;33m #### You are free ....... #### \e[00m"
sleep 1
exit

}


echo -e "\e[107;34m Counting Forever --> \e[00m"
declare -i a=0
echo -e "\e[00;33m ######################################################## \e[00m"


while true
do 
((a++))
sleep .05
printf "$a\r"
trap "fx" SIGTERM SIGINT SIGTSTP SIGQUIT
done
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to send snmp trap & recieve trap in C program minil Programming 3 07-10-2010 10:22 AM
SNMP Trap metallica1973 Linux - Networking 1 11-28-2007 10:14 AM
Kernel trap (Fatal trap 12) m!k@EL *BSD 4 09-06-2007 12:58 AM
specific-trap? PTBmilo Linux - Security 6 05-26-2003 04:09 PM
snmp-trap tied2 Linux - Security 7 07-19-2002 09:20 PM


All times are GMT -5. The time now is 09:52 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration