pipe used in perl - as in unix sense

Discussion in 'Perl Misc' started by amanjun@gmail.com, Oct 23, 2005.

  1. Guest

    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
     
    , Oct 23, 2005
    #1
    1. Advertising

  2. <> 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
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Oct 23, 2005
    #2
    1. Advertising

  3. Brian Wakem Guest

    Brian Wakem, Oct 23, 2005
    #3
  4. Brian Wakem <> wrote in news::

    > 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 <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
     
    A. Sinan Unur, Oct 23, 2005
    #4
  5. 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
     
    John W. Krahn, Oct 23, 2005
    #5
  6. Brian Wakem <> wrote:
    > 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
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Oct 23, 2005
    #6
  7. Guest

    Brian Wakem <> wrote:
    > 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
     
    , Oct 24, 2005
    #7
  8. Guest

    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
     
    , Oct 24, 2005
    #8
  9. Guest

    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"
     
    , Oct 24, 2005
    #9
  10. Guest

    > > $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.
     
    , Oct 24, 2005
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dpackwood
    Replies:
    3
    Views:
    1,839
  2. lee, wonsun
    Replies:
    1
    Views:
    499
    Jack Klein
    Nov 2, 2004
  3. schwerdy
    Replies:
    2
    Views:
    353
    schwerdy
    Oct 27, 2003
  4. Robert Wallace

    my own perl "dos->unix"/"unix->dos"

    Robert Wallace, Jan 21, 2004, in forum: Perl Misc
    Replies:
    7
    Views:
    301
    Michele Dondi
    Jan 22, 2004
  5. Replies:
    1
    Views:
    241
    Ben Morrow
    Jun 2, 2004
Loading...

Share This Page