<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: test the return code. in Operating System - HP-UX</title>
    <link>https://community.hpe.com/t5/operating-system-hp-ux/test-the-return-code/m-p/4988421#M780541</link>
    <description>#!/sbin/sh&lt;BR /&gt;file=/tmp/core_file_list&lt;BR /&gt;grep 'core' $file&lt;BR /&gt;if [ $? -eq 0 ]; then&lt;BR /&gt;  cat $file | mailx -s find_cores_output chuck&lt;BR /&gt;else&lt;BR /&gt;  mailx -s no_cores chuck&lt;BR /&gt;fi&lt;BR /&gt;/usr/sbin/sendmail -q</description>
    <pubDate>Fri, 30 Jun 2006 10:37:17 GMT</pubDate>
    <dc:creator>spex</dc:creator>
    <dc:date>2006-06-30T10:37:17Z</dc:date>
    <item>
      <title>test the return code.</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/test-the-return-code/m-p/4988418#M780538</link>
      <description>&lt;!--!*#--&gt;Ok here is the scenario...&lt;BR /&gt;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.&lt;BR /&gt;&lt;BR /&gt;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.&lt;BR /&gt;&lt;BR /&gt;What I have so far is as follows but doesn't work.&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;#!/sbin/sh&lt;BR /&gt;file=/tmp/core_file_list&lt;BR /&gt;return_value=`cat $file |grep -i core`&lt;BR /&gt;if return_value=0&lt;BR /&gt;    then&lt;BR /&gt;       cat $file | mailx -s find_cores_output chuck&lt;BR /&gt;       /usr/sbin/sendmail -q&lt;BR /&gt;    else&lt;BR /&gt;       mails -s no_cores chuck&lt;BR /&gt;       /usr/sbin/sendmail -q&lt;BR /&gt;fi&lt;BR /&gt;&lt;BR /&gt;#rm $file&lt;BR /&gt;&lt;BR /&gt;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.&lt;BR /&gt;&lt;BR /&gt;This shouldn't be that hard, but can't figure it out.&lt;BR /&gt;&lt;BR /&gt;Quick points.</description>
      <pubDate>Fri, 30 Jun 2006 10:08:07 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/test-the-return-code/m-p/4988418#M780538</guid>
      <dc:creator>Charles Holland</dc:creator>
      <dc:date>2006-06-30T10:08:07Z</dc:date>
    </item>
    <item>
      <title>Re: test the return code.</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/test-the-return-code/m-p/4988419#M780539</link>
      <description>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:&lt;BR /&gt;&lt;BR /&gt;return_value=`cat $file |grep -i core; echo $?`</description>
      <pubDate>Fri, 30 Jun 2006 10:17:57 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/test-the-return-code/m-p/4988419#M780539</guid>
      <dc:creator>Ivan Ferreira</dc:creator>
      <dc:date>2006-06-30T10:17:57Z</dc:date>
    </item>
    <item>
      <title>Re: test the return code.</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/test-the-return-code/m-p/4988420#M780540</link>
      <description>return_value=`cat $file |grep -i core`&lt;BR /&gt;&lt;BR /&gt;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).&lt;BR /&gt;&lt;BR /&gt;typeset file=/tmp/core_file_list&lt;BR /&gt;typeset -i return_value=0&lt;BR /&gt;grep -q -i "core" ${file} # no cat needed &lt;BR /&gt;return_value=${?}&lt;BR /&gt;if [[ ${return_value} -eq 0 ]]&lt;BR /&gt;  then&lt;BR /&gt;    echo "core(s) found"&lt;BR /&gt;  else&lt;BR /&gt;    echo "did not find no cores nohow"&lt;BR /&gt;  fi&lt;BR /&gt;&lt;BR /&gt;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.&lt;BR /&gt;&lt;BR /&gt;If you need the exit status of a pipe&lt;BR /&gt;return_value=$(cat ${file} | grep -q -i "core"),&lt;BR /&gt;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.&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Fri, 30 Jun 2006 10:36:20 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/test-the-return-code/m-p/4988420#M780540</guid>
      <dc:creator>A. Clay Stephenson</dc:creator>
      <dc:date>2006-06-30T10:36:20Z</dc:date>
    </item>
    <item>
      <title>Re: test the return code.</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/test-the-return-code/m-p/4988421#M780541</link>
      <description>#!/sbin/sh&lt;BR /&gt;file=/tmp/core_file_list&lt;BR /&gt;grep 'core' $file&lt;BR /&gt;if [ $? -eq 0 ]; then&lt;BR /&gt;  cat $file | mailx -s find_cores_output chuck&lt;BR /&gt;else&lt;BR /&gt;  mailx -s no_cores chuck&lt;BR /&gt;fi&lt;BR /&gt;/usr/sbin/sendmail -q</description>
      <pubDate>Fri, 30 Jun 2006 10:37:17 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/test-the-return-code/m-p/4988421#M780541</guid>
      <dc:creator>spex</dc:creator>
      <dc:date>2006-06-30T10:37:17Z</dc:date>
    </item>
    <item>
      <title>Re: test the return code.</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/test-the-return-code/m-p/4988422#M780542</link>
      <description>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.&lt;BR /&gt;&lt;BR /&gt;FILE=/tmp/core_file_list&lt;BR /&gt;&lt;BR /&gt;if [ $(grep -c -i "core" ${FILE}) -ne 0 ]&lt;BR /&gt;then&lt;BR /&gt;  cat ${FILE} | mailx -s find_cores_output chuck&lt;BR /&gt;fi&lt;BR /&gt;&lt;BR /&gt;rm ${FILE}</description>
      <pubDate>Fri, 30 Jun 2006 10:46:29 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/test-the-return-code/m-p/4988422#M780542</guid>
      <dc:creator>Jeff_Traigle</dc:creator>
      <dc:date>2006-06-30T10:46:29Z</dc:date>
    </item>
    <item>
      <title>Re: test the return code.</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/test-the-return-code/m-p/4988423#M780543</link>
      <description>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.&lt;BR /&gt;&lt;BR /&gt;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.&lt;BR /&gt;&lt;BR /&gt;Ivan  thanks for your input&lt;BR /&gt;&lt;BR /&gt;Thanks to all</description>
      <pubDate>Fri, 30 Jun 2006 12:42:13 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/test-the-return-code/m-p/4988423#M780543</guid>
      <dc:creator>Charles Holland</dc:creator>
      <dc:date>2006-06-30T12:42:13Z</dc:date>
    </item>
  </channel>
</rss>

