Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 echoing 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?

like image 278
Fred Avatar asked Dec 06 '22 23:12

Fred


2 Answers

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
$ 
like image 97
gahooa Avatar answered Jan 12 '23 01:01

gahooa


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
like image 42
asoundmove Avatar answered Jan 12 '23 00:01

asoundmove