Latest LQ Deal: Linux Power User Bundle
Go Back > Forums > Linux Forums > Linux - General
User Name
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.


  Search this Thread
Old 11-25-2011, 01:12 PM   #1
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:


# Setting

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

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!

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: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959
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.
# 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.

# 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.

Finally, $(..) is highly recommended over `..`
Old 11-27-2011, 11:17 AM   #3
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....


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 / openSUSE 3 05-01-2008 04:18 PM
mailx csaunders Red Hat 1 05-23-2007 08:55 AM
mailx question bubba112557 SUSE / openSUSE 1 12-17-2006 09:50 PM

All times are GMT -5. The time now is 08:28 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration