correct isatty usage

Nick Holloway alfie at warwick.UUCP
Tue Feb 28 20:05:22 AEST 1989


In article <1161 at tikal.Teltone.COM> dave at tikal.UUCP (David Karr) writes:
> In article <18432 at adm.BRL.MIL> Pabbisetty.henr at xerox.com (Nagesh Pabbisetty) writes:
> >> [Doing isatty(FILE*) instead of isatty(int)]
> >
> > [Correct solution of isatty(fileno(FILE*))]
> 
> Now my question is, how would you do this in a script?  Either C or Bourne.

The method I use is tty(1). This will print out the name of the terminal
attached to stdin. This can be used more generally by using the -s flag (No
report just, return the status) and redirecting the input. 
This script is an example of this (call it demo):
	#!/bin/sh
	if tty -s <&0			# redirect from stdin
	then echo "stdin:  from terminal"
	else echo "stdin:  not from terminal"
	fi
	if tty -s <&1			# redirect from stdout
	then echo "stdout: from terminal"
	else echo "stdout: not from terminal"
	fi
	if tty -s <&2			# redirect from stderr
	then echo "stderr: from terminal"
	else echo "stderr: not from terminal"
	fi

Try running as:
	$ demo
	$ demo | cat
	$ demo 2>/dev/null
	$ demo 2>&1 | cat
	$ demo </dev/null 2>&1 | cat

> In C shell, I have this code, but it doesn't appear to work.
> 	[C-Shell script to try and find out whether connected to terminal]

I'm afraid I don't speak c-shell, so I can't comment on your script - although
if you are only interested in whether stdin is connected to terminal you can
use "tty -s" as above. When it comes to checking other streams - I don't know
enough about C-shell redirection (other than it is not as flexible as sh).
--
JANET       : alfie at uk.ac.warwick.cs                  |  `O O'  | Nick Holloway
BITNET/EARN : alfie%uk.ac.warwick.cs at ukacrl           | // ^ \\ | Comp Sci Dept
INTERNET    : alfie%cs.warwick.ac.uk at nss.cs.ucl.ac.uk +---------+ Uni of Warwick
UUCP        : ..!mcvax!ukc!warwick!alfie, alfie at warwick.UUCP    | Coventry, UK.



More information about the Comp.unix.questions mailing list