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

Hi.. Iam very new to perl. I was trying to write my first perl script. Iam not able to use native linux commands in my perl script. In my case iam trying to copy one file from a location into another location. to accomplish this iam using the native linux command within my script. ' cp -rf /source /Destination'; The version of RHEL iam using is 5.4. To my surprise i see the same script working perfectly with RHEL 5.3. Can someone give me an idea what could be wrong.
  • Comment on Not able to use native linux commands in a perl script

Replies are listed 'Best First'.
Re: Not able to use native linux commands in a perl script
by JavaFan (Canon) on Dec 13, 2010 at 18:03 UTC
    Care to post the code fragment you used to copy the file? And whatever error message you're getting? Also, the relevant return variables.

    Having said that, cp -rf /source /Destination looks strange to me. Do you really have a file called source in the root direction you want to copy to a file (or directory) called Destination, also in the root directory?

      use File::Copy;
      copy(source,dest);
      but if you need use native linux command you must use:
      `cp -rf /source /Destination`;
      ` - is reversive '
      or
      system('cp -rf /source /Destination');

      But using 'system' command is dangerous in some variants...
        The dangers are much overrated. They aren't specific to system, `` suffers from that as well (and system has a safer variant). However, if you accept input from untrusted sources, File::Copy is "dangerous" as well.

        But if you're in control of creating the command, neither option is more dangerous than you typing it on the command line.

      please see the code fagment below #! /usr/bin/perl print "Hi\n"; 'cp -rf /nihad /tmp/'; Here nihad is a file in my root directory and iam tying to copy this file to /tmp.
        I guess you failed to read my admonishment to use <code> tags... Let me make sure I have this right:
        #! /usr/bin/perl print "Hi\n"; `cp -rf /nihad /tmp/`;
        You say that 'nihad' is in 'my root directory'. The use of a possessive bothers me. Some people use 'my root dir' and 'my home dir' interchangeably. Also, since most linux installations have a /root directory (the home dir for user root), that is often what people refer to as the 'root directory'. If, indeed, the file 'nihad' is in the '/' directory, the only reason I can see for it not working is permissions. Does the user you are running the script under have read-permission for both the file 'nihad' and the directory '/'? What happens when you type this command from the shell, as the same user (without the 'f' option, so it will give you an error message if it fails)?

        fnord

Re: Not able to use native linux commands in a perl script
by toolic (Bishop) on Dec 13, 2010 at 18:13 UTC
      File::Copy

      Or rather File::Copy::Recursive for cp -r functionality.

      Unfortunately, it's not a core module... which might be reason enough for someone to just stick with shelling out to the system cp — unless portability is a concern.

Re: Not able to use native linux commands in a perl script
by Illuminatus (Curate) on Dec 13, 2010 at 18:04 UTC
    It will be very hard to provide advice without the actual code you are using. Perhaps you could provide the relevant portion (remember to use <code> tags)

    fnord

Re: Not able to use native linux commands in a perl script
by ww (Archbishop) on Dec 13, 2010 at 18:40 UTC
    Special attention to the documents re File::Copy, highlighted by toolic!

    There are -- occasionally -- valid reason for using shelling to system commands rather than using Perl functions (modules). What you've described is NOT one of them.

    And, as others noted, we can't be much (reliable) help without knowing what your code looks like and what error message you're receiving.

      I find File::Copy an excellent reason to use system. Things I cannot do with File::Copy:
      1. Preserve the execute bit.
      2. Copy more than one file at a time.
      3. Recursively copy.
      4. Use any of the other arguments of cp.
      5. Use scp with a mostly similar syntax as cp for copying to different machines.
      Luckely the destructiveness doing of File::Copy::copy("file1", "file2", "dir") has been fixed in 5.12.

      I never use File::Copy anymore. I've spend way too much time fighting it. system "cp" never, ever surprises me, and always just works.