Subject: Re: command-line editing and "standard" shells....
To: NetBSD-current Discussion List <current-users@netbsd.org>
From: Greg A. Woods <woods@most.weird.com>
List: current-users
Date: 03/19/1999 23:41:37
[ On Friday, March 19, 1999 at 22:00:19 (-0500), Bill Sommerfeld wrote: ]
> Subject: Re: command-line editing and "standard" shells.... 
>
> > export ENV='${ENVFILE[(_$-=1)+(_=0)-(_$-!=_${-%%*i*})]}'
> 
> Well, I thought sendmail.cf rewrite rules syntax was obscure, but this
> beats it hands down..  and you expect random users to get that right
> without making a typo?

Nope.  At least not the first time anyway!  ;-)  remember: cut and paste

That's why I've been carefully preserving it ever since I first saw it
posted to Usenet.  I *thought* it would have been more commonly known by
now, or at least that the semi-functional expression given in the
kornshell book would be more widely used.  I *can* usually figure it out
why that expression does what it does with a few moments thought and a
handy ksh manual page to look stuff up in though.  I sure wouldn't have
dreampt it up in the first place though.  It is indeed the most obtuse
shell expression I've ever used.  The trick for putting the current time
in your prompt is trivial in comparison.

I would recommend adding that to the manual page though:

Index: ksh.1
===================================================================
RCS file: /cvs/NetBSD/src/bin/ksh/ksh.1,v
retrieving revision 1.1.1.1
diff -c -c -r1.1.1.1 ksh.1
*** ksh.1	1998/02/20 00:21:40	1.1.1.1
--- ksh.1	1999/03/20 04:18:45
***************
*** 115,120 ****
--- 115,134 ----
  with the \fBDEFAULT_ENV\fP macro defined, the file named in that macro
  is included (after the above mentioned substitutions have been performed).
  .PP
+ The following expression will cause the shell to read the specified file
+ only if you spawn an interactive shell:
+ .RS
+ \fB\&export ENVFILE=$HOME/.kshrc\fP
+ .br
+ \fB\&if [ -n "$ENVFILE" -a -r "$ENVFILE" ] ; then\fP
+ .br
+ \fB\&\ ENV='${ENVFILE[(_$-=1)+(_=0)-(_$-!=_${-%%*i*})]}'\fP
+ .br
+ \fB\&\ export ENV\fP
+ .br
+ \fB\&fi\fP
+ .RE
+ .PP
  The exit status of the shell is 127 if the command file specified
  on the command line could not be opened, or non-zero if a fatal syntax
  error occurred during the execution of a script.

I did find a copy of the posting that I may actually have learned that
trick in this almost 10-year-old posting:

| From: tneff@bfmny0.UUCP (Tom Neff)
| Newsgroups: comp.sys.att
| Subject: Re: Forcing /bin/sh in a script under V/386 3.2 Korn shell
| Summary: isn't it?
| Message-ID: <14463@bfmny0.UUCP>
| Date: 13 Jul 89 01:37:47 GMT
| References: <14445@bfmny0.UUCP> <1989Jul12.191342.1048@cs.dal.ca>
| 
|[[....]]
| 
|  * Although you can't escape KSH's long fork time (apparently), you can
|    definitely escape the potentially lengthy process of glomming your
|    ENV environment script for every subshell, by using the ENV trick
|    described in the KSH documentation:
| 
|         export Envfile=$HOME/.env
|         export ENV='${Envfile[(_$-=1)+(_=0)-(_$-!=_${-%%*i*})]}'
| 
|    This cryptic mass will run your environment script (presumably
|    including your favorite functions and aliases etc., and in this 
|    example named ".env" in your home directory) if and only if you 
|    spawn an interactive shell, such as a naked "!" from rn(1) or vi(1).  
|    If you spawn a command directed subshell, a la "ksh -c echo hi", 
|    the environment script won't run and your subprocess will get going
|    a whole lot faster.

BTW, the incorrect (or at least not very portable) version of the string
that eventually appeared in the book was:

	# The one in the book says:
	#export ENV='${ENVFILE[(_$-=0)+(_=1)-_${-%%*i*}]}'

You'll find a similar but so far as I can tell equally unportable
expression on dejanews if you search for envfile too:

	export ENV='${ENVFILE[(_=1)+(_$-=0)-_${-%%*i*}]}'

I've always supsected that the errors were due to the publisher, but I'm
not sure about that -- I also would have though Korn would have sent
troff source to them....

I've collected all kinds of tricks over the years, and since my upgrade
from one version of the shell to another has usually been rather gradual
most of my tricks were made portable enough to run on any ksh.  Perhaps
I should send my .profile & .kshrc et al to be put in NetBSD in
/usr/share/examples/ksh/?  ;-)

A slightly dated version is available anyway:

	ftp://ftp.weird.com/pub/woods/dotfiles-v19.tar.gz

The SCCS files are even more entertaining....

I should also probably cut a v20 release of my environment any day now
too....

BTW, anyone need a copy of Tom Christiansen's csh-whynot FAQ?  :-)

-- 
							Greg A. Woods

+1 416 218-0098      VE3TCP      <gwoods@acm.org>      <robohack!woods>
Planix, Inc. <woods@planix.com>; Secrets of the Weird <woods@weird.com>