Script waiting for CTRL+D command after mailx

I'm trying to put together a simple script where I send myself an email if an error occurs. I have put together the following code, but when I kick off the shell script from UNIX, it just hangs and waits for me to hit CTRL+D to sned the message. What am I doing wrong here?

if [ ${?} -ne 0 ]
                      then
                        echo "There was an error"
                        mailx -s "The ${header_dat_file} file contained errors." me@domain.com
                        exit -1
           fi
XOracleAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
evilrixConnect With a Mentor Senior Software Engineer (Avast)Commented:
Try: -

echo | mailx -s "The ${header_dat_file} file contained errors." me@domain.com
0
 
omarfaridConnect With a Mentor Commented:
Hi,

Change it to

if [ ${?} -ne 0 ]
then
       echo "There was an error" |  mailx -s "The ${header_dat_file} file contained errors." me@domain.com
       exit -1
fi

The mailx was waiting for the message body, but with pipe it will take it from echo

0
 
evilrixSenior Software Engineer (Avast)Commented:
I'm sure I've just answer this already!
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
omarfaridCommented:
Hi,

Don't worry :)
0
 
XOracleAuthor Commented:
Thanks to both of you. I was actually about to ask why the pipe was necessary, but omar offered the explanation. Makes sense now.

Thanks again!
0
 
TintinCommented:
I'd recommend that you don't use negative exit codes as that breaks the usual Unix convention of 0=success, >0=failure
0
 
XOracleAuthor Commented:
I'm afraid that doesn't make too much sense to me right now. The bulk of my code is coming from cut-and-paste right now, but I am trying to learn as much as I can. I've purchased a couple of books on scripting, but I am still on the basics.

Thanks again,
XOracle
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.