http://qs1969.pair.com?node_id=627124


in reply to Re^2: tar, system() & pipes?
in thread tar, system() & pipes?

I think the following is safe on unix platforms:

my $q_archive = quotemeta($ARGV[0]); my $q_filename = quotemeta($filename); system("tar $args $q_archive > $q_filename")

It can also be written as

system("tar $args \Q$ARGV[0]\E > \Q$filename\E")

quotemeta (called directly or via \Q..\E) will backslash-escape any non-word characters, so the file names can safely contain symbols and spaces.

Replies are listed 'Best First'.
Re^4: tar, system() & pipes?
by Anonymous Monk on Jul 17, 2007 at 20:55 UTC

    Thank you! Use of quotemeta appears to work.

    Why is this needed?
      Why is quotemeta needed or why do you have to call 'system' this way? For quotemeta the code will work for files with special characters in them. For system I imagine the LIST version exists to make it easier to pass in a large number of parameters, deal with ones with special characters, and/or build a list of parameters without having to join them later on. As explained by others the IO redirection ('>') is something the shell handles but when system is called in LIST context it will be passed to the tar command as a parameter instead. Because tar has no '>' command line option it errors out -- just like it does with any other incorrect parameter (e.g. -y).