"new" ksh on "old" hpux

"new" ksh on "old" hpux

Post by steve johnso » Sat, 26 Aug 1995 04:00:00



we've got various hpux boxes scattered around the country running anything
from 9.01 to 9.05, all of which have the"old" ksh, namely a 1988 version.

i'm new to hpux and a recent convert from csh, but some of the cron
scripts i've inherited seem to have a syntax problem.  basically they say:

        if [ $ROOM -lt 5000 ]

which generates a "test: argument expected" from cron when it runs the
scripts.

as near as i can tell from the "new" 1993 ksh spec the syntax should be:

        if (( $ROOM < 5000 ))

but i'm not at all sure this would work on the hps w/the old ksh.

anybody have a clue on this, or better yet, some real live experience?
should i figure out the old syntax (which i suspect leaves out the square
brackets) and use that, or is it possible (albeit highly unlikely) that
our old ksh will correctly interpret the new ksh syntax.  i'm looking for
a relatively long-term solution as the gov't isn't likely to upgrade a
whole bunch of hp boxes to 10.0 (which presumabley has the new ksh) -
money's pretty tight.

--
HEY!

   - james brown

 
 
 

"new" ksh on "old" hpux

Post by David W. Tamk » Sat, 26 Aug 1995 04:00:00



in response to Steve Johnson:

| "test" has problems with situations like this, so you'll sometimes see
| people use workarounds like:
|
|       if [ "$ROOM" -lt 5000 ]               # the quotes protect arg 1

If $ROOM is null or unset, will test see it as 0 for purposes of -lt?

|       if [ X$ROOM < X5000 ]                # the "X" ensures arg 1 will be
|                                       # non-null

That one probably won't work.  Most single-[ tests don't grok "<" to mean
"less than", and those that do use it lexically rather than arithmetically
if the strings to compare are not numbers.  Having that X in there means it
isn't a number any more.

So if that version of test understands "<" at all, it will think that
X6 > X5000 and that X41287342407 < X5000.

This should work, along with some of Greg's suggestions involving [[ or ((:

       if [ ${ROOM:-0} -lt 5000 ]

 
 
 

"new" ksh on "old" hpux

Post by Greg Fru » Sat, 26 Aug 1995 04:00:00



Quote:> scripts i've inherited seem to have a syntax problem.  basically they say:

>    if [ $ROOM -lt 5000 ]

> which generates a "test: argument expected" from cron when it runs the
> scripts.

What's happening is that ROOM is null, so ksh is seeing:

        if [ -lt 5000 ]

which doesn't have enough arguments, hence the error message.  Note that
"[ ... ]" is a Bourne shell-type syntax, and is a synonym for the "test"
shell command. "test" has problems with situations like this, so you'll sometimes see people use workarounds like:

        if [ "$ROOM" -lt 5000 ]               # the quotes protect arg 1
        if [ X$ROOM < X5000 ]                # the "X" ensures arg 1 will be
                                        # non-null

A better way to write this is to use ksh's "[[ ... ]]" operator, which is
more intelligent and provides more options:

        if [[ $ROOM -lt 5000 ]]

Quote:> as near as i can tell from the "new" 1993 ksh spec the syntax should be:

>    if (( $ROOM < 5000 ))

This statement uses ksh's arithmetic "(( ... ))" operator.  In this case it
has the same effect as "[[ ... ]]" but is faster, if ROOM has been
declared as an integer using "typeset -i".  The "(( ... ))" is for integers
only, though.  Otherwise, you'll need to use "[[ ... ]]".

Quote:> but i'm not at all sure this would work on the hps w/the old ksh.

> anybody have a clue on this, or better yet, some real live experience?
> should i figure out the old syntax (which i suspect leaves out the square
> brackets) and use that, or is it possible (albeit highly unlikely) that
> our old ksh will correctly interpret the new ksh syntax.  i'm looking for

"[ ... ]", "[[ ... ]]" and "(( ... ))" are all valid operators in the 1988
ksh, and I assume they are in the 1993 ksh, too.

Good luck!

--
Greg Fruth

The Aerospace Corporation      Telephone : (310) 336-4287
P.O. Box 92957-M4/942                FAX : (310) 336-5827

 
 
 

"new" ksh on "old" hpux

Post by Ken Gre » Sat, 26 Aug 1995 04:00:00



> we've got various hpux boxes scattered around the country running anything
> from 9.01 to 9.05, all of which have the"old" ksh, namely a 1988 version.
> i'm new to hpux and a recent convert from csh, but some of the cron
> scripts i've inherited seem to have a syntax problem.  basically they say:
>    if [ $ROOM -lt 5000 ]
> which generates a "test: argument expected" from cron when it runs the
> scripts.
> as near as i can tell from the "new" 1993 ksh spec the syntax should be:
>    if (( $ROOM < 5000 ))

        Try if [[ $ROOM -lt 5000 ]]
        or  if [ "$ROOM" -lt 5000 ]

Of course you shouldn't be trying to access an uninialized variable at all :-)

Quote:> but i'm not at all sure this would work on the hps w/the old ksh.
> anybody have a clue on this, or better yet, some real live experience?
> should i figure out the old syntax (which i suspect leaves out the square
> brackets) and use that, or is it possible (albeit highly unlikely) that
> our old ksh will correctly interpret the new ksh syntax.  i'm looking for
> a relatively long-term solution as the gov't isn't likely to upgrade a
> whole bunch of hp boxes to 10.0 (which presumabley has the new ksh) -
> money's pretty tight.

The Posix shell at 10.01 works fine

--

Ken Green Computer Consultancy  
                  22 Matthews Chase, Binfield, Berkshire, RG42 4UR.  U.K.

 
 
 

"new" ksh on "old" hpux

Post by hal.. » Tue, 29 Aug 1995 04:00:00



Quote:> ....
>    if [ $ROOM -lt 5000 ]
> ....

Try this
        [ 0$ROOM -lt 5000 ]
or this
        [ "$ROOM" -lt 5000 ]
 
 
 

"new" ksh on "old" hpux

Post by steve johnso » Tue, 29 Aug 1995 04:00:00



Quote:> What's happening is that ROOM is null, so ksh is seeing:

>    if [ -lt 5000 ]

> which doesn't have enough arguments, hence the error message.  Note that
> "[ ... ]" is a Bourne shell-type syntax, and is a synonym for the "test"
> shell command. "test" has problems with situations like this, so you'll sometimes see people use workarounds like:

um, yes.  like i said, i inherited these scripts from someone else and i'm
new to both hpux and, more importantly, ksh.

had i checked more thoroughly i would have noticed that ROOM is a check on
the amount of available disk space.  furthermore, i would have noticed
that it's improperly initialized (using bdf or something) so that it
always returns null, thus the "test: argument expected."

neverthelesss, i learned a few things about ksh and posix thanks to the
emails, followups and some reading on my part.

sorry for the foolish question - i shoulda checked more on my end before
posting.

--
HEY!

   - james brown