免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4383 | 回复: 2
打印 上一主题 下一主题

如何像bash中获得命令执行结果 [复制链接]

coxly 该用户已被删除
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-04-25 09:50 |只看该作者 |倒序浏览
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
0
2 [报告]
发表于 2009-04-25 09:50 |只看该作者


  1. use strict;
  2. use warnings;
  3. my $path="/home/http";
  4. my $key="baidu";
  5. #想得到cat $path|grep $key>>/dev/null;echo $?的执行结果是否成功,应怎样做??
  6. #my $keytest=`cat $path|grep $key>>/dev/null;echo $?`;
  7. system("cat $path|grep $key");
  8. my $keytest = $?>>8;
  9. print "test key=$keytest";
复制代码

或者

  1. use strict;
  2. use warnings;
  3. my $path="/tmp/http";
  4. my $key="baidu";
  5. #想得到cat $path|grep $key>>/dev/null;echo $?的执行结果是否成功,应怎样做??
  6. my $keytest=`cat $path|grep $key >>/dev/null; echo \$?`;
  7. print "keytest=$keytest\n";
复制代码

因为``里面的东西perl是做替换的,所以需要backslash掉

[ 本帖最后由 churchmice 于 2009-4-25 13:11 编辑 ]

论坛徽章:
0
3 [报告]
发表于 2009-04-25 12:51 |只看该作者
你应该使用

  1. system (@command)
  2. if ($? == -1) {
  3.                        print "failed to execute: $!\n";
  4.                    }
  5.                    elsif ($? & 127) {
  6.                        printf "child died with signal %d, %s coredump\n",
  7.                            ($? & 127),  ($? & 128) ? 'with' : 'without';
  8.                    }
  9.                    else {
  10.                        printf "child exited with value %d\n", $? >> 8;
  11.                    }
复制代码

[ 本帖最后由 churchmice 于 2009-4-25 12:58 编辑 ]
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP