[comp.unix.questions] correct isatty usage

Pabbisetty.henr@xerox.com (Nagesh Pabbisetty) (02/22/89)

> I am trying to use this invokation:
>
>         if (isatty (stdin))


Use the following segment of code:

	if (isatty(fileno(stdin))) {
  	        printf("Hello> ");
   	       fflush(stdout);
   	       }

Reason why (isatty (stdin)) will not work:

	isatty requires a file descriptor as an argument but you were trying to
	use it with a file pointer.
	
	 
	
Hope this helps...

Nagesh
716-427-1827 / 5458

dave@tikal.Teltone.COM (news) (02/24/89)

In article <18432@adm.BRL.MIL> Pabbisetty.henr@xerox.com (Nagesh Pabbisetty) writes:
>> I am trying to use this invokation:
>>
>>         if (isatty (stdin))
>
>
>Use the following segment of code:
>
>	if (isatty(fileno(stdin))) {
>  	        printf("Hello> ");
>   	       fflush(stdout);
>   	       }

Now my question is, how would you do this in a script?  Either C or Bourne.
In C shell, I have this code, but it doesn't appear to work.

if ({`test -t 0`}) then
    echo -n "==> "
else
    echo -n "--> "
endif

It should print "-->" if stdin isn't coming from a terminal, but it doesn't
seem to work that way.  It always prints "==>".

-- 
----------------------------------------
David Karr    dave@tikal.Teltone.COM  or  ...uw-beaver!tikal!dave
Teltone Corp., 10801 120th Ave. NE, Kirkland, WA 98033 (206)827-9626
"The above statements do not necessarily reflect the opinions of my employer."

lvc@cbnews.ATT.COM (Lawrence V. Cipriani) (02/24/89)

In article <1161@tikal.Teltone.COM>, dave@tikal.Teltone.COM (news) writes:
+ Now my question is, how would you do this in a script?  Either C or Bourne.
+ In C shell, I have this code, but it doesn't appear to work.
+ 
+ if ({`test -t 0`}) then
+     echo -n "==> "
+ else
+     echo -n "--> "
+ endif

What you want is (Bourne shell or Korn shell):

	if [ -t 0 ]
	then
		echo stdin is a terminal
	else
		echo stdin is not a terminal
	fi
-- 
Larry Cipriani, att!cbnews!lvc or lvc@cbnews.att.com

maart@cs.vu.nl (Maarten Litmaath) (02/24/89)

In article <1161@tikal.Teltone.COM> dave@tikal.UUCP (David Karr) writes:
\if ({`test -t 0`}) then

	if ({ test -t 0 }) then
or
	if { test -t 0 } then

You want the exit status instead of the standard output from test.
-- 
 "Those who do not understand Henry     |Maarten Litmaath @ VU Amsterdam:
Spencer are condemned to reinvent DOS." |maart@cs.vu.nl, mcvax!botter!maart

alfie@warwick.UUCP (Nick Holloway) (02/28/89)

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 </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@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.