Re: unix pipes to perl scripts

From: Josef Moellers (josef.moellers_at_fujitsu-siemens.com)
Date: 03/14/05


Date: Mon, 14 Mar 2005 15:33:53 +0100

trt. wrote:
> Got a problem piping command output to a perl script, in the test below
> the loop accepts the piped files but it also interferes with my STDIN!
>
> ===test=============================
> $ls -1 *.cfg | ./pp
> file: hosts.cfg
> file: pseries.cfg
>
> type something : you typed :
> ===test=============================
>
> I do not get the chance to type something in after "type something"...
>
> Do i have to flush some variable inbetween the foreach loop and
> <STDIN>?
> Is looping trough <ARGV> or <> the best way to catch piped input?

The STDIN of your pp script is redirected from the keyboard to the
STDOUT of the ls command. It is not automagically reconnected to the
keyboard once the ls command has terminated.
It would not be a good idea to reconnect to the keyboard, as quite a lot
of scripts depend upon pipelines to perform certain tasks (this is a
design feature of the UN*X/Linux architecture).

If you must read some more input from the keyboard, you can open /dev/tty:

#!/usr/bin/perl

while ( <> ) {
   print "file: $_";

};

open STDIN, '<', '/dev/tty'; #################################
print "\ntype something : ";
$answer = <STDIN>;
print "you typed : $answer \n";

Note that you'll get problems with buffering, though.

Josef

-- 
Josef Möllers (Pinguinpfleger bei FSC)
	If failure had no penalty success would not be a prize
						-- T.  Pratchett


Relevant Pages

  • Re: Testing for loops
    ... I tried this while loop ... > path to a binary than to just specifiy the executable name. ... Try timing a script written ... command in vi, or the abbrev mode in emacs. ...
    (comp.unix.shell)
  • Re: probleme with read command in a loop
    ... >I made a script to remove accentuated characters from a series of file name ... Because the standard input (file descriptor 0) is redirected within ... the loop since your loop is at the end of a pipe-line. ... is the output of the find command from the previous pipe. ...
    (comp.unix.shell)
  • Re: Strange errors
    ... script, or at another point in the loop. ... you can do classin the command window at that ... why does the same exact code not ...
    (comp.soft-sys.matlab)
  • Re: searching synchronously or asynchronously
    ... computer and immediately return to the loop, ... I have the below script. ... Is there a way to run the script, connect to all computers in the command ... If fName NetPath and fName LocalPath and _ ...
    (microsoft.public.scripting.wsh)
  • Re: RIS Location Problem
    ... What I think is required is a clver little script to remove the US Keyboard ... > Create a batch file by using the following command line to apply the ... > echo [RegionalSettings]> c:\regopts.txt ...
    (microsoft.public.windows.server.setup)