From: Lao Ming on
Suppose I write a Bourne shell script and find later that I want to
grep its output as in:

./script | grep "$re"

Is there any way that I can determine in the shell script that a pipe
swallow the output?
I want to vary following events (following the output) if a pipe
exists on the command line.

Thanks.
From: Janis Papanagnou on
Lao Ming wrote:
> Suppose I write a Bourne shell script and find later that I want to
> grep its output as in:
>
> ./script | grep "$re"
>
> Is there any way that I can determine in the shell script that a pipe
> swallow the output?

Try this...

if [ -t 1 ] # test whether file descriptor #1 is connected to terminal
then echo tty
else echo no tty
fi


Janis

> I want to vary following events (following the output) if a pipe
> exists on the command line.
>
> Thanks.
From: Lao Ming on
On Mar 27, 2:29 am, Janis Papanagnou <janis_papanag...(a)hotmail.com>
wrote:
> Lao Ming wrote:
> > Suppose I write a Bourne shell script and find later that I want to
> > grep its output as in:
>
> >    ./script | grep "$re"
>
> > Is there any way that I can determine in the shell script that a pipe
> > swallow the output?
>
> Try this...
>
>   if [ -t 1 ]   # test whether file descriptor #1 is connected to terminal
>   then echo tty
>   else echo no tty
>   fi
>
> Janis
>
>
>
> > I want to vary following events (following the output) if a pipe
> > exists on the command line.
>
> > Thanks.

I'm not sure if I've sufficiently tested this (script named t1 below)
but it looks perfect! Thanks a bunch.

$ sh -x t1
+ [ -t 1 ]
+ echo tty
tty

$ !! |grep tty
sh -x t1 | grep tty
+ [ -t 1 ]
+ echo no tty
no tty
$
From: Michael Paoli on
Quite non-portable, but one may be able to do something from the shell
like:
$ [ -p /proc/self/fd/1 ] && echo P
$ [ -p /proc/self/fd/1 ] | cat && echo P
P
$

Not sure if there's portable way to do it in minimal POSIX environment
from shell,
but one can do it via C or with a bit of C or Perl utility (just stat
FD 1 and see if it's a pipe/fifo, or not).

On Mar 27, 2:19 am, Lao Ming <laomingliu(a)gmail.com> wrote:
> Suppose I write a Bourne shell script and find later that I want to
> grep its output as in:
>
>    ./script | grep "$re"
>
> Is there any way that I can determine in the shell script that a pipe
> swallow the output?
> I want to vary following events (following the output) if a pipe
> exists on the command line.
>
> Thanks.