Operating System - HP-UX
1847858 Members
2106 Online
104021 Solutions
New Discussion

Re: If then statement help

 
Mel_12
Advisor

If then statement help

Hi, I am still learning shell programming: Please help me with this script. It keeps returning true even when there is no string "oracle" inside the file.

if [ 'cat /export/home/mduru/vxdisk.list|grep oracle' > /dev/null ]

then mailx -s "unix mail test" melduru@yahoo.com st
else echo "no listing"
fi;

Thanks
Mel

8 REPLIES 8
Patrick Wallek
Honored Contributor

Re: If then statement help

How about something like:

COUNT=$(grep -c oracle /export/home/mduru/vxdisk.list)

if [ "${COUNT}" > 0 ] ; then
mailx -s "unix mail test" melduru@yahoo.com else
echo "nno listing"
fi
Sridhar Bhaskarla
Honored Contributor

Re: If then statement help

Couple of ways.

1. Getting the return value of the previous command and testing it for success.

grep -q oracle /export/home/mduru/vxdisk.list
RESP=$?
if [ $RESP = 0 ]
then
mailx -s ....
else
echo "no listing"
fi

2. Directly testing the command with if

if grep -q oracle /export/home/mduru/vxdisk.list
then
mailx -s ...
else
echo "no listing"
fi

There are few other ways. I like the first way as it is more readable to me.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Patrick Wallek
Honored Contributor

Re: If then statement help

No points for the first response please -- I don't think its gonna work. This should though:

COUNT=$(grep -c oracle /export/home/mduru/vxdisk.list)

if (( ${COUNT} > 0 )) ; then
mailx -s "unix mail test" melduru@yahoo.com else
echo "nno listing"
fi
Mel Burslan
Honored Contributor

Re: If then statement help

FILE=/export/home/mduru/vxdisk.list
count=`grep -q oracle $FILE | wc -l`
if [ $count -gt 0 ]
then
mailx -s "unix mail test" melduru@yahoo.com < $FILE
else
echo "Nothing found to mail"
fi


hope this helps
________________________________
UNIX because I majored in cryptology...
Mel_12
Advisor

Re: If then statement help

Thanks guys for your quick response. I have assigned points based upon the usefullness of the replies. I wanted a single line command as presented by Sridhar and it worked beautifully. I had to strike the -q from the grep as the system shouted foul play "illegal use of ...

Regards,
Mel
Bill Hassell
Honored Contributor

Re: If then statement help

grep -q doesn't work? What version of HP-UX are you using? -q has been around for a long time (at least a decade). What does your man page for grep say about -q?

For readability (and to avoid using an obsolete and deprecated shell constructs, the grave accents), here's a simpler version:

MYFILE=/export/home/mduru/vxdisk.list
if $(grep -q $MYFILE)
then
mailx -s "stuff.." me@yahoo.com < $MYFILE
else
echo "no listing"
fi

See man sh-posix or man ksh concerning grave accents. Note that $(...) is also faxable whereas grave accents are almost always lost or copied incorrectly.


Bill Hassell, sysadmin
Sridhar Bhaskarla
Honored Contributor

Re: If then statement help

Hi (Again),

If you are really interested in one line, you can actually use

grep -q oracle /export/home/mduru/vxdisk.list && mailx -s "unix mail test" melduru@yahoo.com < /export/home/mduru/vxdisk.list

Looking at /export structure, I feel yours is a SUN/Solaris system. grep -q is equivalent to " > /dev/null 2>&1".

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Mel_12
Advisor

Re: If then statement help

Sridhar!!!!!! is a natural. I am impressed you were able to decode my solaris system usage. Not to offend anyone, I support both platforms, HP and Solaris under the capacity of Oracle DBA. Bill, please go easy on some of us. Afterall, you have been fully decorated with all those points etc.... Again, I am impressed with the timely responses on this forum.

Regards,
Mel