Bookmarks

Yahoo Gmail Google Facebook Delicious Twitter Reddit Stumpleupon Myspace Digg

Search queries

Use of assignment to $[ is deprecated at /usr/local/sbin/apxs line 86. , wwwxxx vim, mysql closing table and opening table, 800c5000, setgid operation not permitted, pciehp: acpi_pciehprm on IBM, WWWXXX.DBF, 078274121, info0a ip, should prodicers of software_based services be held liable or not liable for economic injuries

Links

XODOX
Impressum

#1: How to know if data is piped into my script

Posted on 2008-04-15 21:37:25 by 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

Report this message

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

Posted on 2008-04-15 21:48:21 by xhoster

Mahesh Asolkar <asolkar@gmail.com> 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 ? <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.

Report this message

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

Posted on 2008-04-15 21:49:06 by 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

Report this message

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

Posted on 2008-04-15 23:08:03 by Mahesh M

On Apr 15, 12:49=A0pm, Frank Seitz <devnull4...@web.de> 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) ? <STDIN> : `$ENV{'SHELL'} -c history`);

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

/Mahesh

Report this message

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

Posted on 2008-04-16 08:38:49 by 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 <devnull4...@web.de> 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) ? <STDIN> : `$ENV{'SHELL'} -c history`);
>
> print map {"$hist{$_} : $_\n"}
> sort {$hist{$b} <=> $hist{$a}}
> keys %hist;
> ---------
>
> /Mahesh

Report this message