Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

if command fails? 1

Status
Not open for further replies.

jouell

MIS
Nov 19, 2002
304
US
How do I do:

if [command fails];then
something
fi

I am missing the syntax in the man page I think.

I'd like to not use the command || command2 syntax...

Thanks!

 
Either:
[ command fails ] || {
something
}
Or:
if [command fails]; then :; else
something
fi

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
So I would do:


[ grep -i word $variable ] || {
echo word not in $variable
}

if I understand correctly?
 
Provided $variable expand to a valid filename:
grep -i word $variable || echo word not in $variable

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
use quotes for your filename as it might contain blanks/newLines etc...

grep -i word "$variable" || echo word not in $variable

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
If you search if variable (as text string) contains word:
case $variable in *word*) :;; *) echo word not in $variable;; esac

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
If you're using Bash, you can write:
Code:
if ! command;
then
    do something
fi
That's probably a Bash-specific extension, though.

For example,
Code:
if ! false;
then
    echo cool
fi
outputs [tt]cool[/tt].

Note that [tt]if[/tt] takes any command as an argument. The [tt][[/tt] command is simply the most commonly used one.
 
Thanks all

I like this the best:

from cdlvj (MIS) Jan 18, 2005

if [ $? != 0 ]

----------------------------------

So I will do


if [ ! -n $EXPORT_DIR ];then
echo EXPORT_DIR is $EXPORT_DIR - not set - Abort! | tee -a $LOGFILE
exit 1;
fi


if [ ! -d $EXPORT_DIR ];then
echo EXPORT_DIR is $EXPORT_DIR - invalid directory - Abort! | tee -a $LOGFILE
exit 1;
fi

if [ $EXPORT_DIR = "/" ];then
echo EXPORT_DIR is / - Abort! | tee -a $LOGFILE
exit 1;
fi


grep -i exports $EXPORT_DIR

if [ $? != 0 ];then
echo exports not found in $EXPORT_DIR - Abort! | tee -a $LOGFILE
exit 1;
fi



As four checks before running my rm statement:

find $EXPORT_DIR -ctime +$DAYS_OLD -name \*.gz -exec rm -e {} \; 2>> $LOGFILE


-john
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top