Piping into Shell scripts
Dave Decot
decot at hpisod2.HP.COM
Sun Mar 4 17:35:49 AEST 1990
> >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
More information about the Comp.unix.questions
mailing list