Path: utzoo!attcan!uunet!mcvax!ukc!warwick!alfie From: alfie@warwick.UUCP (Nick Holloway) Newsgroups: comp.unix.questions Subject: Re: correct isatty usage Keywords: isatty(3), tty(1) Message-ID: <1377@ubu.warwick.UUCP> Date: 28 Feb 89 10:05:22 GMT References: <18432@adm.BRL.MIL> <1161@tikal.Teltone.COM> Sender: news@warwick.UUCP Reply-To: alfie@uk.ac.warwick.cs (Nick Holloway) Organization: Computer Science, Warwick University, UK Lines: 45 In article <1161@tikal.Teltone.COM> dave@tikal.UUCP (David Karr) writes: > In article <18432@adm.BRL.MIL> Pabbisetty.henr@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 &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@uk.ac.warwick.cs | `O O' | Nick Holloway BITNET/EARN : alfie%uk.ac.warwick.cs@ukacrl | // ^ \\ | Comp Sci Dept INTERNET : alfie%cs.warwick.ac.uk@nss.cs.ucl.ac.uk +---------+ Uni of Warwick UUCP : ..!mcvax!ukc!warwick!alfie, alfie@warwick.UUCP | Coventry, UK.