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

On my Sun box I can execute the following statement in Korn Shell...

grep orderNumber $(find /home/homedepot/ECOM/prod/in -mtime -1) | wc -l

But this won't execute in Perl backticks

$x = ` grep orderNumber $(find /home/homedepot/ECOM/prod/in -mtime -1) | wc -l`;

The error I get is...

sh: syntax error at line 1: `(' unexpected

It looks like a bourne shell issue. Is there any way I can execute that command in Korn shell with perl?

Replies are listed 'Best First'.
Re: Perl 'pipe' problem
by bart (Canon) on Apr 24, 2003 at 00:39 UTC
    Likely you should retain the '$' character in the command line, so escape it, by preceding it with a backslash. Otherwise, perl will fill in the value of the variable $(.
    $x = `grep orderNumber \$(find /home/homedepot/ECOM/prod/in -mtime -1) + | wc -l`;
    Alternatively, use the qx with single quotes:
    $x = qx'grep orderNumber $(find /home/homedepot/ECOM/prod/in -mtime -1 +) | wc -l';
Re: Perl 'pipe' problem
by grep (Monsignor) on Apr 23, 2003 at 23:22 UTC
    Send it as an array to avoid interpolation issues.
    my @cmd = qw!grep orderNumber $(find /home/homedepot/ECOM/prod/in -mti +me -1) | wc -l!; my @lines = `@cmd`

    and if you want to use Korn shell the just append /usr/bin/ksh on the front.

    grep
    Mynd you, mønk bites Kan be pretti nasti...

      I just tried it and I get the same error

      sh: syntax error at line 1: `(' unexpected
        ok try
        /bin/bash -c 'grep perl $(find . -mtime -1) | wc -l'

        Obviously, I changed the command a little so I would get some results on my box :) but the command structure is the same as yours.

        I'm running Linux and it's working fine for me.

        grep
        Mynd you, mønk bites Kan be pretti nasti...

Re: Perl 'pipe' problem (or sh monks)
by runrig (Abbot) on Apr 24, 2003 at 00:56 UTC
    If the file names don't have spaces or other metacharacters, you can use xargs (and then the command won't fail if there are too many files):
    find blah blah blah | xargs grep orderNumber | wc -l # Update: # or even: find blah blah blah | xargs grep -c orderNumber | awk '{tot+=$1} END{print tot}'