Conditional setting of ENV in ksh - Question of how to

Michael "Ford" Ditto ditto at cbmvax.UUCP
Fri Aug 19 14:15:58 AEST 1988


In article <3790 at pbhyf.PacBell.COM> rob at pbhyf.PacBell.COM (Rob Bernardo) writes:
>
>In one of the earlier guides to ksh, I found any interesting way
>to set the ENV variable so that your ENV file gets read
>only by *interactive* invocations of ksh (which reduces a lot  of
>overhead when doing shell escapes and whatnot). The solution
>was the following:
>
>export START ENV
>START=$HOME/.kshrc		# Name of file with aliases, functions
>				# for interactive ksh invocations only
>ENV='${START[(_$-=1)+(_=0)-(_$-!=_${-%%*i*})]}'

>Okay, so the above works in setting ENV only if the ksh is interactive.
>So in order to have different ENV files for interactive and non-interactive
>ksh invocations, I figured what I could do this this:
>
>export START ENV
>START[0]=$HOME/.kshirc		# Name of file with aliases, functions
>				# for interactive ksh invocations only
>START[1]=$HOME/.kshnirc		# Name of file with aliases, functions
>				# for non-interactive ksh invocations only
>ENV='${START[(_$-=1)+(_=0)-(_$-!=_${-%%*i*})]}'

>Looks like it should work, but it doesn't! What I find is this:
>
>2. Any other interactive ksh's, do not work okay. ENV is set to the 
>value of START[0], ****but**** the value of START[0] and START[1] have 
>been altered!!!! START[0] now has the former value of START[1] and 
>START[1] isn't set!


This is because you did not give START[] any values in those shells.
Since START was exported by the login shell, it will be inherited by
children as $START, which can also be accessed as $START[0].
$START[1] will not be set.

>I fiddled with this more and found that whatever is the highest subscripted
>START, it's the one whose value gets copied to START[0] in all non-login
>ksh's! This is absolutely bizarre.

It's actually the LAST value that was given to any element of START, not
the highest numbered one.

>2. How do I solve what I originally set out to do -  namely, have ENV
>set to one file for all interactive ksh invocations and set to a
>different file for all non-interactive ksh invocations?

Why not just set your ENV to $HOME/.kshrc or whatever, and in that file
do the checking... like:

case "$-" in
*i*)
	echo "Interactive code goes here"
	;;
*)
	echo "Noninteractive code goes here"
	;;
esac

To make it go faster, have the $ENV file just source one or the other
of two sub-kshrc's.
-- 
					-=] Ford [=-

	.		.		(In Real Life: Mike Ditto)
.	    :	       ,		ford at kenobi.cts.com
This space under construction,		...!ucsd!elgar!ford
pardon our dust.			ditto at cbmvax.commodore.com



More information about the Comp.unix.wizards mailing list