Reputation: 101
I got the following snippet failing on KornShell (ksh):
var1="1"
var2="2"
if [ ( "$var1" != "" -o "$var2" != "") -a ( "$var1" = "$var2" -o " "$var1" = "x") ]; then
echo "True"
else
echo "False"
fi
ksh: syntax error: `"$var1"' unexpected
As I understand, this fails because the parentheses run in a subshell where var1 is not recognized. So how could sets of conditions be grouped inside the square brackets?
N.B. I already know the following solutions and do not want to use them:
Upvotes: 4
Views: 58168
Reputation: 11
You need to use double square brackets.. [[--------]]
Hope it helps.
Regards.
Upvotes: 1
Reputation: 45626
Are you looking for this?
#!/bin/ksh
if [[ -n $1 || -n $2 ]] && [[ $1 == "$2" || $1 == x ]]; then
echo "True"
else
echo "False"
fi
Run:
$ ./if.sh "" ""
False
$ ./if.sh 1 2
False
$ ./if.sh 1 1
True
$ ./if.sh x 2
True
If you are wondering why your code fails:
\(
"
floating aroundSo this ...
if [ ( "$var1" != "" -o "$var2" != "") -a ( "$var1" = "$var2" -o " "$var1" = "x") ]; then
typo ---^ ^
^------------------ missing spaces --------^
... should look like this ...
if [ \( "$var1" != "" -o "$var2" != "" \) -a \( "$var1" = "$var2" -o "$var1" = "x" \) ]; then
and then it will work.
Upvotes: 6