pipe used in perl - as in unix sense

A

amanjun

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
 
T

Tad McClellan

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;
 
A

A. Sinan Unur

Brian Wakem said:
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
 
J

John W. Krahn

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
 
T

Tad McClellan

Brian Wakem said:
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.

:)
 
X

xhoster

Brian Wakem said:
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
 
X

xhoster

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
 
U

usenet

$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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top