Linux - NewbieThis 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
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.
Interesting problem. I have never used trap (although it has been on my todo list for a while) so I can't give you a fix, but I think I can see what goes wrong.
When you call dummy the error handler is invoked, but you are not in the context of the invoking call anymore, so the variable assignment never happens.
If you add
Code:
x=$v
to the end of the error handlers function body you get
Code:
user@localhost:~$ ./test.sh
1
x value is 1
0
z value is
Onviously not a fix, but it gives a pointer as to how the code behaves. I'll play a bit with this further...
So here is another alternative. Basically I do not like passing around global variables if I can help it. Also, I wasn't sure if we should be completely removing the logfile or just truncating?
My thought here was that you want the code to keep running but by removing it you have broken the link to the file and left a dangling exec with no closure.
So here is what I have:
Code:
logfile="/tmp/log.err"
exec 2>$logfile
_err_handler(){
local last_command
local var
last_command=$BASH_COMMAND
if [[ "$last_command" =~ = ]]
then
var=${last_command%=*}
read -r ${var} <<< $(cat $logfile|grep dummy|wc -l)
fi
:> $logfile
}
trap _err_handler ERR
x=$(dummy)
echo "x value is $x "
z=$(notrap)
echo "z value is $z"
Now you may wish to play with the regex but essentially it is just looking to see if the last command had an equals in it and so assigns back to whatever variable was being used.
So here is another alternative. Basically I do not like passing around global variables if I can help it. Also, I wasn't sure if we should be completely removing the logfile or just truncating?
My thought here was that you want the code to keep running but by removing it you have broken the link to the file and left a dangling exec with no closure.
So here is what I have:
Code:
logfile="/tmp/log.err"
exec 2>$logfile
_err_handler(){
local last_command
local var
variable assign
last_command=$BASH_COMMAND
if [[ "$last_command" =~ = ]]
then
var=${last_command%=*}
read -r ${var} <<< $(cat $logfile|grep dummy|wc -l)
fi
:> $logfile
}
trap _err_handler ERR
x=$(dummy)
echo "x value is $x "
z=$(notrap)
echo "z value is $z"
Now you may wish to play with the regex but essentially it is just looking to see if the last command had an equals in it and so assigns back to whatever variable was being used.
I learned something today. Didn't no either $BASH_COMMAND nor the trick to use read to do the variable assignment.
That said, I'll allow myself to point out that your code still ends up using global variables.
That said, I'll allow myself to point out that your code still ends up using global variables.
True in this case I pulled this from an old remnant of code I had where I was passing the variables in, but the beauty here is that you do not have to define what those variables are in the function
True in this case I pulled this from an old remnant of code I had where I was passing the variables in, but the beauty here is that you do not have to define what those variables are in the function
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.