LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Trap --- I am confused ....... (https://www.linuxquestions.org/questions/linux-newbie-8/trap-i-am-confused-921222/)

jv2112 12-30-2011 08:52 AM

Trap --- I am confused .......
 
1 Attachment(s)
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. :doh: 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. :hattip:

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


tronayne 12-31-2011 11:47 AM

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.

David the H. 12-31-2011 12:19 PM

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.

jv2112 01-01-2012 07:36 AM

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.


:study: --> 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



All times are GMT -5. The time now is 09:03 PM.