kirk123 has asked for the wisdom of the Perl Monks concerning the following question:

Hi

How do yo execute a unix command in perl and get its output.

Unix command is:

ls -l $name | wc -l | sed 's/^ *//'
As you can see $name is a perl varible.

I know you have to use backtick.

I got error when I did it this way:

my $number = `ls -l $name | wc -l | sed 's/^ *//''
--kirk123

Replies are listed 'Best First'.
Re: Executing unix command in perl and getting the output
by Enlil (Parson) on Jan 14, 2003 at 00:20 UTC
    you need another backtick at the end.From this:
    my $number = `ls -l $name | wc -l | sed 's/^ *//'';
    to this:
    my $number = `ls -l $name | wc -l | sed 's/^ *//'`;

    -enlil

Re: Executing unix command in perl and getting the output
by rob_au (Abbot) on Jan 14, 2003 at 00:32 UTC
    I must say I am curious why one would want to invoke an additional instance of the shell in order to perform this task when it could easily be carried out within Perl - For example the above shell call could be reduced to the following:

    my $number = scalar @{[ glob $name ]};

    I have written about this topic before from a security perspective in the node - Think beyond Taint and warnings

     

    perl -le 'print+unpack("N",pack("B32","00000000000000000000001000010110"))'

      Actually, I was trying to execute the unix command lpstat -o and passing in the variable $name. Which is the result of another command and whose value I don't know. Thanks for input --kirk123