[comp.unix.wizards] How can I determine controlling tty...

tony@oha.UUCP (Tony Olekshy) (05/14/88)

In <2661@ttrdc.UUCP> levy@ttrdc.UUCP (Daniel R. Levy) writes:
>
> I am having a problem with always getting an error message back to the
> terminal from which the command was issued...
> Now, of course, if I knew the explicit name of the ex-controlling terminal,
> I could open that and send the error message to it.

I long ago tired of trying to reliably determine the current tty name. 
However, I also long ago standardized on a /sys/etc/login script that is
source'd in *every* user's .login.  /sys/etc/login includes a

			setenv TTY "`tty`"

which works (ie: does *not* set TTY to "not a tty") because stdio is connected
to the terminal while .login runs.

As a result, I can use getenv("TTY") in c programs and "$TTY" in shell scripts,
and if I find TTY undefined or empty I can say something intelligent to the
user.  This has the added advantage that you can run a background command with
stuff that would go to $TTY redirected to another place, just use;

		sh -c 'TTY=some_place my_command' &