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

Hi, I need to write a perl script on UNIX platform in which I have to use a number of UNIX commands like jar,cp etc... How do I embed these commands in the script? Can I use all UNIX commands as it is used from the command line. Help is appreciated. Thank You.
  • Comment on How do I execute UNIX commands from a perl script

Replies are listed 'Best First'.
Re: How do I execute UNIX commands from a perl script
by jwest (Friar) on Nov 02, 2001 at 03:28 UTC
    For what it sounds like you'll want, the most you should need is to call system() with the commands you want to call. See the link for details on how to use it.

    A quick and dirty example would be something like:

    system("cp /foo /bar");

    or

    system("cp", "/foo", "/bar");

    each of which do slightly different things. See system() or try a Super Search for more discussion.

    Hope this helps!

    --jwest

    -><- -><- -><- -><- -><-
    All things are Perfect
        To every last Flaw
        And bound in accord
             With Eris's Law
     - HBT; The Book of Advice, 1:7
    
      Just use backticks. to remove a file called file.doc all you'd have to do is put in your program `rm file.doc`; you might want to put in directories or whatever, but, I have found that to be the easiest way to execute unix command options from perl script
Re: How do I execute UNIX commands from a perl script
by VSarkiss (Monsignor) on Nov 02, 2001 at 05:12 UTC

    Nobody mentioned the Shell module that comes standard with Perl. It does some hackery with AUTOLOAD to see if any undefined routines are shell commands. Here are some examples of what it can do quoted from the documentation

    #!/usr/bin/perl use Shell; $foo = echo("howdy", "<funny>", "world"); print $foo; $passwd = cat("</etc/passwd"); print $passwd; sub ps; print ps -ww; cp("/etc/passwd", "/tmp/passwd");

    HTH

Re: How do I execute UNIX commands from a perl script
by msemich (Novice) on Nov 02, 2001 at 03:54 UTC
    Also make sure that none of the commands you are sending to the system make use of tainted user data; e.g.: data that you have recieved from the user and have not checked for obvious problems (i.e. rm -r -f). This could be a big door to script kiddies of the world. You can make the interpreter get *really* paranoid about input by adding the -t option to your shebang line (just like turning -w warnings on). This will make you *have* to pass all user supplied input thru a regex to keep perl from horking it.
Re: How do I execute UNIX commands from a perl script
by hopes (Friar) on Nov 02, 2001 at 13:35 UTC
    You can use:
    system: You can catch exit code of the command
    exec: Read de docs, your script will not continue normally
    qx You can catch the STDOUT response
    `` (the same of qx)

    Try a SuperSearch on this, the question has been asked many times; there are responses that could help you.

    Hope this helps
    Hopes
Re: How do I execute UNIX commands from a perl script
by skinnymofo (Scribe) on Nov 02, 2001 at 07:40 UTC
    With the system() command, a good tip is to check the return code of the call to make sure everything worked the way it should have. Kinda like this:
    $command = "mv /foo/bar ./new/bar"; (system($command) == 0) || warn "$command didn't work!";
Re: How do I execute UNIX commands from a perl script
by Rex(Wrecks) (Curate) on Nov 02, 2001 at 04:36 UTC
    There is system() and exec() which do quite diffrent things, see the help on both for more info.

    And there is also backticks, which is what I like to use as I get the output of the command returned to me.

    "Nothing is sure but death and taxes" I say combine the two and its death to all taxes!