KornShell Boolean Conditional Logic
I am a little confused with this KornShell (ksh) script I am writing, mostly with booleans and conditionals.
So the first part of my script I have catme
and wcme
both set to either true
or false
. This part is working fine, as I have tried echo
ing them and they produce the expected results. Later on, I have this code:
if [[ $catme ]] ; then
some commands
fi
And I repe开发者_Python百科at this with wcme
. However, unexpectedly, no matter what wcme
and catme
are, the commands inside my if
statement are executed.
Is this a syntax error? I have tried [[ $catme -eq true ]]
but that does not seem to work either. Could someone point me in the right direction?
test
and [
are the same thing. You need to get rid of the test
command from your if statement, so it would look like this:
if $catme; then
some commands
fi
Type man test
to get more info.
For example:
$ v=true
$ $v
$ if $v; then
> echo "PRINTED"
> fi
PRINTED
$ v=false
$ if $v; then
> echo "PRINTED"
> fi
$
You can also try the trial and error method:
if [[ true ]]; then echo +true; else echo -false; fi
+true
if [[ false ]]; then echo +true; else echo -false; fi
+true
if [[ 0 ]]; then echo +true; else echo -false; fi
+true
if [[ -1 ]]; then echo +true; else echo -false; fi
+true
if (( -1 )); then echo +true; else echo -false; fi
+true
if (( 0 )); then echo +true; else echo -false; fi
-false
if (( 1 )); then echo +true; else echo -false; fi
+true
if [[ true == false ]]; then echo +true; else echo -false; fi
-false
if [[ true == true ]]; then echo +true; else echo -false; fi
+true
if true; then echo +true; else echo -false; fi
+true
if false; then echo +true; else echo -false; fi
-false
Try [[ $catme == true ]]
instead.
Or better still, gahooa's answer is pretty good.
精彩评论