[prog] shell script problem

Conor Daly conor.daly-linuxchix at cod.homelinux.org
Mon Dec 5 03:37:16 EST 2005


On Wed, Nov 30, 2005 at 09:37:09AM +0000 or so it is rumoured hereabouts, 
Noir thought:
> I am writing this script to email me the output of a
> program. 
> 
> When I run the program (/usr/bin/program) it gives me
> a small output in <STDIN>. Basically, my bash script
> looks for an "okay" and if it finds it, it then email
> me the whole output <STDIN> with the subject header of
> the mail as $SUB1 else it sends me an email with
> subject as $SUB2 and the whole <STDIN> output.

Why are you searching in the output?  Can you use the program's exit
status to decide whether you have a failure or not.  If /usr/bin/program
returns useable status when run, you can use the $? variable to decide
what to do next:

#!/bin/bash
SUB1="program success"
SUB2="program failure"
/usr/bin/program .... > tmpfile.tmp 
if [ $? -eq 0 ]; then
  mail -s $SUB1 user at example.com < tmpfile.tmp
else
  mail -s $SUB2 user at example.com < tmpfile.tmp
fi
  
$? always contains the result of the last command run.  Normally program
success returns a 0 while failure returns non-zero.  It depends on the
particular program whether you can usefully use it since the absence of
'okay' in your program's output may not be considered failure.  However,
all is not lost.  A grep for 'okay' in the tmpfile.tmp can be used with $?
to determine success also.

Conor {there's alway more than one way to do it...}
-- 
Conor Daly <conor.daly at cod.homelinux.org>
-----BEGIN GEEK CODE BLOCK-----
Version: 3.1
GCS/G/S/O d+(-) s:+ a+ C++(+) UL++++ US++ P>++ L+++>++++ E--- W++ !N
PS+ PE Y+ PGP? tv(-) b+++(+) G e+++(*) h-- r+++ z++++ 
------END GEEK CODE BLOCK------


More information about the Programming mailing list