Executing Shell Commands PDF Print E-mail
In any OS Perl can pass commands to the shell for execution.  There are of course security considerations when using this functionality, but for our sys admin scripts, it usually doesn't much matter since we are merely trying to save some time and keystrokes.  There are different ways to approach executing system commands depending on the value you are wanting returned. 

The backtick (under the tilde on the keyboard) is the default encapsulation, but depending on your Perl editor of choice they might be easily confused with single quotes.  Here are some examples for executing shell commands. 


For commands that only need to report whether they executed successfully (0 = Successful, 1+ = Error), use the system function and then test the results:

my $result = system ( "mkdir /tmp/test" );
die "Error $? returned \n" if $?;


If instead you need the output from a shell command use backticks or qx (Quoted eXecution) like this:

my $result = `ls /tmp/test`;
die "Error $? returned \n" if $?;


Also, you can replace the hard-to-read backticks with qx :

my $result = qx[ ls /tmp/test ];
die "Error $? returned \n" if $?;


Note: In most cases your error handling will be much more graceful.

For more advanced system command execution or shell command passing from Perl, look into exec and fork.

 
Joomla SEF URLs by Artio
 
JOOMLA TEMPLATES Joomla Templates By JoomlaBear