How to determine if input was pipe'd 
Author Message
 How to determine if input was pipe'd

I am writing a script which acts upon the output of another program, which may
be supplied to my script either as a filename (containing the data), or may be
piped through my script--ie, my script has two different calling syntax forms:
  1) other_prog >output_file
     my_script.pl output_file
-or-
  2) other_prog | my_script.pl

I need to be able to determine that if no filename argument is given, that
the user has pipe'd the output, or print the correct usage if not.  I'm not
sure how to determine this in Perl & would appreciate suggestions.

Thanks,
David

--

    /// Sr. Software Engineer  (310) 444-6150
\\\/// Philips Interactive Media of America (PIMA), Los Angeles, CA
 \XX/ [OPINIONS ARE MY OWN & DO NOT NECESSARILY REFLECT PHILIPS POLICIES]



Sun, 13 Apr 1997 01:57:58 GMT  
 How to determine if input was pipe'd

Quote:

>I am writing a script which acts upon the output of another program, which may
>be supplied to my script either as a filename (containing the data), or may be
>piped through my script--ie, my script has two different calling syntax forms:
>  1) other_prog >output_file
>     my_script.pl output_file
>-or-
>  2) other_prog | my_script.pl

>I need to be able to determine that if no filename argument is given, that
>the user has pipe'd the output, or print the correct usage if not.  I'm not
>sure how to determine this in Perl & would appreciate suggestions.

#!/usr/bin/perl

require 'sys/stat.ph';

$stdinMode = (stat STDIN)[2];

if (&S_ISFIFO ($stdinMode))
{
  print "stdin's a fifo\n";

Quote:
}

__END__

does the trick on mu SunOS 4.1.3 machines, on the Solaris machines the
h2ph output needs massaging and I'm too lazy to do it.

Hope this helps,

Mike

--
The "usual disclaimers" apply.    | Meiko
Mike Stok                         | 130C Baker Ave. Ext

Meiko tel: (508) 371 0088 x124    |



Thu, 17 Apr 1997 00:53:16 GMT  
 How to determine if input was pipe'd

Quote:

>I am writing a script which acts upon the output of another program, which may
>be supplied to my script either as a filename (containing the data), or may be
>piped through my script--ie, my script has two different calling syntax forms:
>  1) other_prog >output_file
>     my_script.pl output_file
>-or-
>  2) other_prog | my_script.pl

>I need to be able to determine that if no filename argument is given, that
>the user has pipe'd the output, or print the correct usage if not.  I'm not
>sure how to determine this in Perl & would appreciate suggestions.

You can use the "-t" operator to test if STDIN (or another fd) is opened
to a terminal or not. In your case, the program would look like this:

#!/usr/local/bin/perl

# test if no args and STDIN is a tty, or more than 1 arg.
if ( ($#ARGV == -1 && -t) || $#ARGV > 0 ) {
   die "usage:
 1) other_prog > output_file
    $0 output_file
-or-
 2) other_prog | $0
";

Quote:
}

while ( <> ) {
   ... etc ...

Glad to be able to help,

--
Jan-Pieter Cornet



Fri, 18 Apr 1997 18:50:06 GMT  
 How to determine if input was pipe'd

Quote:

>I am writing a script which acts upon the output of another program, which may
>be supplied to my script either as a filename (containing the data), or may be
>piped through my script--ie, my script has two different calling syntax forms:
>  1) other_prog >output_file
>     my_script.pl output_file
>-or-
>  2) other_prog | my_script.pl

>I need to be able to determine that if no filename argument is given, that
>the user has pipe'd the output, or print the correct usage if not.  I'm not
>sure how to determine this in Perl & would appreciate suggestions.

From your questions, I assume that you are using "while (<>) {" to iterate
over your input.  

When using "<>" to iterate, the variable $ARGV will hold the name of the
current file being read.   If you are reading from stdin:

        other_prog | my_script.pl
        my_script.pl < output_file

$ARGV will be "-".  If you are reading from a file it will be the name
of the file.

I don't think there's any easy way to tell where the data is coming from
when reading from stdin.  However you can tell if it coming from the user:
if (-t STDIN).   This test should only be used if you already know that
the data is coming from stdin ($ARGV eq "-").

You don't have to use <>.  Instead you can process the arguments to your

-Dave



Fri, 18 Apr 1997 09:15:57 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Can't read input pipe

2. How to determine AM or PM time

3. Input Dialog, What am I missing here?

4. Pipe, et al, what am I doing wrong?

5. Reading from an input pipe?

6. pipes for both input and output

7. opening pipe for input and output

8. Connecting input and output of pipes?

9. input from a pipe is buffered.

10. How do I turn off buffered input w/ pipes

11. read from STDIN prompt after piped input file

12. Piping input to output of a program through a Perl filter

 

 
Powered by phpBB® Forum Software