LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   bash error handling (https://www.linuxquestions.org/questions/linux-newbie-8/bash-error-handling-877118/)

m3phisto 04-26-2011 04:12 AM

bash error handling
 
hi there...

i'm currently working on some error handling in my scripts...

for example i tried to get the "permission denied" from on of my scripts (if it fails) to be send in a mail...


here is what i'm trying to do...

Code:

message="/gfsdisk/scripts/autoresponder.log"

if [ -f $file ]
  then

    true > $file &> $message

fi



here is the mail code:

Code:


trap 'errormail' 1 2 3 6 9 14 15 ERR

errormail()
 {
        for host in ${mailaddr[@]}; do
                /bin/mail -s "$subject" $host < "$message"
        done
echo mail...
 }



if i run the script as root... i don't get any permission denied replys...
when i run it as a non priviledged user i get a "permission denied" messige, but it'S not written in the log file...


what am i doing wrong`?
thx a lot

grail 04-26-2011 04:59 AM

I am not sure how the above all jells together, but do you understand what the following line is doing?
Code:

true > $file &> $message
Maybe you should try the following test on the command line:
Code:

echo hello > file1 &> file2

m3phisto 04-26-2011 05:58 AM

as far as i know "ture > $file" makes $file a blank file
and "&> $message" writes the standart output and error message, if there is any in the $message file

m3phisto 04-26-2011 07:03 AM

problem solved:


Code:

if [ -f $file ]
  then
    ERROR=$( { > $file; } 2>&1 )
    echo $ERROR > $message
fi


grail 04-26-2011 07:28 AM

Quote:

and "&> $message" writes the standart output
Yes and so if $message had anything in it previously it will get blown away just as $file will.

As you have your solution though ... good luck


All times are GMT -5. The time now is 11:28 PM.