LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 04-13-2015, 02:04 PM   #1
vrusu
LQ Newbie
 
Registered: Oct 2010
Posts: 8

Rep: Reputation: 0
trap error and assign in bash script


hi
I try to assign return from a _error_handler_function to a variable
this is just a test( dummy test ) not a really case


#!/bin/bash
logfile="/dev/shm/log.err"
exec 2>$logfile
_err_handler(){
v=$(cat $logfile|grep dummy|wc -l);
rm $logfile
echo $v
}

trap _err_handler ERR

x=$(dummy);
echo "x value is $x "
z=$(notrap)
echo "z value is $z"


expected output :
bash-4.1$ sh test2.sh
x value is 1
z value is 0


what is wrong here and how should I fix?

thanks in advance

Last edited by vrusu; 04-13-2015 at 02:24 PM.
 
Old 04-13-2015, 03:02 PM   #2
joe_2000
Senior Member
 
Registered: Jul 2012
Location: Aachen, Germany
Distribution: Void, Debian
Posts: 1,016

Rep: Reputation: 308Reputation: 308Reputation: 308Reputation: 308
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...
 
Old 04-13-2015, 03:58 PM   #3
joe_2000
Senior Member
 
Registered: Jul 2012
Location: Aachen, Germany
Distribution: Void, Debian
Posts: 1,016

Rep: Reputation: 308Reputation: 308Reputation: 308Reputation: 308
How is this
Code:
#!/bin/bash
logfile="/tmp/log.err"
exec 2>$logfile
_err_handler(){
    v=$(cat $logfile|grep dummy|wc -l);
    rm $logfile
    LAST_V=$v
}

trap _err_handler ERR 

dummy
x=$LAST_V
echo "x value is $x "
notrap
z=$LAST_V
echo "z value is $z"
 
Old 04-13-2015, 09:15 PM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,999

Rep: Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190
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.
 
2 members found this post helpful.
Old 04-13-2015, 10:35 PM   #5
vrusu
LQ Newbie
 
Registered: Oct 2010
Posts: 8

Original Poster
Rep: Reputation: 0
thank you both!!

i'm wondering if there is a way to change _error_handler to also allow use directly in test condition:

[[ 0 -lt $(dummy) ]] && echo "is a dummy (test)"

instead of assignment and test
x=$(dummy);
[[ 0 -lt $x ]] && echo " has a dummy"

Last edited by vrusu; 04-13-2015 at 10:43 PM.
 
Old 04-13-2015, 10:53 PM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,999

Rep: Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190
I am not sure I follow? Why not just test $? after the assignment and if errored (> 0) then do what you need?

Overall it may help if you explained what you actually need as grepping the error in a file when you know it will happen seems a little pointless.
 
Old 04-14-2015, 12:06 PM   #7
joe_2000
Senior Member
 
Registered: Jul 2012
Location: Aachen, Germany
Distribution: Void, Debian
Posts: 1,016

Rep: Reputation: 308Reputation: 308Reputation: 308Reputation: 308
Quote:
Originally Posted by grail View Post
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.
 
Old 04-15-2015, 12:20 AM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,999

Rep: Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190
Quote:
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
 
1 members found this post helpful.
Old 04-15-2015, 07:58 AM   #9
joe_2000
Senior Member
 
Registered: Jul 2012
Location: Aachen, Germany
Distribution: Void, Debian
Posts: 1,016

Rep: Reputation: 308Reputation: 308Reputation: 308Reputation: 308
Quote:
Originally Posted by grail View Post
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
Agreed
 
  


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
[SOLVED] What is the best way to trap an error in a script AntBla Linux - Newbie 8 02-21-2015 09:40 PM
Trap MySQL errors on remote server using bash script linuxlover.chaitanya Linux - General 0 06-13-2014 05:44 PM
[SOLVED] Using trap in bash script sree_ec Programming 13 03-28-2012 10:48 PM
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
bash script Want to capture return key and assign a value procfs Programming 9 07-07-2006 01:38 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:57 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
Open Source Consulting | Domain Registration