Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

pipes

Status
Not open for further replies.

Corwinsw

Programmer
Sep 28, 2005
117
BG
Code:
#!/usr/bin/perl -w
use strict;

open READ, "| ls";
print READ "/root/Desktop/proba/";

Why this don't work? How pipes work?




Corwin
 
When you want to read the output of a process into your script you put the pipe symbol (the |) at the end of the command which starts that process. Try it like this:
Code:
# first start the process
open(READ,'ls /root/Desktop/proba/ |');
# and now read the output from that process one line at a time
print "Start of output from ls command\n";
while(<READ>){
# for this example I'm just going to print each line from ls
[tab]print $_, "\n";
}
print "end of output from ls command\n";


Mike

You cannot really appreciate Dilbert unless you've read it in the
original Klingon.

Want great answers to your Tek-Tips questions? Have a look at faq219-2884

 
No I actually understood that with the output with the pipes. I want to write on the STDIN of the pipe, and use | in front of the command.
Perl Cookbook:
[cite]
The leading pipe symbol in the filename argument to open tells Perl to start another process instead. It connects the opened filehandle to the process's STDIN. Anything you write to the filehandle can be read by the program through its STDIN. When you close the filehandle, the opened process will get an eof when it next tries to read from STDIN.
[/cite]
So when I call my previous source code, I should giver /root/desktop/proba at the STDIN of ls and it should print what is in this directory.

PS. Mike sorry for tormenting u, but just this is interesting for me and i wanna understand it

Corwin
 
Hello Corwin,

I corrected your code because the ls command doesn't read it's STDIN at all as far as I know, so all you can do with that command is read from its STDOUT.

As far as I know the correct way to tell ls that you want it to read the /root/desktop/proba directory is to specify that directory on the command line - rather than trying to write to ls's STDIN.

Regards,

Mike
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top