if true ???

if true ???

am 19.12.2007 20:14:35 von bernd

Hi folks,

the ksh knows the following exported aliases (amongst others):

true=':'
false='let 0'

So I could do a Boolean test in the following way:

if $BOOLTEST
then

# some code
fi

(provided I made sure that $BOOLTEST was set to true or false before)

Is this correct ksh-usage (syntax was checked by me, so this would be
o.k.) or is there something contradicting this usage?

I just ask since, although quite handy (avoiding the square braces
stuff), I have not seen that kind of code in a programmers script.

Cheers


Bernd

Re: if true ???

am 19.12.2007 20:43:04 von Lew Pitcher

On Dec 19, 2:14 pm, bernd wrote:
> Hi folks,
>
> the ksh knows the following exported aliases (amongst others):
>
> true=':'
> false='let 0'
>
> So I could do a Boolean test in the following way:
>
> if $BOOLTEST
> then
>
> # some code
> fi
>
> (provided I made sure that $BOOLTEST was set to true or false before)
>
> Is this correct ksh-usage (syntax was checked by me, so this would be
> o.k.) or is there something contradicting this usage?

I'm no ksh expert, but this usage looks OK to me; Mostly useless, but
OK

> I just ask since, although quite handy (avoiding the square braces
> stuff), I have not seen that kind of code in a programmers script.

What's wrong with
if /bin/true
then
# some code that is always performed
fi

and

if /bin/false
then
# some code that is never performed
fi

and (given your definitions of true and false above) why would you
need squarebracketted expressions?

Re: if true ???

am 19.12.2007 23:55:27 von Claudio

On 2007-12-19, bernd wrote:
> Hi folks,
>
> the ksh knows the following exported aliases (amongst others):
>
> true=':'
> false='let 0'
>
> So I could do a Boolean test in the following way:
>
> if $BOOLTEST
> then
>
> # some code
> fi
>
> (provided I made sure that $BOOLTEST was set to true or false before)
>
> Is this correct ksh-usage (syntax was checked by me, so this would be
> o.k.) or is there something contradicting this usage?


If you are not sure, execute the code in debug mode:

sh -x script

> I just ask since, although quite handy (avoiding the square braces
> stuff), I have not seen that kind of code in a programmers script.

I haven't too. You can't check the return value from a command/script in
this way. Is it a possible cause ?

Re: if true ???

am 20.12.2007 01:54:30 von Janis Papanagnou

bernd wrote:
> Hi folks,
>
> the ksh knows the following exported aliases (amongst others):
>
> true=':'
> false='let 0'
>
> So I could do a Boolean test in the following way:
>
> if $BOOLTEST
> then
>
> # some code
> fi
>
> (provided I made sure that $BOOLTEST was set to true or false before)
>
> Is this correct ksh-usage (syntax was checked by me, so this would be
> o.k.) or is there something contradicting this usage?

"Correct" if the variable expands to the name of a command.

> I just ask since, although quite handy (avoiding the square braces
> stuff), I have not seen that kind of code in a programmers script.

And I think you've not yet seen it in existing code for good reasons.

The if expects a "compound list", so any command is appropriate. Now
assume you have branches in your code where variable BOOLTEST will
not be set (by accident or oversight or ignorance or a bug or future
extensions by other people, whatever). Then you can feed commands
from the environment. Assuming your script is named ifvar...

BOOLTEST="/bin/rm -rf" ksh ifvar

Choose arbitrary commands at your delight. It may not even be that
apparent as in my example if an export BOOLTEST="..." is defined
somewhere hidden in another script which calls your script.

Janis

>
> Cheers
>
>
> Bernd

Re: if true ???

am 20.12.2007 08:02:46 von Stephane CHAZELAS

On Wed, 19 Dec 2007 11:14:35 -0800 (PST), bernd wrote:
> Hi folks,
>
> the ksh knows the following exported aliases (amongst others):
>
> true=':'
> false='let 0'
>
> So I could do a Boolean test in the following way:
>
> if $BOOLTEST
> then
>
> # some code
> fi
>
> (provided I made sure that $BOOLTEST was set to true or false before)
>
> Is this correct ksh-usage (syntax was checked by me, so this would be
> o.k.) or is there something contradicting this usage?
[...]

It will only work as long as the IFS special parameter does not
contain any of the t, r, u, e, f, a, l, s characters, because by
leaving $BOOLTEST unquoted, you *explicitely request* it to
split it according to $IFS (and perform filename generation).

The correct syntax would be:

if "$BOOLTEST"
then
...
fi

Or you could use a function:

booltest() true
booltest() false

if booltest
then
...
fi

--
Stephane