|
Home > Archive > Unix Shell > August 2007 > proper syntax for checking values of multiple variables
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
proper syntax for checking values of multiple variables
|
|
| Billy Patton 2007-08-22, 1:23 pm |
| 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 2007-08-22, 1:23 pm |
| 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
--
Glenn Jackman
"You can only be young once. But you can always be immature." -- Dave Barry
| |
| Ed Morton 2007-08-22, 1:23 pm |
| Glenn Jackman wrote:
> At 2007-08-22 09:01AM, "Billy Patton" wrote:
>
>
>
> 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.
| |
| Cyrus Kriticos 2007-08-22, 7:23 pm |
| 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 2007-08-22, 7:23 pm |
| 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 2007-08-22, 7:23 pm |
| Cyrus Kriticos wrote:
> Billy Patton wrote:
>
>
>
> Simple add the missing `]'
>
> if [ "$gds" = '' ] && [ "$oasis" = '' ] && [ "$laff" = '' ] && [ "$lef"
> = '' ] && [ "$def" = '' ] ; then
or
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
| |
|
| Ed Morton <mor...@lsupcaemnt.com> wrote:
> Glenn Jackman wrote:
[vbcol=seagreen]
> 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...ities/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
|
|
|
|
|