4

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?

3 Answers 3

12

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 $ 
Sign up to request clarification or add additional context in comments.

Comments

4

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 

Comments

0

Try [[ $catme == true ]] instead.

Or better still, gahooa's answer is pretty good.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.