home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!dtix!darwin.sura.net!zaphod.mps.ohio-state.edu!sol.ctr.columbia.edu!usc!sdd.hp.com!hp-cv!hp-pcd!hpfcso!ajs
- From: ajs@hpfcso.FC.HP.COM (Alan Silverstein)
- Newsgroups: comp.sys.hp
- Subject: Re: ksh and HP Console?
- Message-ID: <7371253@hpfcso.FC.HP.COM>
- Date: 25 Aug 92 19:19:13 GMT
- References: <GHENNIGA.92Aug18125155@ampere.ampere>
- Organization: Hewlett-Packard, Fort Collins, CO, USA
- Lines: 90
-
- > ENV=~/.kshrc
- > export ENV
- >
- > This will make the ksh read your ~/.kshrc each time it creates a subsequent
- > shell.
-
- Unfortunately, that applies to shell scripts too. There's a workaround,
- from Korn himself -- as follows.
-
- How to only read .kshrc if in an interactive shell:
-
- > ENV='${START[(_$-=1)+(_=0)-(_$-!=_${-%%*i*})]}'
- > START=~/.kshrc
- >
- > Please don't ask me how this works, but it does, even in the old ksh.
-
- I'll take a stab at it. How better to learn?
-
- Note that you can write it a little more legibly as:
-
- ENV='${START[ (_$- = 1) + (_ = 0) - (_$- != _${-%%*i*}) ]}'
-
- Now the first part:
-
- ENV='...'
-
- ENV is evaluated for parameter substitution at every use. The
- quotes hide the value from immediate evaluation.
-
- ${START[...]}
-
- An element of array START, of which only [0] is defined (as the
- name of the .kshrc file). A clever way to yield to null in some
- cases.
-
- (...) + (...) - (...)
-
- Surprise! Array parameter indices can be arithmetic
- expressions, not just constants. I overlooked this completely.
-
- _$- = 1
-
- The leading underscore is just a way to insure the left side is
- not null even if "$-" evaluates to null. This is an assignment
- statement, the parameter assigned is named "_<current flags>",
- and it gets the value "1". And the expression results in 1.
-
- _ = 0
-
- Here, a parameter named "_" gets the value "0".
-
- _$- != _${...}
-
- Now this is ugly. The key is that "$-" and "${...}" are being
- expanded, and then on top of that the resulting parameter named
- "_<something>" is being expanded too... a double evaluation.
- The parameter "_<current flags>" is compared with "_${...}" and
- the result is either 1 (true, they're different, so the element
- is 0, so ENV gets set to something) or 0 (false, they're the
- same, so ENV evaluates to null).
-
- -%%*i*
-
- If "$-" (current flags) matches "*i*" (contains an "i", for
- interactive), then it is completely ("%%") wiped to null.
-
- If the shell is interactive, "$-" includes "i" at least, so the
- left side is "$_<something non null>" which has value "1", and
- the right side is "$_<null>" which has value "0", so "!=" is
- true (1), so element 0 is used, so ENV evaluates to non-null, as
- desired.
-
- If the shell is not interactive, "$-" may be null or not. (I
- think it never is, but that might be a defect of a different
- color.)
-
- If "$-" is not null, "$_<something>" (1) is equal to "$_<same
- something>" (1 again) and the expression yields zero, so element
- 1 is used, so ENV evaluates to null, as desired.
-
- If "$-" is null, "$_" (0) is equal to "$_" (0), and so on,
- likewise.
-
- The reason for the complexity, I think, is the need to cover the
- case where "$-" is null.
-
- This is so perverse, I'd never use it without most of this
- explanation.
-
- Don't forget to export ENV and START.
-