1. system
Perl can also use system to call shell commands. It is the same as awk system, and the return value is also the exit status of the command it calls.
Copy the codeThe code is as follows:
[root@AX3sp2 ~]# cat
#! /usr/bin/perl -w
$file = "";
system("ls -l ");
$result = system "ls -l $file";
print "$result \n"; #Exit status of the output command
system "date";
[root@AX3sp2 ~]# perl
-rwxr-xr-x 1 root root 126 12-16 15:12
-rwxr-xr-x 1 root root 126 12-16 15:12
0
Thursday, December 16, 2010 15:58:34 CST
2. Back quotes
The perl system function is the same as that of awk, and cannot return the output of the command.
To get the output of the command, you have to use the same command as the shell itself: ` `
Copy the codeThe code is as follows:
[root@AX3sp2 ~]# cat
#! /usr/bin/perl
print `date`;
print "this is test \n";
[root@AX3sp2 ~]# perl
Thursday, December 16, 2010 15:51:59 CST
this is test
III. Exec
Finally, perl can also use exec to call shell commands. exec is similar to system, the difference is that after exec is called, perl will exit immediately without continuing to execute the remaining code.
Copy the codeThe code is as follows:
[root@AX3sp2 ~]# cat
#! /usr/bin/perl
exec ("echo this is test");
print "good bye !\n"; #This sentence will not be output
[root@AX3sp2 ~]# perl
this is test