jalebie has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I am trying to do a rcp command within a system command, and for some reason whether the system command always returns a 0, irrespective of whether the rcp call works or fails. Please look at my simplified version of my code and let me know what i am doing wrong here.


use strict; #create the file mine.txt if doesn't exist if (! -e "mine.txt") { system("echo 'hey you' > mine.txt"); } #delete the file min.txt if exists if (-e "min.txt") { print "File unlink.txt ".unlink(<min.txt>)."deleted \n"; } my $cmd1 = "rcp mine.txt cmd1.txt"; my $cmd2 = "rcp min.txt cmd2.txt"; my ($val1, $val2); if ($val1 = system("$cmd1")) { print "$cmd1 failed \n"; } if ($val2 = system("$cmd2")) { print "$cmd2 failed \n"; } print "val1: $val1 \n"; print "val2: $val2 \n"; print " done \n";


The result i get is as follows:
cp: cannot access min.txt val1: 0 val2: 0 done

Replies are listed 'Best First'.
Re: unix rcp command within a system call
by Monky Python (Scribe) on Aug 28, 2001 at 20:59 UTC
    I assume that this is a problem from the rcp command itself,
    because if you try the same in a terminal you also get 0 as return value.
    rcp mine.txt cmd1.txt; echo $? /bin/cp: mine.txt:cannot access min.txt 0
    but if you use cp you'll get a correct return value:
    cp mine.txt cmd1.txt; echo $? /bin/cp: mine.txt:cannot access min.txt 1

    maybe it's possible for you to use cp instead of rcp.

    MP

      I know, but I have to use the rcp command to copy files across different machines. This code is actually a part of a larger program that i said earlier.

Re: unix rcp command within a system call
by jlongino (Parson) on Aug 28, 2001 at 21:15 UTC
    If your code executes correctly, min.txt does not exist. You test to see if it exists. Since your print statement does not appear in your output, the file doesn't exist. Then you try to copy min.txt (which doesn't exist) to cmd2.txt. Thus your error message.

    If the code and the comments disagree, then both are probably wrong. -- Norm Schryer

      i know i intentioanlly had it that way..... it was to check the return codes from a valid rcp call, and an invalid one... that is the reason i delete the min.txt if does exist i am trying to test here the return code by a valid vs. an invalid rcp command
        After perusing the man pages on rcp (there is at least one documented case in which rcp "may not correctly fail") and the given results you've had, I doubt you will be able to use it reliably without substantial, manual error checking of your own afterwards. E.g., manually checking after issuing the command to see if the resulting file or directory structures was successfully copied. That will probably necessitate the use of Net:Ftp or Net::Telnet module. Perhaps there is another module I'm unaware of.

        If you are just trying to copy a few files, I would use Net::Ftp. If you want to copy directory structures, you might look at using tar in conjunction with Net::Telnet.

        If the code and the comments disagree, then both are probably wrong. -- Norm Schryer