home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / sys / hp / 9667 < prev    next >
Encoding:
Internet Message Format  |  1992-08-25  |  3.1 KB

  1. 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
  2. From: ajs@hpfcso.FC.HP.COM (Alan Silverstein)
  3. Newsgroups: comp.sys.hp
  4. Subject: Re: ksh and HP Console?
  5. Message-ID: <7371253@hpfcso.FC.HP.COM>
  6. Date: 25 Aug 92 19:19:13 GMT
  7. References: <GHENNIGA.92Aug18125155@ampere.ampere>
  8. Organization: Hewlett-Packard, Fort Collins, CO, USA
  9. Lines: 90
  10.  
  11. >       ENV=~/.kshrc
  12. >       export ENV
  13. > This will make the ksh read your ~/.kshrc each time it creates a subsequent
  14. > shell.
  15.  
  16. Unfortunately, that applies to shell scripts too.  There's a workaround,
  17. from Korn himself -- as follows.
  18.  
  19.     How to only read .kshrc if in an interactive shell:
  20.  
  21.     > ENV='${START[(_$-=1)+(_=0)-(_$-!=_${-%%*i*})]}'
  22.     > START=~/.kshrc
  23.     > 
  24.     > Please don't ask me how this works, but it does, even in the old ksh.
  25.  
  26.     I'll take a stab at it.  How better to learn?
  27.  
  28.     Note that you can write it a little more legibly as:
  29.  
  30.     ENV='${START[ (_$- = 1) + (_ = 0) - (_$- != _${-%%*i*}) ]}'
  31.  
  32.     Now the first part:
  33.  
  34.         ENV='...'
  35.  
  36.     ENV is evaluated for parameter substitution at every use.  The
  37.     quotes hide the value from immediate evaluation.
  38.  
  39.         ${START[...]}
  40.  
  41.     An element of array START, of which only [0] is defined (as the
  42.     name of the .kshrc file).  A clever way to yield to null in some
  43.     cases.
  44.  
  45.         (...) + (...) - (...)
  46.  
  47.     Surprise!  Array parameter indices can be arithmetic
  48.     expressions, not just constants.  I overlooked this completely.
  49.  
  50.         _$- = 1
  51.  
  52.     The leading underscore is just a way to insure the left side is
  53.     not null even if "$-" evaluates to null.  This is an assignment
  54.     statement, the parameter assigned is named "_<current flags>",
  55.     and it gets the value "1".  And the expression results in 1.
  56.  
  57.         _ = 0
  58.  
  59.     Here, a parameter named "_" gets the value "0".
  60.  
  61.         _$- != _${...}
  62.  
  63.     Now this is ugly.  The key is that "$-" and "${...}" are being
  64.     expanded, and then on top of that the resulting parameter named
  65.     "_<something>" is being expanded too...  a double evaluation.
  66.     The parameter "_<current flags>" is compared with "_${...}" and
  67.     the result is either 1 (true, they're different, so the element
  68.     is 0, so ENV gets set to something) or 0 (false, they're the
  69.     same, so ENV evaluates to null).
  70.  
  71.         -%%*i*
  72.  
  73.     If "$-" (current flags) matches "*i*" (contains an "i", for
  74.     interactive), then it is completely ("%%") wiped to null.
  75.  
  76.     If the shell is interactive, "$-" includes "i" at least, so the
  77.     left side is "$_<something non null>" which has value "1", and
  78.     the right side is "$_<null>" which has value "0", so "!=" is
  79.     true (1), so element 0 is used, so ENV evaluates to non-null, as
  80.     desired.
  81.  
  82.     If the shell is not interactive, "$-" may be null or not.  (I
  83.     think it never is, but that might be a defect of a different
  84.     color.)
  85.  
  86.     If "$-" is not null, "$_<something>" (1) is equal to "$_<same
  87.     something>" (1 again) and the expression yields zero, so element
  88.     1 is used, so ENV evaluates to null, as desired.
  89.  
  90.     If "$-" is null, "$_" (0) is equal to "$_" (0), and so on,
  91.     likewise.
  92.  
  93.     The reason for the complexity, I think, is the need to cover the
  94.     case where "$-" is null.
  95.  
  96.     This is so perverse, I'd never use it without most of this
  97.     explanation.
  98.  
  99.     Don't forget to export ENV and START.
  100.