At Wed, 09 Oct 2024 09:57:31 +0700, Robert Elz <kre%munnari.OZ.AU@localhost> wrote: Subject: Re: interactive shell detection in shrc > > But if the idea is to use just that to determine if a shell > is interactive, as in: > > | When I decided this is what interactive means > | all of the edge cases went away. Is there a > | case I am not considering? > > what that might be intending to mean, then no, that won't > work at all. > > Run any script (from any interactive shell, as a command line) as > > sh script > (or just ./script if it has 'x' perms & perhaps a #!) > > and in the script if you do "test -t 0" you'll get "true" - in that > script, standard input is a terminal, the same one as the shell from > which the script was run from. Yet the shell running the script is > certainly not interactive. To be interactive, then as well as stderr > also needing to be a terminal, the shell needs to be reading its commands > from that standard input (being a terminal). There's no shell operator > or function to tell you which file descriptor the shell is reading its > command input from (none of the script's business.) Indeed. I've given up on testing for TTYs and the "test $0 = $SHELL" bit as I've realized that for shells I care about that'll be true for shell fragments run from system(3) too. Only the ksh's (pdksh and derivatives, as well as AT&T Ksh) and Bash put 'c' in $- when passed '-c' on the command line, which would be the only other sure way to test, and even with the 'c' they still leave $#=0. None of the true Bourne shells or derivatives, nor Ash and its derivatives add the 'c'. Anyway it's pretty academic and meaningless in the real world as $ENV is pretty much only supported by shells which do always add 'i' to "$-" whenever they are interactive (Ash has done so since its initial 1989 release on comp.sources.unix), and furthermore only a very few shells source $ENV when they are not interactive (and thus they don't need any interactivity test at all). Even AT&T Ksh gave up on always expanding and sourcing $ENV with the original ksh93 release (i.e. as of 1993/12/28), so it is always POSIX compliant now. (I hadn't realized that until I read the manual more carefully and tested it more explicitly.) -- Greg A. Woods <gwoods%acm.org@localhost> Kelowna, BC +1 250 762-7675 RoboHack <woods%robohack.ca@localhost> Planix, Inc. <woods%planix.com@localhost> Avoncote Farms <woods%avoncote.ca@localhost>
Attachment:
pgph89_Kk4OGD.pgp
Description: OpenPGP Digital Signature