Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

proper syntax for checking values of multiple variables

6 views
Skip to first unread message

Billy Patton

unread,
Aug 22, 2007, 9:01:39 AM8/22/07
to
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.

Glenn Jackman

unread,
Aug 22, 2007, 9:53:38 AM8/22/07
to

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

--
Glenn Jackman
"You can only be young once. But you can always be immature." -- Dave Barry

Ed Morton

unread,
Aug 22, 2007, 11:04:51 AM8/22/07
to
Glenn Jackman wrote:

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.

Message has been deleted
Message has been deleted

Cyrus Kriticos

unread,
Aug 22, 2007, 4:21:19 PM8/22/07
to
Billy Patton wrote:

>
> ERROR IS HERE if [ "$gds" == '' && "$oasis" == '' && "$laff" == '' &&
> "$lef" == '' && "$def" == '' ] ; then
>

> On the line above where I have 'ERROR IS HERE' my xterm is complaining
> about :
>
> line 41: [: missing `]'

> How do I put the syntax so that 1 of the 5 variables must be set?

Simple add the missing `]'

if [ "$gds" = '' ] && [ "$oasis" = '' ] && [ "$laff" = '' ] && [ "$lef"
= '' ] && [ "$def" = '' ] ; then

--
Best | "Was bekommt man/frau, wenn man/frau Software kauft?
regards | Nichts außer einem Haufen Nullen und Einsen."
Cyrus | -- aus d. Lizenzvereinbarung von Spybot Search&Destroy

Cyrus Kriticos

unread,
Aug 22, 2007, 4:27:34 PM8/22/07
to
Cyrus Kriticos wrote:
> Billy Patton wrote:
>
>>
>> ERROR IS HERE if [ "$gds" == '' && "$oasis" == '' && "$laff" == ''
>> && "$lef" == '' && "$def" == '' ] ; then
>>
>> On the line above where I have 'ERROR IS HERE' my xterm is complaining
>> about :
>>
>> line 41: [: missing `]'
>
>> How do I put the syntax so that 1 of the 5 variables must be set?
>
> Simple add the missing `]'
>
> if [ "$gds" = '' ] && [ "$oasis" = '' ] && [ "$laff" = '' ] && [ "$lef"
> = '' ] && [ "$def" = '' ] ; then

or

if [ "${gds}${oasis}${laff}${lef}${def}" = '' ] ; then

bsh

unread,
Aug 23, 2007, 7:34:17 AM8/23/07
to
Ed Morton <mor...@lsupcaemnt.com> wrote:
> Glenn Jackman wrote:
> > "Billy Patton" wrote:
> > > sub routine to check for required arguments for a script.

> and the test for equality is "=", not "==", ...

Only in POSIX-compliant versions of _external_ test(1), which
will never be executed either in bash(1) or ksh(1) -- and if he's
using those test builtins, it's assured that abherent cases
of variable substitution will not confuse it as long as parameter
cardinality is correct.

However: amusingly, the recommendation deprecates the use
of interstital -o and -a in preference of && and || to obviate
some pathological input cases!:

http://www.opengroup.org/onlinepubs/009695399/utilities/test.html
Search for string: "APPLICATION USAGE"

> if [ -z "$gds$oasis$laff$lef$def" ]; then
> # error
> fi

In that case, I'd do a "complete makeover":

: ${tech:?} ${process:?} ${cell:?}
[ -z "$gds$oasis$laff$lef$def" ] && error

> and get rid of all those redundant semicolons at the end of every line -
> they'll come back to bite you one day.

Seconded. Semicolons are not parsed the way
programmers with C/C++/Java experience think they
should be. They are not statement terminators, but
newline substitutors. In fact, there are some cases
where a semicolon _should_ work, but doesn't. For
instance:

for var; in cases; do ... done

=Brian

0 new messages