shell script and mailx

Posted on 2005-04-13
Last Modified: 2013-12-26
making a script to send a alert, can't quit figure out what to put in the  [ ] below
command work ok if done on command line, just can't put it into a script the right way


su - pmx -c '/opt/pmx4/bin/pmx status' | grep -i dead
if [ echo = dead ]; then
  mailx -s "pmx is dead ." 777-777-7777\;

Question by:bt707

    Author Comment

    one other note,
    when i run the maix comand from the command line I have to
    use a ctrl-d to send the message, not sure how to do that
    part in a script either
    LVL 20

    Accepted Solution

    su - pmx -c '/opt/pmx4/bin/pmx status' | grep -i dead
    if [ $? -eq 0 ]; then
      mailx -s "pmx is dead ." 777-777-7777\ < /dev/null

    $? will be 0 if the last command (grep) returned "success" i.e. it found the text you were looking for

    < /dev/null could also be < any_file, to tell mailx to use that file as the body of the message

    Author Comment


    that just about did it, one problem though, when i run it this way i still have the same
    problem, when it gets to the  mailx command it just hangs waiting for more info,
    when i hit the crtl-d it then finishes and sends the message,

    how can i end this with a crt-d somehow so I don't have to do it maually.

    LVL 20

    Expert Comment

    Oops, taking input from /dev/null will never supply and EOF (== Ctrl-d ) - Supply a real file, e.g. /etc/issue, or some dummy text:

    mailx -s "xx" < yourfile

    or even
    ls | mailx -s "xx"
    LVL 48

    Expert Comment

    or simply

    echo | mailx -s "subject"

    However, using

    mailx -s "subject" </dev/null

    works on Solaris and Linux (with the mail command).

    What Unix flavour do you have bt707?

    Author Comment

    using solaris 8,   checking things again, works just fine when i run the script other than it wait for me to do
    a ctrl-d before it sends out.

    Author Comment

    not sure what i did the first time, i think i left off the   < /dev/null

    anyway I went back and recreated it just as tfewster had put it the in the first place
    and all is working just fine.

    Thanks a lot all,

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
    This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
    This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now