How to know if data is piped into my script

How to know if data is piped into my script

am 15.04.2008 21:37:25 von Mahesh M

I have the following script:

-----
#!/usr/bin/perl

use strict;
use warnings;

my %hist = ();
my @hist;

$hist{(split /\s+/)[3]}++
foreach (`$ENV{'SHELL'} -c history`);

print map {"$hist{$_} : $_\n"}
sort {$hist{$b} <=> $hist{$a}}
keys %hist;
-----

I want to change it such that it uses '`$ENV{'SHELL'} -c history`'
only if nothing is piped into the script. If I call the script like:

% myscript.pl

It should use '`$ENV{'SHELL'} -c history`'. However if it is called
like:

% history | myscript.pl

It should use the piped in data instead.

What would be the best approach?

Thanks,
Mahesh

Re: How to know if data is piped into my script

am 15.04.2008 21:48:21 von xhoster

Mahesh Asolkar wrote:
> I have the following script:
>
> -----
> #!/usr/bin/perl
>
> use strict;
> use warnings;
>
> my %hist = ();
> my @hist;
>
> $hist{(split /\s+/)[3]}++
> foreach (`$ENV{'SHELL'} -c history`);
>
> print map {"$hist{$_} : $_\n"}
> sort {$hist{$b} <=> $hist{$a}}
> keys %hist;
> -----

Assuming it is safe to load all input into memory at once:

my @x=<>;
foreach (@x? @x : `$ENV{'SHELL'} -c history`) {

This will do what <> does, not just pipes. If you want only STDIN, and
only when STDIN is hooked up to a pipe, then maybe:

foreach (-p STDIN ? : `$ENV{'SHELL'} -c history`) {


Xho

--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.

Re: How to know if data is piped into my script

am 15.04.2008 21:49:06 von Frank Seitz

Mahesh Asolkar wrote:
>
> It should use '`$ENV{'SHELL'} -c history`'. However if it is called
> like:
>
> % history | myscript.pl
>
> It should use the piped in data instead.

unless (-t) {
# STDIN is not opend to a tty but a file or pipe
}

Frank
--
Dipl.-Inform. Frank Seitz; http://www.fseitz.de/
Anwendungen für Ihr Internet und Intranet
Tel: 04103/180301; Fax: -02; Industriestr. 31, 22880 Wedel

Re: How to know if data is piped into my script

am 15.04.2008 23:08:03 von Mahesh M

On Apr 15, 12:49=A0pm, Frank Seitz wrote:
> Mahesh Asolkar wrote:
>
> > It should use =A0'`$ENV{'SHELL'} -c history`'. However if it is called
> > like:
>
> > =A0 % history | myscript.pl
>
> > It should use the piped in data instead.
>
> unless (-t) {
> =A0 =A0 # STDIN is not opend to a tty but a file or pipe
>
> }
>

Thanks Frank and Xho. -t and -p do exactly what I was looking for.

Xho, I had tried the first way you suggested too. It works in the case
of 'history | myscript.pl', but it blocks 'myscript.pl' waiting for <>
to return.

Here's the script with the changes (I picked -p):

---------
#!/usr/bin/perl

use strict;
use warnings;

my %hist =3D ();
my @hist;

$hist{(split /\s+/)[3]}++
foreach ((-p STDIN) ? : `$ENV{'SHELL'} -c history`);

print map {"$hist{$_} : $_\n"}
sort {$hist{$b} <=3D> $hist{$a}}
keys %hist;
---------

/Mahesh

Re: How to know if data is piped into my script

am 16.04.2008 08:38:49 von nntpman68

Hi Makesh,


Small clarification.

-p checks whether input is a pipe
and "-p STDIN" would capture the case:

program | perlscript.pl


whereas -t CHECKS WHETHER input is a terminal.
so "unless -t STDIN" would also capture

perlscript.pl < filename


bye

N


Mahesh Asolkar wrote:
> On Apr 15, 12:49 pm, Frank Seitz wrote:
>> Mahesh Asolkar wrote:
>>
>>> It should use '`$ENV{'SHELL'} -c history`'. However if it is called
>>> like:
>>> % history | myscript.pl
>>> It should use the piped in data instead.
>> unless (-t) {
>> # STDIN is not opend to a tty but a file or pipe
>>
>> }
>>
>
> Thanks Frank and Xho. -t and -p do exactly what I was looking for.
>
> Xho, I had tried the first way you suggested too. It works in the case
> of 'history | myscript.pl', but it blocks 'myscript.pl' waiting for <>
> to return.
>
> Here's the script with the changes (I picked -p):
>
> ---------
> #!/usr/bin/perl
>
> use strict;
> use warnings;
>
> my %hist = ();
> my @hist;
>
> $hist{(split /\s+/)[3]}++
> foreach ((-p STDIN) ? : `$ENV{'SHELL'} -c history`);
>
> print map {"$hist{$_} : $_\n"}
> sort {$hist{$b} <=> $hist{$a}}
> keys %hist;
> ---------
>
> /Mahesh