网站首页  汉语字词  英语词汇  考试资料  写作素材  旧版资料

请输入您要查询的考试资料:

 

标题 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 © 2002-2024 cuapp.net All Rights Reserved
更新时间:2025/5/24 17:07:38