Curious ksh hack
M.BRILLIANT
marty1 at houdi.UUCP
Sat Mar 12 08:21:56 AEST 1988
In article <340 at manta.UUCP>, brant at manta.UUCP (Brant Cheikes) writes:
> In a memo by David Korn describing ksh, there is a paragraph that
> states:
>
> "The ENV file can have an undesirable effect on performance....
> .... If you export the startup file name in the
> variable START, then setting
>
> ENV='${START[(_$-=1)+(_=0)-(_$-!=_{-%%*i*})]}'
>
> will only invoke the startup file for interactive shells since the
> subscript evaluates to 0 only if the shell is interactive."
>
> Now, I've done this and it works, but I can't quite figure out how.
I've figured out how, but I can't make it work.
The manual entry for ksh says that the subscript in an array parameter,
that is, x in $START[x], is evaluated as an arithmetic expression.
In arithmetic evaluation, as described under the "let" command, you can
put subexpressions in parentheses and use = for arithmetic replacement.
In an interactive shell, $- usually evaluates to "is". So the first
term in parentheses, (_$-=1), evaluates to 1 but also sets the
identifier _is equal to 1. The second term, (_=0), evaluates to zero
and sets the identifier _ to zero. The third term is apparently a
misprint for (_$-!=_${-%%*i*}). The part before the != evaluates, as
previously defined, to 1. The part after the != uses ${..%..} to
lop off any trailing substring in $- that contains an i, so that part
evaluates to _ (zero) in an interactive shell, but to _$- (=1) in a
noninteractive shell. The != is true in an interactive shell, equals
1, but is false in an noninteractive shell, equals zero. The sum of
the three terms is zero in an interactive shell but is 1 in a
noninteractive shell.
The desired effect is achieved if, as it says in the manual entry,
"command and parameter substitution is performed on the value [of $ENV]
to generate the pathname of the script ...." On my system, substitution
doesn't seem to happen, so it doesn't work.
M. B. Brilliant Marty
AT&T-BL HO 3D-520 (201)-949-1858
Holmdel, NJ 07733 ihnp4!houdi!marty1
Disclaimer: Opinions stated herein are mine unless and until my employer
explicitly claims them; then I lose all rights to them.
More information about the Comp.sys.att
mailing list