Perl调用shell命令 Perl调用shell命令方法小结

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Perl调用shell命令 Perl调用shell命令方法小结

  2021-03-19 我要评论
想了解Perl调用shell命令方法小结的相关内容吗,在本文为您仔细讲解Perl调用shell命令的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Perl,调用shell命令,下面大家一起来学习吧。

一、system
perl也可以用system调用shell的命令,它和awk的system一样,返回值也是它调用的命令的退出状态.

复制代码 代码如下:

[root@AX3sp2 ~]# cat aa.pl
#! /usr/bin/perl -w
$file = "wt.pl";
system("ls -l wt.pl");
$result = system "ls -l $file";
print "$result \n"; #输出命令的退出状态
system "date";

[root@AX3sp2 ~]# perl aa.pl
-rwxr-xr-x 1 root root 126 12-16 15:12 wt.pl
-rwxr-xr-x 1 root root 126 12-16 15:12 wt.pl
0
2010年 12月 16日 星期四 15:58:34 CST     


二、反引号
perl的system函数和awk的一样不能够返回命令的输出.
要得到命令的输出,就得使用和shell本身一样的命令:     ` `
复制代码 代码如下:

[root@AX3sp2 ~]# cat bb.pl
#! /usr/bin/perl
print `date`;
print "this is test \n";

[root@AX3sp2 ~]# perl bb.pl
2010年 12月 16日 星期四 15:51:59 CST
this is test


三、exec
最后,perl还可以使用exec来调用shell的命令. exec和system差不多,不同之处在于,调用exec之后,perl马上就退出,而不会去继续执行剩下的代码
复制代码 代码如下:

[root@AX3sp2 ~]# cat cc.pl
#! /usr/bin/perl
exec ("echo this is test");
print "good bye !\n";  #这句话不会被输出

[root@AX3sp2 ~]# perl cc.pl
this is test

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们