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 repeat 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
$ 

您也可以尝试尝试和错误的方法:

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.

链接地址: http://www.djcxy.com/p/17516.html

上一篇: 如何在mkdir中创建makefile中的目录

下一篇: KornShell布尔条件逻辑