1833313 Members
3215 Online
110051 Solutions
New Discussion

test the return code.

 
SOLVED
Go to solution
Charles Holland
Trusted Contributor

test the return code.

Ok here is the scenario...
Have a script on each server that runs every morning looking for "core" files. The script builds a small file that contains information about the system it runs on, if any core files exist and where they are located and then emails it to me. The majority of the email contains nothing but system information and nothing about core files, as no core files exist in the file systems.

I've tried to modify the last part of the script to test if the word core is in the file. If it is I want the script to cat the file and send it to my email. If the file doesn't contain the word core then drop through and do not mail anything. But in both cases remove the file created from the file system.

What I have so far is as follows but doesn't work.


#!/sbin/sh
file=/tmp/core_file_list
return_value=`cat $file |grep -i core`
if return_value=0
then
cat $file | mailx -s find_cores_output chuck
/usr/sbin/sendmail -q
else
mails -s no_cores chuck
/usr/sbin/sendmail -q
fi

#rm $file

I know that the return code is 0 if something is found. In the finished product there would be nothing done between the else and fi.

This shouldn't be that hard, but can't figure it out.

Quick points.
"Not everything that can be counted counts, and not everything that counts can be counted" A. Einstein
5 REPLIES 5
Ivan Ferreira
Honored Contributor

Re: test the return code.

The return code is 0 if it's found, but there, you are setting the return_value variable with the ouput of the command, not the return code. The return code is obtained by the $? environment variable, so your code should look like:

return_value=`cat $file |grep -i core; echo $?`
Por que hacerlo dificil si es posible hacerlo facil? - Why do it the hard way, when you can do it the easy way?
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: test the return code.

return_value=`cat $file |grep -i core`

First of all, all you care about is the exit code so you do not need stdout just $? (and let's improve your coding style whiule we're at it).

typeset file=/tmp/core_file_list
typeset -i return_value=0
grep -q -i "core" ${file} # no cat needed
return_value=${?}
if [[ ${return_value} -eq 0 ]]
then
echo "core(s) found"
else
echo "did not find no cores nohow"
fi

Note that the -q option is quiet; all we want is the exit status. Notice that I also captured ${?} in a variable so that if it needed later, you have it.

If you need the exit status of a pipe
return_value=$(cat ${file} | grep -q -i "core"),
note that return_value acrtually contains the stdout of the command; ${?} is the exit status of the last command in the pipeline, ie the exit status of grep in this case.

If it ain't broke, I can fix that.
spex
Honored Contributor

Re: test the return code.

#!/sbin/sh
file=/tmp/core_file_list
grep 'core' $file
if [ $? -eq 0 ]; then
cat $file | mailx -s find_cores_output chuck
else
mailx -s no_cores chuck
fi
/usr/sbin/sendmail -q
Jeff_Traigle
Honored Contributor

Re: test the return code.

You've gotten good responses on what you were doing wrong in trying to use the return code. Another slightly modified approach that avoids using the return code at all is to use the -c option on grep, which returns the number of matching lines.

FILE=/tmp/core_file_list

if [ $(grep -c -i "core" ${FILE}) -ne 0 ]
then
cat ${FILE} | mailx -s find_cores_output chuck
fi

rm ${FILE}
--
Jeff Traigle
Charles Holland
Trusted Contributor

Re: test the return code.

The responses from A. Clay and Jeff both worked right out of the box. A. Clays was a bit more in depth, and had to look up what typeset was, but I got it.

Spex - Yours was the most easy for me to read, but when executed I had the following results. If the file did *not* contain the word core the script would hang. Pressing Ctrl-C gave me the message (Interupt -- one more to kill letter) Pressing Ctrl-C again brought me back to the prompt. If the file did contain the word core everything worked fine.

Ivan thanks for your input

Thanks to all
"Not everything that can be counted counts, and not everything that counts can be counted" A. Einstein