- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- test the return code.
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-30-2006 03:08 AM
06-30-2006 03:08 AM
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.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-30-2006 03:17 AM
06-30-2006 03:17 AM
Re: test the return code.
return_value=`cat $file |grep -i core; echo $?`
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-30-2006 03:36 AM
06-30-2006 03:36 AM
SolutionFirst 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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-30-2006 03:37 AM
06-30-2006 03:37 AM
Re: test the return code.
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-30-2006 03:46 AM
06-30-2006 03:46 AM
Re: test the return code.
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-30-2006 05:42 AM
06-30-2006 05:42 AM
Re: test the return code.
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