|
From: Mahesh Asolkar on 15 Apr 2008 15:37 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
From: xhoster on 15 Apr 2008 15:48 Mahesh Asolkar <asolkar(a)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.
From: Frank Seitz on 15 Apr 2008 15:49 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
From: Mahesh Asolkar on 15 Apr 2008 17:08 On Apr 15, 12:49 pm, Frank Seitz <devnull4...(a)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
From: nntpman68 on 16 Apr 2008 02:38 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...(a)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
|
Pages: 1 Prev: FAQ 9.15 How do I parse a mail header? Next: Accessing a hash whose name is constructed |