LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 11-25-2011, 01:12 PM   #1
Efo
LQ Newbie
 
Registered: Nov 2004
Distribution: Gentoo
Posts: 22

Rep: Reputation: 1
bash + mailx question


Hi all,

I am working on a bash script that mails the outcome of a command.
Here is what I have so far:

************************
#!/bin/bash

# Setting
SUBJECT=$1
SMS_ADDRESS="localhos@localdomain"

# Check arguments
if [ $# == 1 ]
then
# Write starting date
BODY=$BODY"\n"`date`
# Execute command
exec $1 &
# Check for errors
if [ $? -gt 0 ]; then
BODY=$BODY"\n***ERROR***"
else
BODY=$BODY"\n***DONE***"
fi
# Write ending date
BODY=$BODY"\n"`date`
# Send sms
echo -e "$BODY" | mailx -i -s "$SUBJECT" "$SMS_ADDRESS"
echo $?
else
echo "Only one argument accepted (use quotation marks)"
fi
****************************

If the executed command is successful (e.g. $1=ls), it send the mail and the signal at "echo $?" is 0, however, is the command doesnt exists (e.g. $1=junk) it doesnt send the mail and the signal is 127.

I think this is due to the signal generated by the command, which is non-zero if it fails. How do I force mailx to send the mail regardless of signals previously occurred in the script?

Thanks a bunch!

Efo
 
Old 11-27-2011, 09:40 AM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
Please use [code][/code] tags around your code, to preserve formatting and to improve readability.

And please take some time to format your script a bit more. Add some blank spaces between logical sections. But at least you're commenting.
Code:
# Execute command
exec $1 &
# Check for errors
if [ $? -gt 0 ]; then
You have to wait until a command finishes before you can test its exit code (naturally). But when you execute a command in the background you are telling the shell to fork off a new process and continue on to the next command immediately. The subsequent test is then run before the result of the previous command is known.

Instead of using exec and &, try simply running the command in a subshell or grouped command block.

Code:
# Execute command
{ $1 ;}		# or ( $1 )
# Check for errors
if (( $? > 0 )); then
Bash's ((..)) arithmetic evaluation is recommended for numeric tests like this, BTW.

However, I don't think it's a good idea to transfer a whole command in a single parameter. Storing code in variables is a tricky thing and not generally recommended. It may be safer if you just use $@ instead to pass it the entire argument list as the command to launch, and forget about quoting it.

http://mywiki.wooledge.org/BashFAQ/050

Finally, $(..) is highly recommended over `..`
 
Old 11-27-2011, 11:17 AM   #3
Efo
LQ Newbie
 
Registered: Nov 2004
Distribution: Gentoo
Posts: 22

Original Poster
Rep: Reputation: 1
Hi David the H.,

Thank you for replying and the good tips.
I modified the code to include your suggestion, and indeed the signal is now 0. However, for some reason it still does not mail the message when the command fails. It may have something to do with mailx....
 
  


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 On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
TO ANYONE USING MAILX (mailx 12.1). stf92 Linux - Software 22 07-08-2010 09:44 PM
[SOLVED] bash: how to redirect to file verbose output of mailx? val3xiv Linux - General 3 02-26-2009 07:02 PM
mailx pramka Suse/Novell 3 05-01-2008 04:18 PM
mailx csaunders Red Hat 1 05-23-2007 08:55 AM
mailx question bubba112557 Suse/Novell 1 12-17-2006 09:50 PM


All times are GMT -5. The time now is 09:05 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration