What shell is running?

Guy Harris guy at auspex.UUCP
Sun Feb 19 05:47:31 AEST 1989


>How can a running program tell what shell spawned it?

With great difficulty, at best.

 >Here is my problem.  I have a Curses-based application that must work
 >properly when its input, output, or both are redirected.  This program
 >must not coredump, even if the user does strange things like invoke it
 >interactively in the background.  But the Curses implementation I am
 >using likes to coredump under certain situations like this when running
 >under csh.

Well, step 1 is to report that as a bug; if a library supplied with your
system dumps core in situations like that, it's not a feature, it's a
bug.

 >My workaround is to not permit the application to run at all when invoked
 >in the background under csh when either stdin or stdout is not redirected.
 >To determine which shell is running at the time, I am using a horrible
 >kludge:  Check the line discipline the tty driver is using; if its the new
 >discipline then assume csh, otherwise assume sh.

Sorry, wrong answer.  That won't work in SunOS 4.0, for example; there
*is* no (moral equivalent of) the "old" line discipline in 4.0.  The
same will probably be true of POSIX-style tty drivers, such as the one
Berkeley's working on.

It also won't work on systems with job-control shells other than "csh",
such as the Korn shell or the BRL Bourne shell (or the S5R4 Bourne
shell).

However, I'm not sure whether your problems are caused by anything
specific to "csh"; they may be caused by job-control shells in general.

 >To determine background-ness, I compare the tty's process group with
 >my process id, and assume background if they're different.

This determines "job control shell"-style background-ness; if you're not
running under a job control shell, this test will probably not say that
you're in the background, even if you are.  As such, it may just be
sufficient to have your application test whether it's in the background;
if it is, it's running under "csh" or some other job control shell, and
if it isn't, it's either running in the foreground or running in the
background under a non-job-control shell (such as "sh").

The test may differ on systems with POSIX job control.



More information about the Comp.unix.wizards mailing list