Dan Anderson wrote:

> Is it possible to do something to a string so that it gets escaped for a
> shell command (i.e. if I'm executing a command and I have the file name
> "foo's bar.jpg" it will be escaped to: "foo\'s\ bar.jpg"?
> 
> Right now the following script is giving me problems because whenever
> the mv command is executed the file names never get properly escaped.

I find that a significant majority of actions I may want to do with the
filesystem are already solved in pure-perl, or at least in a module
that already deals with this.  File::Copy, I believe, has a move
function that would probably work well here.  But let's say you have a
specific executable that you need to use that has no equivalent perl
module (or writing that functionality in perl would be plain silly).

The way to do it is with the "safe" version of system:

> #! /usr/bin/perl
> 
> use warnings;
> use strict;
> 
> my @output = `ls`;
> my $count = 1;
> 
> while ($_ = shift @output) {
>   chomp $_;
> #   print $_, "\n";
> #   $_ =~ s/ /\\ /;
>   print "mv ./${_} ./${count}.jpg\n" unless $_ =~ /rename.pl/;

my @cmd = ('mv', "./$_", "./${count}.jpg");
print "cmd = ", join(" ", @cmd), "\n";
my $rc = system(@cmd);
if ($?)
{
  print "Error occured during the move...\n";
}

The shell should not be involved here.

>   $count++;
> }
> 
> 
> Thanks in advance,
> 
> Dan

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to