LinuxQuestions.org
Review your favorite Linux distribution.
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-26-2011, 04:12 AM   #1
m3phisto
Member
 
Registered: Mar 2010
Posts: 47

Rep: Reputation: 15
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
 
Old 04-26-2011, 04:59 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,500

Rep: Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867
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
 
Old 04-26-2011, 05:58 AM   #3
m3phisto
Member
 
Registered: Mar 2010
Posts: 47

Original Poster
Rep: Reputation: 15
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
 
Old 04-26-2011, 07:03 AM   #4
m3phisto
Member
 
Registered: Mar 2010
Posts: 47

Original Poster
Rep: Reputation: 15
problem solved:


Code:
if [ -f $file ]
  then
     ERROR=$( { > $file; } 2>&1 )
     echo $ERROR > $message
fi
 
Old 04-26-2011, 07:28 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,500

Rep: Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867
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
 
  


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] problem in handling arrays in bash himu3118 Programming 8 07-06-2010 04:02 AM
Handling newlines in Bash MS3FGX Programming 7 09-10-2007 10:15 PM
Bash Script and Loop error handling Kedelfor Programming 5 05-22-2005 02:22 PM
Signal handling from bash shy Programming 2 11-20-2004 08:41 AM
handling whitespaces in bash guerilla fighta Linux - General 2 01-25-2003 04:12 PM


All times are GMT -5. The time now is 10:25 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration