PDA

View Full Version : bash: if fail, then exit code



newbie22
March 19th, 2007, 09:28 AM
if [ $1 = "" ]; then echo "fail"; exit 0; fi
The code needs a $1 entry in order to work. Without it, the code will just blur out errors. If failed to provide a $1 entry, I want the script to exit instead of blurring out the errors.

What needs fixing in the above code?

cwaldbieser
March 19th, 2007, 09:57 AM
if [ $1 = "" ]; then echo "fail"; exit 0; fi
The code needs a $1 entry in order to work. Without it, the code will just blur out errors. If failed to provide a $1 entry, I want the script to exit instead of blurring out the errors.

What needs fixing in the above code?



if [ -z "$1" ]; then echo "fail"; exit 0; fi

Try "man test" or "man [" for more info on the types of tests you can do.

Jussi Kukkonen
March 19th, 2007, 09:58 AM
An non-existing variable is not the same as "". You can bypass this with a trick:

if [ x$1 = x"" ]; then
echo "fail";
exit 0;
fi

cwaldbiesers suggestion is better in this case, but my code works even if you actually wanted to compare to something:
if [ x$1 = x$COMP_VAR ];