Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > pipe used in perl - as in unix sense

Reply
Thread Tools

pipe used in perl - as in unix sense

 
 
amanjun@gmail.com
Guest
Posts: n/a
 
      10-23-2005
Hi


I am trying to use "pipe" as in the unix command line sense in Perl
below:



for(`ls $ARGV[0]`)

{

$contents = `ls $_ \| wc \| awk '{print $1}'` ;

print $contents if ($contents != 0) ;

}



Here I am trying to findout the empty directories in $ARGV[0].

It gives errors like:



sh: -c: line 2: syntax error near unexpected token `|w'

sh: -c: line 2: ` |wc'



Can any of you help me in implementing this function?

Thank you.

Manju

 
Reply With Quote
 
 
 
 
Tad McClellan
Guest
Posts: n/a
 
      10-23-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) <(E-Mail Removed)> wrote:

> I am trying to use "pipe" as in the unix command line sense in Perl
> below:
>
>
>
> for(`ls $ARGV[0]`)



You shouldn't shell out for things that are easily done in native Perl:

for ( glob $ARGV[0] )


> $contents = `ls $_ \| wc \| awk '{print $1}'` ;

^^
^^

Since you want that to be a shell variable (not a Perl variable),
you had better backslash the dollar sign.

You should always enable warnings and strict when developing Perl programs:

use warnings;
use strict;


--
Tad McClellan SGML consulting
(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
 
 
 
Brian Wakem
Guest
Posts: n/a
 
      10-23-2005
(E-Mail Removed) wrote:

> Hi
>
>
> I am trying to use "pipe" as in the unix command line sense in Perl
> below:
>
>
>
> for(`ls $ARGV[0]`)



What happens if $ARGV[0] eq ';rm -rf /' ?



--
Brian Wakem
Email: http://homepage.ntlworld.com/b.wakem/myemail.png
 
Reply With Quote
 
A. Sinan Unur
Guest
Posts: n/a
 
      10-23-2005
Brian Wakem <(E-Mail Removed)> wrote in news:(E-Mail Removed):

> (E-Mail Removed) wrote:
>
>> Hi
>>
>>
>> I am trying to use "pipe" as in the unix command line sense in Perl
>> below:
>>
>>
>>
>> for(`ls $ARGV[0]`)

>
>
> What happens if $ARGV[0] eq ';rm -rf /' ?


In addition to the deathly security hole, why would anyone want to spawn
about (3(n+1) + 1), where n is the number of lines ls returns, external
processes for something that can easily be done in Perl?

Sinan

--
A. Sinan Unur <(E-Mail Removed)>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/cl...uidelines.html

 
Reply With Quote
 
John W. Krahn
Guest
Posts: n/a
 
      10-23-2005
(E-Mail Removed) wrote:
>
> I am trying to use "pipe" as in the unix command line sense in Perl
> below:
>
> for(`ls $ARGV[0]`)


That won't work for all file names, better to use opendir/readdir or glob.

perldoc -f opendir
perldoc -f readdir
perldoc -f glob


> {
> $contents = `ls $_ \| wc \| awk '{print $1}'` ;


If you type that exactly as shown on the command line then your shell will
interpret \| as a file name. As shown, perl will interpolate $1 to whatever
$1 contains. You want:

$contents = `ls "$_" | wc -l`;

But that won't include "hidden" files in the count!


> print $contents if ($contents != 0) ;
> }




John
--
use Perl;
program
fulfillment
 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      10-23-2005
Brian Wakem <(E-Mail Removed)> wrote:
> (E-Mail Removed) wrote:
>
>> I am trying to use "pipe" as in the unix command line sense in Perl
>> below:
>>
>>
>> for(`ls $ARGV[0]`)

>
>
> What happens if $ARGV[0] eq ';rm -rf /' ?



The same thing that would happen if the user had typed that
same argument on its own command line.




--
Tad McClellan SGML consulting
(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
xhoster@gmail.com
Guest
Posts: n/a
 
      10-24-2005
Brian Wakem <(E-Mail Removed)> wrote:
> (E-Mail Removed) wrote:
>
> > Hi
> >
> >
> > I am trying to use "pipe" as in the unix command line sense in Perl
> > below:
> >
> >
> >
> > for(`ls $ARGV[0]`)

>
> What happens if $ARGV[0] eq ';rm -rf /' ?


The same thing as happens if you type 'rm -rf /' on the command line.

Xho

--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
 
Reply With Quote
 
xhoster@gmail.com
Guest
Posts: n/a
 
      10-24-2005
(E-Mail Removed) wrote:
> Hi
>
> I am trying to use "pipe" as in the unix command line sense in Perl
> below:
>
> for(`ls $ARGV[0]`)
>
> {
>
> $contents = `ls $_ \| wc \| awk '{print $1}'` ;
>
> print $contents if ($contents != 0) ;
>
> }



The immediately problem is that you are not stripping the new line from
the results of the first ``. But you are surely going about this the wrong
way in the first place, as others have pointed out.


Xho

--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
 
Reply With Quote
 
usenet@DavidFilmer.com
Guest
Posts: n/a
 
      10-24-2005
(E-Mail Removed) wrote:
> I am trying to use "pipe" as in the unix command line sense


This approach makes no sense, command-line or otherwise.

> $contents = `ls $_ \| wc \| awk '{print $1}'` ;


perldoc -q "How do I count the number of lines in a file"

 
Reply With Quote
 
usenet@DavidFilmer.com
Guest
Posts: n/a
 
      10-24-2005
> > $contents = `ls $_ \| wc \| awk '{print $1}'` ;

Oh, and FWIW, you should be able to do "wc -l" (show linecount only,
instead of wc | awk '{print $1}'). But don't do that in Perl.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[named pipe] i wanna know about validate of pipe handle of client lee, wonsun C++ 1 11-02-2004 04:29 AM
Why does IO::Pipe::END generate an EXCEPT pipe message? lvirden@gmail.com Perl Misc 1 06-02-2004 02:17 PM
my own perl "dos->unix"/"unix->dos" Robert Wallace Perl Misc 7 01-22-2004 10:59 PM
Skript that reads from Unix Pipe _AND_ interacts by getkey() schwerdy Python 2 10-27-2003 04:11 PM
Perl Help - Windows Perl script accessing a Unix perl Script dpackwood Perl 3 09-30-2003 02:56 AM



Advertisments