LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-21-2010, 11:48 AM   #1
sree_ec
Member
 
Registered: Sep 2010
Location: world
Distribution: Ubuntu 12.04LTS
Posts: 76

Rep: Reputation: 5
Using trap in bash script


Code:
#!/bin/sh
onexit()
{

echo "Error At line - $1"


}
trap 'onexit ${LINENO}' 127
Why this code gives me error
Quote:
trap: 127: bad trap
I tried
Code:
trap 'onexit ${LINENO}' ERR
as well. It gives me same error

What I want to do is to make the onexit function to handle any generic error that comes during script execution. I am not thinking about using 'set -e' since it will cause a quick exit.
I did 127, because I wanted my script to handle atleast the command not found error.
I am not thinking about handling any interrupt or any other signal at the moment.

Last edited by sree_ec; 11-21-2010 at 11:49 AM.
 
Old 11-21-2010, 05:31 PM   #2
pupok
LQ Newbie
 
Registered: Jul 2009
Location: Banská Bystrica, Slovak republic
Distribution: Slackware, Ubuntu
Posts: 18

Rep: Reputation: 2
This way

Try it this way
Code:
#!/bin/sh
onexit()
{
echo "Error At line - $1"
}
trap "{ onexit \${LINENO} ; }" ERR

badcommand1
ls -d /
badcommand2

Last edited by pupok; 11-21-2010 at 05:46 PM. Reason: After realizing the real problem, complete rewrite
 
1 members found this post helpful.
Old 11-21-2010, 07:47 PM   #3
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,783

Rep: Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083
Trap deals with signals not exit status.
Code:
~$ help trap
trap: trap [-lp] [[arg] signal_spec ...]
    Trap signals and other events.
...
    If a SIGNAL_SPEC is EXIT (0) ARG is executed on exit from the shell.  If
    a SIGNAL_SPEC is DEBUG, ARG is executed before every simple command.
    
...    
    Options:
      -l	print a list of signal names and their corresponding numbers
      -p	display the trap commands associated with each SIGNAL_SPEC
    
    Each SIGNAL_SPEC is either a signal name in <signal.h> or a signal number.
...
~$ trap -l
 1) SIGHUP	 2) SIGINT	 3) SIGQUIT	 4) SIGILL	 5) SIGTRAP
 6) SIGABRT	 7) SIGBUS	 8) SIGFPE	 9) SIGKILL	10) SIGUSR1
11) SIGSEGV	12) SIGUSR2	13) SIGPIPE	14) SIGALRM	15) SIGTERM
16) SIGSTKFLT	17) SIGCHLD	18) SIGCONT	19) SIGSTOP	20) SIGTSTP
21) SIGTTIN	22) SIGTTOU	23) SIGURG	24) SIGXCPU	25) SIGXFSZ
26) SIGVTALRM	27) SIGPROF	28) SIGWINCH	29) SIGIO	30) SIGPWR
31) SIGSYS	34) SIGRTMIN	35) SIGRTMIN+1	36) SIGRTMIN+2	37) SIGRTMIN+3
38) SIGRTMIN+4	39) SIGRTMIN+5	40) SIGRTMIN+6	41) SIGRTMIN+7	42) SIGRTMIN+8
43) SIGRTMIN+9	44) SIGRTMIN+10	45) SIGRTMIN+11	46) SIGRTMIN+12	47) SIGRTMIN+13
48) SIGRTMIN+14	49) SIGRTMIN+15	50) SIGRTMAX-14	51) SIGRTMAX-13	52) SIGRTMAX-12
53) SIGRTMAX-11	54) SIGRTMAX-10	55) SIGRTMAX-9	56) SIGRTMAX-8	57) SIGRTMAX-7
58) SIGRTMAX-6	59) SIGRTMAX-5	60) SIGRTMAX-4	61) SIGRTMAX-3	62) SIGRTMAX-2
63) SIGRTMAX-1	64) SIGRTMAX
ERR isn't one of the signals.
 
1 members found this post helpful.
Old 11-21-2010, 11:05 PM   #4
sree_ec
Member
 
Registered: Sep 2010
Location: world
Distribution: Ubuntu 12.04LTS
Posts: 76

Original Poster
Rep: Reputation: 5
Quote:
Originally Posted by pupok View Post
Try it this way
Code:
#!/bin/sh
onexit()
{
echo "Error At line - $1"
}
trap "{ onexit \${LINENO} ; }" ERR

badcommand1
ls -d /
badcommand2
Thanks mate. It works in RHEL..
But even my old code works when I tested it in RHEL. I first tested it on ubuntu ,but my code was not working.
I will test yours in ubuntu as well and post the result here .
 
Old 11-22-2010, 04:56 AM   #5
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
FYI:
Code:
c@CW8:~$ trap -l | grep ERR
c@CW8:~$ trap 'foo \$LINENO' ERR
[no output]
c@CW8:~$ echo $?
0
c@CW8:~$ bash --version
GNU bash, version 4.1.7(2)-release (x86_64-slackware-linux-gnu)
[snip]
 
1 members found this post helpful.
Old 11-22-2010, 11:20 AM   #6
sree_ec
Member
 
Registered: Sep 2010
Location: world
Distribution: Ubuntu 12.04LTS
Posts: 76

Original Poster
Rep: Reputation: 5
Quote:
Originally Posted by catkin View Post
FYI:
Code:
c@CW8:~$ trap -l | grep ERR
c@CW8:~$ trap 'foo \$LINENO' ERR
[no output]
c@CW8:~$ echo $?
0
c@CW8:~$ bash --version
GNU bash, version 4.1.7(2)-release (x86_64-slackware-linux-gnu)
[snip]
Which means this works right?
Why this is not working in ubuntu??
I have used the above ccode as well which worked for me in RHEL.. That is also not working well in ubuntu.. It is showing bad trap....
 
Old 11-22-2010, 11:22 AM   #7
sree_ec
Member
 
Registered: Sep 2010
Location: world
Distribution: Ubuntu 12.04LTS
Posts: 76

Original Poster
Rep: Reputation: 5
Quote:
Originally Posted by sree_ec View Post
Which means this works right?
Why this is not working in ubuntu??
I have used the above ccode as well which worked for me in RHEL.. That is also not working well in ubuntu.. It is showing bad trap....
Quote:
sreejith@sreejith-Inspiron-N4010:~/work/shellscript$ trap 'foo \$LINENO' ERR
sreejith@sreejith-Inspiron-N4010:~/work/shellscript$ echo $?
0
sreejith@sreejith-Inspiron-N4010:~/work/shellscript$ bash --version
GNU bash, version 4.1.5(1)-release (i686-pc-linux-gnu)
Copyright (C) 2009 Free Software Foundation, Inc.

Why in shell script alone, it is showing bad trap???
 
Old 11-22-2010, 02:40 PM   #8
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by sree_ec View Post
Which means this works right?
Yes:
Code:
c@CW8:~$ trap foo ERR
c@CW8:~$ function foo {
> echo foo running
> }
c@CW8:~$ jfhdsfh
bash: jfhdsfh: command not found
foo running
Which bash version do you have? bash --version will tell.
 
Old 11-22-2010, 03:14 PM   #9
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,783

Rep: Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083
Quote:
Originally Posted by sree_ec View Post
Why in shell script alone, it is showing bad trap???
Probably in Ubuntu /bin/sh links to dash which doesn't support the pseudo-signal ERR, use #!/bin/bash instead.


@catkin: interesting, apparently the help text is missing some info. You've got an extra level of escaping though:
Code:
~$ foo() { echo lineo: $1; }
~$ trap 'foo \$LINENO' ERR
~$ false
lineo: $LINENO
~$ trap 'foo $LINENO' ERR
~$ false
lineo: 6
~$ trap "foo \$LINENO" ERR
~$ false
lineo: 8
Code:
$ man bash
...
       trap [-lp] [[arg] sigspec ...]
              If  a  sigspec is EXIT (0) the command arg is executed on exit from
              the shell.  If a sigspec is DEBUG,  the  command  arg  is  executed
              before every simple command, for command, case command, select com‐
              mand, every arithmetic for command, and before  the  first  command
              executes  in  a shell function (see SHELL GRAMMAR above).  Refer to
              the description of the extdebug option to  the  shopt  builtin  for
              details  of  its effect on the DEBUG trap.  If a sigspec is RETURN,
              the command arg is executed each time a shell function or a  script
              executed with the . or source builtins finishes executing.

              If  a sigspec is ERR, the command arg is executed whenever a simple
              command has a non-zero exit status, subject to the following condi‐
              tions.   The ERR trap is not executed if the failed command is part
              of the command list immediately following a while or until keyword,
              part  of the test in an if statement, part of a command executed in
              a && or ⎪⎪ list, or if the command's return value is being inverted
              via !.  These are the same conditions obeyed by the errexit option.
 
2 members found this post helpful.
Old 11-22-2010, 08:51 PM   #10
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by ntubski View Post
@catkin: interesting, apparently the help text is missing some info. You've got an extra level of escaping though
Oops! Thank you for pointing it out
 
Old 11-23-2010, 09:28 AM   #11
sree_ec
Member
 
Registered: Sep 2010
Location: world
Distribution: Ubuntu 12.04LTS
Posts: 76

Original Poster
Rep: Reputation: 5
Quote:
Originally Posted by ntubski View Post
Probably in Ubuntu /bin/sh links to dash which doesn't support the pseudo-signal ERR, use #!/bin/bash instead.


@catkin: interesting, apparently the help text is missing some info. You've got an extra level of escaping though:
Code:
~$ foo() { echo lineo: $1; }
~$ trap 'foo \$LINENO' ERR
~$ false
lineo: $LINENO
~$ trap 'foo $LINENO' ERR
~$ false
lineo: 6
~$ trap "foo \$LINENO" ERR
~$ false
lineo: 8
Code:
$ man bash
...
       trap [-lp] [[arg] sigspec ...]
              If  a  sigspec is EXIT (0) the command arg is executed on exit from
              the shell.  If a sigspec is DEBUG,  the  command  arg  is  executed
              before every simple command, for command, case command, select com‐
              mand, every arithmetic for command, and before  the  first  command
              executes  in  a shell function (see SHELL GRAMMAR above).  Refer to
              the description of the extdebug option to  the  shopt  builtin  for
              details  of  its effect on the DEBUG trap.  If a sigspec is RETURN,
              the command arg is executed each time a shell function or a  script
              executed with the . or source builtins finishes executing.

              If  a sigspec is ERR, the command arg is executed whenever a simple
              command has a non-zero exit status, subject to the following condi‐
              tions.   The ERR trap is not executed if the failed command is part
              of the command list immediately following a while or until keyword,
              part  of the test in an if statement, part of a command executed in
              a && or ⎪⎪ list, or if the command's return value is being inverted
              via !.  These are the same conditions obeyed by the errexit option.
You are spot on..
It is pointing to dash.. In dash I get this error!!!
 
Old 03-26-2012, 07:26 AM   #12
leaveinriver
LQ Newbie
 
Registered: Mar 2012
Posts: 1

Rep: Reputation: Disabled
Quote:
Originally Posted by ntubski View Post
Probably in Ubuntu /bin/sh links to dash which doesn't support the pseudo-signal ERR, use #!/bin/bash instead.


@catkin: interesting, apparently the help text is missing some info. You've got an extra level of escaping though:
Code:
~$ foo() { echo lineo: $1; }
~$ trap 'foo \$LINENO' ERR
~$ false
lineo: $LINENO
~$ trap 'foo $LINENO' ERR
~$ false
lineo: 6
~$ trap "foo \$LINENO" ERR
~$ false
lineo: 8
Code:
$ man bash
...
       trap [-lp] [[arg] sigspec ...]
              If  a  sigspec is EXIT (0) the command arg is executed on exit from
              the shell.  If a sigspec is DEBUG,  the  command  arg  is  executed
              before every simple command, for command, case command, select com‐
              mand, every arithmetic for command, and before  the  first  command
              executes  in  a shell function (see SHELL GRAMMAR above).  Refer to
              the description of the extdebug option to  the  shopt  builtin  for
              details  of  its effect on the DEBUG trap.  If a sigspec is RETURN,
              the command arg is executed each time a shell function or a  script
              executed with the . or source builtins finishes executing.

              If  a sigspec is ERR, the command arg is executed whenever a simple
              command has a non-zero exit status, subject to the following condi‐
              tions.   The ERR trap is not executed if the failed command is part
              of the command list immediately following a while or until keyword,
              part  of the test in an if statement, part of a command executed in
              a && or ⎪⎪ list, or if the command's return value is being inverted
              via !.  These are the same conditions obeyed by the errexit option.
I had the same problem, change the first line to "#!/bin/bash" in the script may solve this issue.
But if my script's first line is "#!/bin/sh", I execute "echo $SHELL", I also get the result "/bin/bash". So I don't know where is different between "#!/bin/sh" to "#!/bin/bash".
 
Old 03-26-2012, 08:34 AM   #13
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Please start a new thread for your question
 
Old 03-28-2012, 10:48 PM   #14
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,783

Rep: Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083
Quote:
Originally Posted by leaveinriver View Post
But if my script's first line is "#!/bin/sh", I execute "echo $SHELL", I also get the result "/bin/bash".
$SHELL doesn't tell you the current shell, it tells what shell the current user will login to.
Code:
% echo $SHELL
/usr/bin/zsh
% bash
~/tmp$ echo $SHELL
/usr/bin/zsh
~/tmp$ exit
exit
% dash
$ echo $SHELL
/usr/bin/zsh
 
  


Reply



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 09:22 AM
LXer: Use the Bash trap Statement to Clean Up Temporary Files LXer Syndicated Linux News 0 05-06-2009 01:00 AM
invisible chrs and no newline in bash after running script with 'trap 2' and 'read' SlackerJack Linux - General 3 08-11-2008 11:03 AM
Linux - Bash - Trap SIGTERM bigdogdan2 Programming 13 07-20-2004 10:54 AM
shell script - trap program termination dnijaguar Linux - Software 0 07-19-2003 12:27 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:39 PM.

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
Open Source Consulting | Domain Registration