Unix Shell - if true ???

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > December 2007 > if true ???





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author if true ???
bernd

2007-12-19, 1:23 pm

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
Lew Pitcher

2007-12-19, 7:27 pm

On Dec 19, 2:14 pm, bernd <bew...@gmx.net> 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?


Claudio

2007-12-19, 7:27 pm

On 2007-12-19, bernd <bew_ba@gmx.net> 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 ?



Janis Papanagnou

2007-12-19, 7:27 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?


"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

Stephane Chazelas

2007-12-20, 1:40 am

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
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com