Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Answer: How to run a shell script from a Perl program?

by radiantmatrix (Parson)
on Nov 03, 2004 at 22:01 UTC ( #405014=categorized answer: print w/replies, xml ) Need Help??

Q&A > programs and processes > How to run a shell script from a Perl program? - Answer contributed by radiantmatrix

Perl offers three slightly different mechanisms for executing external commands:
  1. system
  2. exec
  3. Backticks or qx//
  1. Using system

    system($command, @arguments); # For example: system( "sh", "script.sh", "--help" ); system("sh script.sh --help");
    System will execute the $command with @arguments and return to your script when finished. You may check $! for certain errors passed to the OS by the external application.

    Read the documentation for system for the nuances of how various invocations are slightly different.

  2. Using exec

    This is very similar to the use of system, but it will terminate your script upon execution. Again, read the documentation for exec for more.

  3. Using backticks or qx//

    my $output = `script.sh --option`; my $output = qx/script.sh --option/;
    The backtick operator and it's equivalent qx// excute the command and options inside the operator and return that commands output to STDOUT when it finishes.

There are also ways to run external applications through creative use of open, but this is advanced use; read the documentation for more.

Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
[Eily]: might have noticed before and forgotten
LanX has ideas for more similar pics
[LanX]: well I gimp ed a while to make it look natural
[LanX]: ... obviously too long
[Eily]: I yes, the natural onion in the flamming eye, I know what you mean
[LanX]: Eily your turn to provide a pic :)
[Eily]: occupe toi de tes oignons
LanX could do a zoom out in animated gif ...
[LanX]: heh
[marto]: get with the times, webm :P

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (10)
As of 2017-12-14 16:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What programming language do you hate the most?




















    Results (398 votes). Check out past polls.

    Notices?