Subject: Re: Ways to detect an interactive sh
To: Giles Lean <giles@nemeton.com.au>
From: None <netbsd99@sudog.com>
List: netbsd-help
Date: 02/17/2003 09:47:20
On Saturday 15 February 2003 14:40, Giles Lean wrote:
> Davvid Forrai writes:
>
> > I used the TERM variable to determine whether it was interactive (i.e.
> > if TERM is set, sh is interactive).
> >
> > I'm wondering if there's a better or more elegant solution to make this
> > determination.
>
> You can use test(1) to find out if standard input or standard output
> is a tty:
>
> -t file_descriptor
> True if the file whose file descriptor number is
> file_descriptor is open and is associated with a
terminal.
>
> For years my .profile and .kshrc have tested standard output.
> Standard input would seem to make more sense, but it was many years
> ago that I set this up, so I don't know if there is a reason I test
> standard output or not.
>
> if [ -t 1 ]
> then
> # interactive stuff
> # ...
> fi
I've found another way as well: "tty". If it returns "not a tty" then it's
non-interactive. Else, it's interactive. Then you can push the logic behind
finding this kind of thing out behind a tool to worry about it for you.
-s