Re: proper syntax for checking values of multiple variables
- From: Ed Morton <morton@xxxxxxxxxxxxxx>
- Date: Wed, 22 Aug 2007 10:04:51 -0500
Glenn Jackman wrote:
At 2007-08-22 09:01AM, "Billy Patton" wrote:
I have this sub routine I'm calling to check for required arguments for a script.
/bin/bash
Linux
check_required () {
if [ "$tech" == '' ] ; then pod2text $0; printf " -tech is required!\n" ; exit 1; fi;
if [ "$process" == '' ] ; then pod2text $0; printf " -process is required!\n" ; exit 1; fi;
if [ "$cell" == '' ] ; then pod2text $0; printf " -cell is required!\n" ; exit 1; fi;
ERROR IS HERE if [ "$gds" == '' && "$oasis" == '' && "$laff" == '' && "$lef" == '' && "$def" == '' ] ; then
pod2text $0;
printf "must have one of -gds , -laff , -oasis , -lef , -def\n";
exit 1;
fi;
}
On the line above where I have 'ERROR IS HERE' my xterm is complaining about :
line 41: [: missing `]'
I think it is probably the && that is killing it.
How do I put the syntax so that 1 of the 5 variables must be set?
I know I can do it with if then else for each of them, but something like IO have reads much better.
You'll want to look up the 'test' command in the bash man page, where
you'll find:
expr1 -a expr2
True if both expr1 and expr2 are true.
expr1 -o expr2
True if either expr1 or expr2 is true.
So:
if [ ! ( "$gds" -o "$oasis" -o "$laff" -o "$lef" -o "$def" ) ]; then
# error
fi
and the test for equality is "=", not "==", plus to test for an empty variable use -z "$var" instead of just "$var", and ( ... ) may not be supported by your shell, and instead of testing for a negative "NOT (populated)" it's clearer to test for a positive "empty", and if you just want to test for all of them being empty, you don't need to test them all individually so all things considered I'd go with just:
if [ -z "$gds$oasis$laff$lef$def" ]; then
# error
fi
and get rid of all those redundant semicolons at the end of every line - they'll come back to bite you one day.
Ed.
.
- Follow-Ups:
- References:
- proper syntax for checking values of multiple variables
- From: Billy Patton
- Re: proper syntax for checking values of multiple variables
- From: Glenn Jackman
- proper syntax for checking values of multiple variables
- Prev by Date: Re: file compare script
- Next by Date: multiple pipes and tail -f
- Previous by thread: Re: proper syntax for checking values of multiple variables
- Next by thread: Re: proper syntax for checking values of multiple variables
- Index(es):
Relevant Pages
|