shell questions

In QNX4 I used to be able to say in a shell script:

if [ -n $XYZ -a $XYZ == XYZ ]; then

in QNX6 I seem to have to change this to:
if [ -n $XYZ ]; then
if [ $XYZ == XYZ ]; then

I understand why it is failing when the $XYZ expression is NULL, but
why in QNX6 is the shell parsing the second expression if the first
expression is FALSE?

Also, (completely different issue), why do
if [ -n $XYZ ]
and
if [ -z $XYZ ]
both return TRUE if $XYZ is undefined?

And is there a way to test IF XYZ is defined?
(I saw that I can assing a different value to a variable based on weather
or not XYZ is defined.)


Bill Caroselli – Q-TPS Consulting
1-(626) 824-7983
qtps@earthlink.net

quote your shell variable so it looks like
if [ -n “$XYZ” -a “$XYZ” == XYZ ]

Bill Caroselli <qtps@earthlink.net> wrote:

In QNX4 I used to be able to say in a shell script:

if [ -n $XYZ -a $XYZ == XYZ ]; then

in QNX6 I seem to have to change this to:
if [ -n $XYZ ]; then
if [ $XYZ == XYZ ]; then

I understand why it is failing when the $XYZ expression is NULL, but
why in QNX6 is the shell parsing the second expression if the first
expression is FALSE?

Also, (completely different issue), why do
if [ -n $XYZ ]
and
if [ -z $XYZ ]
both return TRUE if $XYZ is undefined?

And is there a way to test IF XYZ is defined?
(I saw that I can assing a different value to a variable based on weather
or not XYZ is defined.)


Bill Caroselli – Q-TPS Consulting
1-(626) 824-7983
qtps@earthlink.net

liug <liug@mama.indstate.edu> wrote:

quote your shell variable so it looks like
if [ -n “$XYZ” -a “$XYZ” == XYZ ]

Thanks. This DID work.

Was it a bug in QNX4 that the quotes weren’t needed?


Bill Caroselli <> qtps@earthlink.net> > wrote:

Also, (completely different issue), why do
if [ -n $XYZ ]
and
if [ -z $XYZ ]
both return TRUE if $XYZ is undefined?

And is there a way to test IF XYZ is defined?
(I saw that I can assing a different value to a variable based on weather
or not XYZ is defined.)

Any thoughts on this one?

\

Bill Caroselli – Q-TPS Consulting
1-(626) 824-7983
qtps@earthlink.net

Bill Caroselli <qtps@earthlink.net> wrote:

Also, (completely different issue), why do
if [ -n $XYZ ]
and
if [ -z $XYZ ]
both return TRUE if $XYZ is undefined?

And is there a way to test IF XYZ is defined?
(I saw that I can assing a different value to a variable based on weather
or not XYZ is defined.)

Any thoughts on this one?

same thing, you need to quote the variable, so it will look
if [ -z “$XYZ” ]
or
if [ -n “$XYZ” ]

either one will work.

liug <liug@mama.indstate.edu> wrote:
: quote your shell variable so it looks like
: if [ -n “$XYZ” -a “$XYZ” == XYZ ]


The other trick folks are using is this

if [ z$XYZ == zXYZ ]


The problem is that the expansion is done before by the shell
so this:

if [ -n $XYZ -a $XYZ == XYZ ]; then

will end up looking like this:

if [ -n -a == XYZ ]; then

if the variable $XYZ is not set, definitely not what you want
and undefined behaviour depending on the command test i.e. [



: Bill Caroselli <qtps@earthlink.net> wrote:
:> In QNX4 I used to be able to say in a shell script:

:> if [ -n $XYZ -a $XYZ == XYZ ]; then

:> in QNX6 I seem to have to change this to:
:> if [ -n $XYZ ]; then
:> if [ $XYZ == XYZ ]; then

:> I understand why it is failing when the $XYZ expression is NULL, but
:> why in QNX6 is the shell parsing the second expression if the first
:> expression is FALSE?

:> Also, (completely different issue), why do
:> if [ -n $XYZ ]
:> and
:> if [ -z $XYZ ]
:> both return TRUE if $XYZ is undefined?

:> And is there a way to test IF XYZ is defined?
:> (I saw that I can assing a different value to a variable based on weather
:> or not XYZ is defined.)

:> –
:> Bill Caroselli – Q-TPS Consulting
:> 1-(626) 824-7983
:> qtps@earthlink.net


au revoir, alain

Aussi haut que l’on soit assis, on est toujours assis que sur son cul !!!

Alain Magloire <alain@qnx.com> wrote:

liug <> liug@mama.indstate.edu> > wrote:
: quote your shell variable so it looks like
: if [ -n “$XYZ” -a “$XYZ” == XYZ ]



The other trick folks are using is this

if [ z$XYZ == zXYZ ]

Or, even safer, do both:

if [ “z$XYZ” == zXYZ ]

The quotes protect you from $XYZ being empty or containing a space, but
often you still need to do something about the case of $XYZ starting
with a dash.

BTW A single “=” is equivalent to “==” but more portable: the “=” is
POSIX, while the “==” is not.

liug <liug@mama.indstate.edu> wrote:

same thing, you need to quote the variable, so it will look
if [ -z “$XYZ” ]
or
if [ -n “$XYZ” ]

either one will work.

Cool. I figured that if the sh wasn’t bitching about syntax,
then what I had was good enough.

Thanks

Wojtek Lerch <wojtek_l@yahoo.ca> wrote:

Alain Magloire <> alain@qnx.com> > wrote:
liug <> liug@mama.indstate.edu> > wrote:
: quote your shell variable so it looks like
: if [ -n “$XYZ” -a “$XYZ” == XYZ ]



The other trick folks are using is this

if [ z$XYZ == zXYZ ]

Or, even safer, do both:

if [ “z$XYZ” == zXYZ ]

The quotes protect you from $XYZ being empty or containing a space, but
often you still need to do something about the case of $XYZ starting
with a dash.

BTW A single “=” is equivalent to “==” but more portable: the “=” is
POSIX, while the “==” is not.

Thank you Alain & Wojtek.

I did understand why it wasn’t working. I didn’t understand why it
did work in QNX4.

I did not know about the ‘==’ thing. Thanks.
My old scripts were written using the QNX4 helpviewer pages.
I see that this documentation has been updated for QNX6.
Thanks Steve.

David Kuechenmeister <david.kuechenmeister@viasat.com> wrote:

Thanks for the tips about strings. Now how about numbers? I can’t seem to
test for the correct number of command line arguments.

if [ $# -ne 1]
then

fi

The shell is fussy about parsing tokens. Try a space before the ‘]’.
if [ $# -ne 1 ]


Bill Caroselli – Q-TPS Consulting
1-(626) 824-7983
qtps@earthlink.net

Thanks for the tips about strings. Now how about numbers? I can’t seem to
test for the correct number of command line arguments.

if [ $# -ne 1]
then

fi

Thanks,
David Kuechenmeister

Thanks. I guess the shell needs something to separate the tokens.


“Bill Caroselli” <qtps@earthlink.net> wrote in message
news:b2u3c3$sch$4@inn.qnx.com

David Kuechenmeister <> david.kuechenmeister@viasat.com> > wrote:
Thanks for the tips about strings. Now how about numbers? I can’t seem
to
test for the correct number of command line arguments.

if [ $# -ne 1]
then

fi

The shell is fussy about parsing tokens. Try a space before the ‘]’.
if [ $# -ne 1 ]


Bill Caroselli – Q-TPS Consulting
1-(626) 824-7983
qtps@earthlink.net