Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Problem with Parentheses

by jjoyce (Initiate)
on Aug 21, 2012 at 21:02 UTC ( [id://988869]=perlquestion: print w/replies, xml ) Need Help??

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

This script executes in my Linux without problem:

 find . -type f \( -perm -0020 -o -perm -0004 -o -perm -0002 -o -perm -0001 -o -perm -1000 \) -ls

However, when I try to embedded it on a Perl code:

#!/usr/bin/perl my $out = "output.txt"; open (MYFILE,"> $out"); print MYFILE `find . -type f \( -perm -0020 -o -perm -0004 -o -perm -0 +002 -o -perm -0001 -o -perm -1000 \) -ls`;

I receive the following error message:

sh: -c: line 0: syntax error near unexpected token `(' sh: -c: line 0: `find . -type f /( -perm -0020 -o -perm -0004 -o -perm -0002 -o -perm -0001 -o -perm -1000 /) -ls'

How can I fix the above code?

Replies are listed 'Best First'.
Re: Problem with Parentheses
by choroba (Cardinal) on Aug 21, 2012 at 21:12 UTC
    Double-escape the parentheses:
    print MYFILE `find . -type f \\( -perm -0020 -o -perm -0004 -o -perm - +0002 -o -perm -0001 -o -perm -1000 \\) -ls`;
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      Thank you for your help, this solves my problem.
Re: Problem with Parentheses
by toolic (Bishop) on Aug 21, 2012 at 21:11 UTC
    This avoids the error for me:
    my $cmd = 'find . -type f \( -perm -0020 -o -perm -0004 -o -perm -0002 + -o -perm -0001 -o -perm -1000 \) -ls'; my $out = "output.txt"; open (MYFILE,"> $out"); print MYFILE `$cmd`;

    See also:

Re: Problem with Parentheses
by kcott (Archbishop) on Aug 21, 2012 at 21:21 UTC

    The code you show has backslashes (\( and \)) but the error message shows forward slashes (/( and /)). Regardless, the code you need has two backslashes (in each position):

    $ perl -Mstrict -Mwarnings -e ' print `find . -type f \\( -perm -0020 -o -perm -0004 -o -perm -0002 -o + -perm -0001 -o -perm -1000 \\) -ls`;' ... ls listing here ...

    -- Ken

Re: Problem with Parentheses
by james2vegas (Chaplain) on Aug 22, 2012 at 00:44 UTC
    Also worth noting, you can use File::Find to duplicate this functionality without running a separate process.

    Running find2perl . -type f \( -perm -0020 -o -perm -0004 -o -perm -0002 -o -perm -0001 -o -perm -1000 \) -ls returns this equivalent Perl code (find2perl here):

    Obviously this code could be optimised.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://988869]
Approved by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2024-03-19 08:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found