Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1441
  • Last Modified:

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
0
XOracle
Asked:
XOracle
  • 2
  • 2
  • 2
  • +1
2 Solutions
 
evilrixSenior Software Engineer (Avast)Commented:
Try: -

echo | mailx -s "The ${header_dat_file} file contained errors." me@domain.com
0
 
omarfaridCommented:
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now