Reputation: 3
I've been looking all over the place but couldn't find an answer. In ksh, how do you do something like this:
while [ [ ! [ [ -n $var1 ] || [ [ -n $var2 ] && [ -n $var3 ] ] ] ] && [ ! [ [ -n $var1 ] && [ -n $var2 ] ] ] ]; do
...etc etc
done
or in pseudo/a bit easier way to see
while ((! ((var1 != none) or
((var2 != none) and (var3 != none)))
and
(!((var1 != none) and (var2 != none)))) {
....
}
...essentially any kind of conditions that are grouped
I've found a lot on simple conditions like if [ -z $var1 ] && [ -n $var2 ]; then
but not like the one above.
Any help would be appreciated.
Upvotes: 0
Views: 111
Reputation: 20748
It's much easier to use ksh
's [[ ]]
syntax. (
and )
don't need to be quoted and you can use &&
and ||
just as in C language. For example:
[[ ! ( -n $var1 && ( -n $var2 || -n $var3 ) ) ]]
Also, you don't need to double quote $var
within [[ ]]
which would make the code much cleaner.
Upvotes: 1
Reputation: 10367
There are a number of issues with the test you want to perform.
-n $var
will fail if $var
is empty as it will be expanded to a single -n
.Using -n "$var"
instead is the bulletproof version here.
and is expressed with the -a
operator not with &&
or is expressed with the -o
operator not with ||
the grouping operator is ()
not another nested []
construct
Applying this rules your expression would come closer to looking something like that:
[ ! ( -n "$var1" -o ( -n "$var2" -a -n "$var3" ) ) -a ! ( -n "$var1" -a -n "$var2" ) ]
Note that use of spaces is mandatory here.
Depending on escaping rules you might have to escape (
and )
.
As I don't know how those are handled in ksh
I can't tell for sure if really necessary there, but the all shell proof version would look something like that:
[ ! '(' -n "$var1" -o '(' -n "$var2" -a -n "$var3" ')' ')' -a ! '(' -n "$var1" -a -n "$var2" ')' ]
Note that [
is an executable usually located in /usr/bin/
so there aren't any syntactic differences across different shells.
It's only different meta characters that you have to be aware of, when using [
with a different shell.
Upvotes: 1