Problem with cp in system call running Mac OS X

T

Tom McDonough

I'm writing a back up script and want to put all files in directory
(MRIS) which are not a directory in their own right into a temporary
directory (TEMP).

I'm operating in Mac OS X 10.2.8 with the perl 5.6 that came with the
distro and the cp is 4th Berkeley Distribution April 18, 1994

From the shell my cp is working as I expect but when I use cp in a
perl script in a system call I get odd - to my mind - results.

When the script is run from the parent directory, this:

$copy = `cp mris/$thisone mris/temp/$thisone`;

Elicits this:
usage: cp [-R [-H | -L | -P]] [-f | -i] [-p] src target
cp [-R [-H | -L | -P]] [-f | -i] [-p] src1 ... srcN directory
sh: mris/temp/test.pl: No such file or directory

And this:

$copy = `cp mris/$thisone mris/temp`;

Results in this:
usage: cp [-R [-H | -L | -P]] [-f | -i] [-p] src target
cp [-R [-H | -L | -P]] [-f | -i] [-p] src1 ... srcN directory
sh: mris/temp: is a directory

When I go down to the mris directory and run the script, this

$copy = `cp $thisone temp`;

Results in this:
usage: cp [-R [-H | -L | -P]] [-f | -i] [-p] src target
cp [-R [-H | -L | -P]] [-f | -i] [-p] src1 ... srcN directory
sh: temp: command not found

BTW: cd doesn't seem to work in a system call either. Script is
copied below.

Stymied in Stanton
*******************
#!/usr/bin/perl

# this script is located in mris

@ls = `ls -l`;
foreach $file (@ls) {
if ($file =~ /^d/) {
@file = split / /, $file;
$thisone = pop @file;
print "$thisone";
}
else {
@file = split / /, $file;
if (!($file =~ 'total')) {
$thisone = pop @file;
$copy = `cp $thisone temp/$thisone`;
}
}
}
 
T

Todd de Gruyl

#!/usr/bin/perl

use warnings;
use strict;

(and fix the warnings that shows - have a look at the posting guidelines
that are posted here regularly)
# this script is located in mris

@ls = `ls -l`;
foreach $file (@ls) {
if ($file =~ /^d/) {
@file = split / /, $file;
$thisone = pop @file;
print "$thisone";
}
else {
@file = split / /, $file;
if (!($file =~ 'total')) {
$thisone = pop @file;

$thisone includes the final \n of the original input, try adding:

chomp $thisone;
$copy = `cp $thisone temp/$thisone`;
}
}
}

You also might want to check out readdir and opendir (e.g. perldoc -f
readdir...), that might make the code you are looking at easier to read
(of course, the ls command isn't all that hard to deal with, YMMV)

HTH
 
T

Tintin

Tom McDonough said:
*******************
#!/usr/bin/perl

# this script is located in mris

@ls = `ls -l`;
foreach $file (@ls) {
if ($file =~ /^d/) {
@file = split / /, $file;
$thisone = pop @file;
print "$thisone";
}
else {
@file = split / /, $file;
if (!($file =~ 'total')) {
$thisone = pop @file;
$copy = `cp $thisone temp/$thisone`;
}
}
}


Ugg! Either write a shell script or a Perl script.

Here's a Perl version.

#!/usr/bin/perl
use strict;
use File::Copy;

foreach my $file (<*>) {
next unless -f $file;
copy $file ,"temp/$file" or warn "Can not copy $file $!\n";
}
 
J

Joe Smith

Tom said:
$copy = `cp mris/$thisone mris/temp/$thisone`;

Elicits this:
usage: cp [-R [-H | -L | -P]] [-f | -i] [-p] src target
cp [-R [-H | -L | -P]] [-f | -i] [-p] src1 ... srcN directory
sh: mris/temp/test.pl: No such file or directory

Any time you have a problem with backticks or system(), the first thing
you should do is print out the command that you think you are executing.

Adding a simple
print "About to execute 'cp mis/$thisone mris/temp/$thisone'\n";
would have immediately pointed out the source of your problem.
-Joe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top