ksh & command line interpretation

ksh & command line interpretation

in the book "Unix SystemV Release4 An Intoduction" by Ken Rosen,Richard
Rosinski and James Farber there is a script called ksh.preamble as
follows:
if [x"\$RANDOM" = x"\$RANDOM"]
then

exit "\$?"
fi

couple of questions
(1)what is the phrase x"\$RANDOM"

(2)how does the shel expand/interpret the line

thanks

ksh & command line interpretation

> in the book "Unix SystemV Release4 An Intoduction" by Ken Rosen,Richard
> Rosinski and James Farber there is a script called ksh.preamble as
> follows:
>         if [x"\$RANDOM" = x"\$RANDOM"]
>         then

>                 exit "\$?"
>         fi

> couple of questions
>  (1)what is the phrase x"\$RANDOM"

>  (2)how does the shel expand/interpret the line

> thanks

Heather,
\$RANDOM is a ksh builtin random number generator. Each reference to
\$RANDOM will return a different number.  Therefore, in ksh \$RANDOM
will never equal \$RANDOM (the x is added because in a non-ksh script
\$RANDOM probably won't be defined and you could get an error from
the test operator due to missing arguments).  So, if x\$RANDOM DOES
equal x\$RANDOM, you aren't running ksh, so this example will execute
ksh to re-execute the current script, with existing arguments preserved.

--
Allen Kirby                     AT&T ITS Production Services

ksh & command line interpretation

| in the book "Unix SystemV Release4 An Intoduction" by Ken Rosen,Richard
| Rosinski and James Farber there is a script called ksh.preamble as
| follows:
|       if [x"\$RANDOM" = x"\$RANDOM"]
|       then

|               exit "\$?"
|       fi

Do be careful with [.  The above will fail because you left out the
white space after [, and before ].  "[" is another way to spell "test",
a fraudulent and only partly successful attempt to simulate a
syntactical feature of other languages.  The "case" command avoids
this nonsense and does a better job with string comparisons.

| couple of questions
|  (1)what is the phrase x"\$RANDOM"

The Korn shell generates a random number on reference to this variable,
each time, so it won't equal itself - if it's ksh.  That's the question.

|  (2)how does the shel expand/interpret the line

Well, it doesn't, but if you swap ` for ', it will determine the full
path to the current script and run ksh on itself.  Also see "exec",
which the authors probably should have used, and see if you can find
what the default value for "exit" is, if no parameter is supplied.

Donn Cave, University Computing Services, University of Washington

ksh & command line interpretation

| [Date: Fri, 13 Jun 1997 11:54:49 -0400]

|   >         if [x"\$RANDOM" = x"\$RANDOM"]
--<snip-snip>--
|   ...  (the x is added because in a non-ksh script \$RANDOM probably won't
|   be defined and you could get an error from the test operator due to
|   missing arguments).

Strictly spoken, the x is not necessary when there are "" double quotes
around the variable reference:
if ["\$RANDOM" = "\$RANDOM"]
should work as well (apart from the syntax error, see below) in any sh-like
shell, though you need the x if you go
if [x\$RANDOM = x\$RANDOM]

*BUT* there should be a space after the `[', like this
if [ x"\$RANDOM" = x"\$RANDOM" ]
otherwise it won't work at all

Regards
R'
--
Enter any 11-digit prime number to continue...

Hi all,

Command completion in ksh is done with Esc Esc (hitting the escape key
twice). Although I've never been successful at getting this to work on
my system at work (AIX 3.2.5), I read that's the key sequence for
command/path completion.

My question is, can that be changed to the Tab key and if so how, in
ksh? Command completion in tcsh and bash use the Tab key. The Tab key is
SO MUCH MORE convenient a key for command completion.

Another question... about previous command retrieval. bash and tcsh use
the UP and DOWN arrow keys for command retrieval. ksh uses Esc-k & Esc-j
respectively. UP & DOWN are MUCH BETTER choices. Can this be changed in
ksh and how?

Any guidance would be appreciated. I like the ksh for it's better (I
think so) scripting/programming features but the way it implements
command/path completion and previous command retrieval is really
annoying me. So much time can be saved with the use of Tab and UP,DOWN
keys.

Thanks, Zoran.