On Jun 8, 2006, at 8:49 AM, John Oliver wrote:

I want to grep for a given value, and if that value is found do X, and
if it isn't found, do Y.  How do I do that?

The magic of return codes.  From the grep manpage:

DIAGNOSTICS
Normally, exit status is 0 if selected lines are found and 1 otherwise. But the exit status is 2 if an error occurred, unless the -q or -- quiet
   or --silent option is used and a selected line is found.


-----
#!/bin/sh
# look for "foo"

# stuff the output into /dev/null, since we don't
# really care about it anyway
grep foo /etc/hosts > /dev/null 2>&1

# Put grep's return code someplace safer than $?
rc=${?}

if [ ${rc} -eq 0 ]; then
    # we found what we wanted
    # do X
else
    # we didn't find what we wanted
    # do Y
fi
-----

Enjoy! :)

Gregory

--
Gregory K. Ruiz-Ade <[EMAIL PROTECTED]>
OpenPGP Key ID: EAF4844B  keyserver: pgpkeys.mit.edu


Attachment: PGP.sig
Description: This is a digitally signed message part

-- 
[email protected]
http://www.kernel-panic.org/cgi-bin/mailman/listinfo/kplug-list

Reply via email to