ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
#!/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.
#!/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 .
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....
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???
$ 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.
$ 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!!!
$ 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".
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.