Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Calling pax within a perl script (was stupid substitution question)

by viffer (Beadle)
on Aug 11, 2010 at 09:36 UTC ( #854292=perlquestion: print w/replies, xml ) Need Help??

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

Thanks for everyones comments on the issue I was having simply replacing a '.' with '\.'. I've now realized that wasn't the issue. The code I had was working, it was looking at in in perl debug mode that was giving me a strange result.
I receive a tarred file that has the directory path within it. What I'm trying to do is untar the file into a different directory using pax. I thought the problem was due to the substitution not recognizing full stops within the directory name. However I have now found that whilst the command
pax -r -f pax.tar -s'/\/home\/kev\/pax\// \/data\/kev\/atlanta\/emtex\/invoices\//p'
works on the command line with the following result
$ tar -tvf pax.tar drwxrwxr-x 252 1 0 Aug 11 14:12:55 2010 /home/kev/pax/ -rw-rw-r-- 252 1 0 Aug 11 14:12:52 2010 /home/kev/pax/t1 -rw-rw-r-- 252 1 0 Aug 11 14:12:54 2010 /home/kev/pax/t2 -rw-rw-r-- 252 1 0 Aug 11 14:12:55 2010 /home/kev/pax/t3 $ pax -r -f pax.tar -s'/\/home\/kev\/pax\// \/data\/kev\/atlanta\/emte +x\/invoices/p' /home/kev/pax/t1 >> /data/kev/atlanta/emtex/invoices/t1 /home/kev/pax/t2 >> /data/kev/atlanta/emtex/invoices/t2 /home/kev/pax/t3 >> /data/kev/atlanta/emtex/invoices/t3
when I try to embed the command into a perl script via
my $homedir="/home/kev"; chdir $homedir; my $cmd = "pax -r -f pax.tar -s'/\/home\/kev\/pax\// \/data\/kev\/atla +nta\/emtex\/invoices\//p'"; system ( $cmd );
I get the error
pax: 0511-660 A replacement string was not added : 0511-661 An invalid trailing option was specified with the -s option
So what I *should* have been asking was, what am I doing wrong when I embed this pax command into my perl script?
Thanks guys and gals
Kev

Replies are listed 'Best First'.
Re: Calling pax within a perl script (was stupid substitution question)
by JavaFan (Canon) on Aug 11, 2010 at 09:40 UTC
    That's because your backslashes get "eaten" by the double quotes in perl.

    Use q{} instead of the double quotes.

    Note also that pax allows any delimiter instead of /. Choosing a different one means you don't need backslashes at all.

      Thank you - appreciate your help. Whilst this works perfectly for
      my $cmd = q{pax -r -f /home/kev/pax.tar -s'/\/home\/kev\/pax\//\/data\/kev\/atlanta\/emtex\/invoices\//p'};

      When the directory name has several nodes and the filename being paxed/untarred is a variable I guess it isn't interpolating correctly

      $command= q{pax -r -f $file -s'/DirPart1.DirPart2.DirPart3/SubDir\//\/data\/kev\/atlanta\/emtex\/invoices\//p'};

      I have tried several variants of this, such as
      $command= q{pax -r -f $file -s'/DirPart1\.DirPart2\.DirPart3\/SubDir\//\/data\/kev\/atlanta\/emtex\/invoices\//p'};
      When running this I get the error:
      A file or directory in the path name does not exist.
      which makes me think the string isn't being interpolated correctly.
      I can see how using a different delimiter would obviate the need for using backslashes in the first example that works,
      but in the second example don't I need escape backslashes for the '.' if not for the subdir '/'?

        $file won't interpolate inside q{}. A handful of alternatives:
        • $command = "pax -r -f $file -s'!DirPart1.DirPart2.DirPart3/SubDir/!/data/kev/atlanta/emtex/invoices/!p'"
        • $command = qq{pax -r -f $file } . q{-s'/DirPart1.DirPart2.DirPart3\/SubDir\//\/data\/kev\/atlanta\/emtex\/invoices\//p'};
        • $command = sprintf q{pax -r -f %s -s'/DirPart1.DirPart2.DirPart3/SubDir\//\/data\/kev\/atlanta\/emtex\/invoices\//p'}, $file;
        • $command= q{pax -r -f $file -s'/DirPart1.DirPart2.DirPart3/SubDir\//\/data\/kev\/atlanta\/emtex\/invoices\//p'}; $command =~ s/\$file/$file/;
        Or something else.
Re: Calling pax within a perl script (was stupid substitution question)
by Utilitarian (Vicar) on Aug 11, 2010 at 09:42 UTC
    Try printing $cmd to see what it contains.
    print "Good ",qw(night morning afternoon evening)[(localtime)[2]/6]," fellow monks."

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2022-05-23 11:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (82 votes). Check out past polls.

    Notices?