Bootstrap FreeKB - Perl (Scripting) - Return Code using system
Perl (Scripting) - Return Code using system

Updated:   |  Perl (Scripting) articles

The built in system function can be used to get the return code of a command.

#!/usr/bin/perl
use strict;
use warnings;
system("/usr/bin/hostname");   

 

In this example, the output of the /usr/bin/hostname command (on a Linux system) will be printed to the console

~]$ perl example.pl
server1.example.com

 

Often, system is used to get the return code of the command. In this example, the return code of the /usr/bin/hostname command is stored in a variable named $return_code. Printing the $return_code variable shows that the return code is 0 (success).

#!/usr/bin/perl
use strict;
use warnings;
my $return_code = system("/usr/bin/hostname");
print "$return_code \n";       

 

Let's say you have some bogus command.

#!/usr/bin/perl
use strict;
use warnings;
my $return_code = system("/usr/bin/bogus");
print "$return_code \n";       

 

The return code is -1 (failed).

Can't exec "/usr/bin/bogus": No such file or directory at testing.pl line 25.
-1

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 157e47 in the box below so that we can be sure you are a human.