Problem with cp in system call running Mac OS X

Discussion in 'Perl Misc' started by Tom McDonough, Nov 7, 2004.

  1. 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`;
    }
    }
    }
     
    Tom McDonough, Nov 7, 2004
    #1
    1. Advertising

  2. On 11/7/04 12:50, Tom McDonough wrote:
    > #!/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
    --
    Todd de Gruyl
     
    Todd de Gruyl, Nov 7, 2004
    #2
    1. Advertising

  3. Tom McDonough

    Tintin Guest

    "Tom McDonough" <> wrote in message
    news:...
    > *******************
    > #!/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";
    }
     
    Tintin, Nov 7, 2004
    #3
  4. Tom McDonough

    Joe Smith Guest

    Tom McDonough wrote:

    > $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
     
    Joe Smith, Nov 8, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. AznCollegeGuy
    Replies:
    7
    Views:
    12,963
  2. hg
    Replies:
    4
    Views:
    309
    goodepic
    Jan 5, 2007
  3. Fabio Mastria
    Replies:
    4
    Views:
    740
    Fabio Mastria
    Jan 28, 2008
  4. geert
    Replies:
    8
    Views:
    403
    Graham Dumpleton
    Mar 19, 2008
  5. Mr. Zeus
    Replies:
    6
    Views:
    273
    Ben Morrow
    Oct 13, 2004
Loading...

Share This Page