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"?
I don't know about this, but I wonder about the possibility of just not shelling out to begin with and using Perl's file handling routines.
Right now the following script is giving me problems because whenever the mv command is executed the file names never get properly escaped.
#! /usr/bin/perl
use warnings; use strict;
my @output = `ls`; my $count = 1;
If we drop the my `ls` trick above, we can use Perl's directory handling calls:
use Cwd; opendir DIR, getcwd() or die "Directory error: $!\n";
The we can change the loop below to:
while (<DIR>) {
while ($_ = shift @output) { chomp $_;
chomp() works on $_ by default, so you can just use:
chomp;
# print $_, "\n"; # $_ =~ s/ /\\ /;
Ditto.
s/ /\\ /; # is the same as the line above.
print "mv ./${_} ./${count}.jpg\n" unless $_ =~ /rename.pl/;
First, $_ =~ /rename.pl/ is not a pattern match. To test equality use $_ eq 'rename.pl'.
I assume this print is working fine, but if you wanted to change it into an actual action use Perl's rename().
perldoc -f rename
$count++; }
Thanks in advance,
I hope that helps you. It's really just better to avoid the shell when Perl can already do what you need.
James
-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]