Beefy Boxes and Bandwidth Generously Provided by pair Networks RobOMonk
Syntactic Confectionery Delight
 
PerlMonks  

Shell characters coming back to bite me.

by mull (Monk)
on Mar 20, 2012 at 14:01 UTC ( #960577=perlquestion: print w/ replies, xml ) Need Help??
mull has asked for the wisdom of the Perl Monks concerning the following question:

Working on an old Darwin 8.1 box where there have been layers of ACLs applied. The only native tools supplied to work with the ACLs are extended versions of ls and chmod.

We need to move off this box so to help I'm creating a script that produces an ACL map for a given user.

To my delight I've found that the file and directory names contain lots of interesting characters I have to be able to handle.

So far this works but I'm concerned that it blows up if I encounter anything with a " character in the name.

I've been searching for a better way but I need hints.

#!/usr/bin/perl -w # trying to map acls use Getopt::Long; use File::Find; no warnings 'File::Find'; my $optuser = ''; my $findroot = ''; GetOptions('user:s' => \$optuser, 'root=s' => \$findroot); unless( $findroot ){ print<<HERE $0 --root path [--user username] perform a recursive find rooted at the given pathname producing an +ACL map. if optional username is provided, filter ACL map results to that us +ername. HERE } find(\&wanted, $findroot); sub wanted { -f && next; open RES, "ls -lde \"$_\" |"; # filespec is discarded <RES>; while (my $acl=<RES>){ if($optuser){ $acl =~ /user:$optuser\s/ && print "$File::Find::name $acl"; } else { print "$File::Find::name $acl"; } } }

I'm interested in the 3 arg version of open() but so far I've been unable to get it working for this task; the spaces and shell chars are passed to ls and it seems I'm back to square one.

open RES, "-|", "ls -lde $_";

Comment on Shell characters coming back to bite me.
Select or Download Code
Re: Shell characters coming back to bite me.
by moritz (Cardinal) on Mar 20, 2012 at 14:13 UTC
      Ok thanks a lot, that was exactly what I needed!
Re: Shell characters coming back to bite me.
by JavaFan (Canon) on Mar 20, 2012 at 14:31 UTC
    An answer how to use many arg open has already been given, and you probably should use that.

    For future reference, if I do have to pass something to the shell, but I need to be sure it will not blow up due to characters special to the shell, I do:

    my $thingy = "...."; $thingy =~ s/'/'"'"'/g; my $cmd = "womble womble '$thingy' wibble wibble";

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2013-05-25 05:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best material for plates (tableware) is:









    Results (518 votes), past polls