Re: proper syntax for checking values of multiple variables



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.
.



Relevant Pages

  • Re: proper syntax for checking values of multiple variables
    ... On the line above where I have 'ERROR IS HERE' my xterm is complaining ... True if both expr1 and expr2 are true. ...
    (comp.unix.shell)
  • Re: find and NFS mounts
    ... This is taken from the findmanpage on a Slackware 11.0 system. ... the directory tree rooted at each given file name by evaluating the ... Same as expr1 expr2. ...
    (comp.os.linux.misc)
  • Compare results of two expressions within query
    ... I can display the results in the query in two columns without a problem. ... What I would like to do is, in the same query, have a single column that combines these answers, using a third If expression, and not show the results of the first two: ... I'm looking into doing Expr1 and Expr2 within a subquery of ExprTotal, but not sure if this would work. ...
    (microsoft.public.access.queries)
  • Re: Simple string search question
    ... I have a long STR and two EXPR (EXPR1, EXPR2) which are set; ... This is "an occurrence of characters" between EXPR1 and EXPR2. ...
    (comp.soft-sys.matlab)