Path: utzoo!censor!geac!torsqnt!jarvis.csri.toronto.edu!rutgers!tut.cis.ohio-state.edu!ucbvax!hplabs!hpda!hpcupt1!hpisod2!decot From: decot@hpisod2.HP.COM (Dave Decot) Newsgroups: comp.unix.questions Subject: Re: Piping into Shell scripts Message-ID: <10650083@hpisod2.HP.COM> Date: 4 Mar 90 07:35:49 GMT References: <1990Feb28.142050.11607@virtech.uucp> Organization: Hewlett Packard, Cupertino Lines: 31 > >Is there any way of getting a Shell script (C or Bourne) to recognise > >whether it is receiving input from a pipe? > > tty(1) will report "not a tty" if it's input is not from a tty. > > So the following should work: > > testdata="`tty`" > if [ "$testdata" = "not a tty" ]; then > echo "we are not running from a terminal. therefore we must" > echo "be running from a pipe or with stdin redirected from a" > echo "file of some sort (of course, it also could be closed)" > > exit > fi This is not portable: many systems output different messages for this same condition, and some systems write the message on the standard *error* output. For instance, "Not a typewriter" is a message used on some systems. Instead, use: if tty -s then echo "Running from a terminal" else echo "Not running from a terminal" fi Dave Decot