The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM



View Single Post in UNIX Forums - Click on the Thread or Permalink to View Entire Thread -->
  #3 (permalink)  
Old 05-17-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,650
That's a Useless Use of Test $?. if already examines the exit code $? from the command it executes, so it's simpler and more straightforward to do

Code:
if egrep -i "string" file_name
then
    echo Found
else
    return $?
fi
To negate an if, use an exclamation mark.

Code:
if ! egrep -i "string" file_name
then
    return $?
fi
There's another shorthand you should know about: the "or" connective. It executes the second command if the first command fails.

Code:
egrep -i "string" file_name >/dev/null || return $?
There is also && "and" which operates the other way around.

(I added redirection to /dev/null as egrep is used simply for its return value here. You don't want it to actually print any matches. If your egrep has the -q option, you could use that too.)
Reply With Quote