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

Hello All
I have simple scripts (.sh) that run shell commands, it is as simple as:
cp thisFile /root/thatFile mkdir /root/thisDirectory scp blah blah....
How can I do this in Perl ??
Thanks

Replies are listed 'Best First'.
Re: Running a shell command via Perl
by McDarren (Abbot) on Apr 24, 2006 at 04:46 UTC
    You can enclose your shell command in backticks (``), or you can use the system command.

    However, for portability it's generally best to try and avoid invoking the shell where-ever possible and either make use of perl inbuilt commands or modules.

    For example, instead of using "cp", you could use File::Copy, you could use the builtin mkdir function, and for "scp" you could make use of Net::SCP

    Cheers,
    Darren :)

Re: Running a shell command via Perl
by mda2 (Hermit) on Apr 24, 2006 at 04:51 UTC
Re: Running a shell command via Perl
by planetscape (Chancellor) on Apr 24, 2006 at 04:56 UTC
Re: Running a shell command via Perl
by meleon (Novice) on Apr 24, 2006 at 05:22 UTC
    you can also run them as pipes so you can parse the output.

    open (FileHandle,"cp thisFile /root/thatfile|"); @Output = <FileHandle>;

    This allows you to see the output of the comand, it is good for error checking with the cp command
      Thank you ALL
      I have it working now.
      I also found something else:
      use Shell; my $sh = Shell->new; $tmp = $sh->exec('readlink /root/Desktop/PerlScripting/mylink '); print "$tmp";
      (here I run the "readlink" shell command)
      What is your opinion on this ?
      BTW, although most command work, I can't get the cd command to work (e.g - `cd /root/Desktop/perlTesting/` )
      Why ?
        Regarding the `cd /root/.... you need to use the Perl built-in chdir

        Cheers,

        JohnGG

        When you need to make a call for system resources try to see (or search in portuguese) if similar command are available on builtin funtions...

        $ perldoc -f readlink readlink EXPR readlink Returns the value of a symbolic link, if symbolic links are implemented. If not, gives a fatal error. If there is some system error, returns the undefined value and sets $! (errno). If EXPR is omitted, uses $_.

        This resource is new for me, but my first try is perldoc local or via web. (the second is CPAN! ;)

        --
        Marco Antonio
        Rio-PM

Re: Running a shell command via Perl
by jesuashok (Curate) on Apr 24, 2006 at 04:35 UTC
    Hi

    `cp thisFile /root/thatFile`
    `mkdir /root/thisDirectory`
    `scp blah blah....`
    But be sure that your SCP command setup should be in such a way that, It should not ask for the password for the Ipaddress you are accessing for.

    "Keep pouring your ideas"
Re: Running a shell command via Perl
by jcc (Sexton) on Apr 24, 2006 at 16:39 UTC
    If you do end up keeping the shell commands, remember to call them via their full path (/usr/bin/cp etc..) so you don't run into trouble if someone puts a malicious/inadvertant script with the same name higher in the PATH of your executing env.
      Thank you both, again, you brought up a good point ! I'll do so.