免费注册 查看新帖 |

Chinaunix

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

perl中如何得到shell脚本的返回值 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-08-28 15:55 |只看该作者 |倒序浏览
我在perl中调用了shell脚本,想获取shell脚本的返回值,如下:

a.sh:
#!/bin/sh

list=(aa bb cc dd)
if grep "aa"  ${list[@]}
then
     exit 1
fi

b.pl:
#!/usr/bin/perl -w
use strict;

`bash a.sh`;
my $temp=`echo $?`;
print $temp;

运行上面的结果,当a.sh返回0时,b.pl中$temp的值是0;
但是当a.sh返回1时,b.pl中$temp的值是256,为什么?

[ 本帖最后由 jiangxue1327 于 2008-8-28 18:01 编辑 ]

论坛徽章:
0
2 [报告]
发表于 2008-08-28 16:38 |只看该作者
256 = 1 00000000

论坛徽章:
0
3 [报告]
发表于 2008-08-28 16:43 |只看该作者
       $CHILD_ERROR
       $?      The status returned by the last pipe close, backtick (``) com-
               mand, successful call to wait() or waitpid(), or from the sys-
               tem() operator.  This is just the 16-bit status word returned
               by the wait() system call (or else is made up to look like it).
               Thus, the exit value of the subprocess is really ("$? >> 8"),
               and "$? & 127" gives which signal, if any, the process died
               from, and "$? & 128" reports whether there was a core dump.
               (Mnemonic: similar to sh and ksh.)

               Additionally, if the "h_errno" variable is supported in C, its
               value is returned via $? if any "gethost*()" function fails.

               If you have installed a signal handler for "SIGCHLD", the value
               of $? will usually be wrong outside that handler

一共是有16bit
高八位是子进程退出值
直接$? >>8 就可以了

[ 本帖最后由 churchmice 于 2008-8-28 16:46 编辑 ]

论坛徽章:
0
4 [报告]
发表于 2008-08-28 16:52 |只看该作者

  1. my $temp=`echo $?`;
复制代码

``中的变量是会被替换掉的
所以
`bash a.bash`
执行以后
$? (perl 内置变量) 就是 00000001 00000000
所以替换之后就相当于
my $temp = `echo 256`
结果当然是256了
你想干的事情应该是

  1. my $temp = `echo \$?`;
复制代码

但是这样还是有问题的
因为你执行了两次外部命令开了两个shell进程,这两个shell进程之间是没有关系的

[ 本帖最后由 churchmice 于 2008-8-28 16:53 编辑 ]

论坛徽章:
0
5 [报告]
发表于 2008-08-28 18:12 |只看该作者

回复 #4 churchmice 的帖子

多谢,大致明白了,那我的脚本这样写没错吧:

a.sh:
#!/bin/sh

list=(aa bb cc dd)
if grep "aa"  ${list[@]}
then
     exit 1
fi

b.pl:
#!/usr/bin/perl -w
use strict;

`bash a.sh`;
my $temp=`echo $?`;
if($temp!=0) {die "test";}

我就是判断一下a.sh的返回值如果非0时,就结束掉perl程序

论坛徽章:
23
15-16赛季CBA联赛之吉林
日期:2017-12-21 16:39:27白羊座
日期:2014-10-27 11:14:37申猴
日期:2014-10-23 08:36:23金牛座
日期:2014-09-30 08:26:49午马
日期:2014-09-29 09:40:16射手座
日期:2014-11-25 08:56:112015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:49:0315-16赛季CBA联赛之山东
日期:2017-12-21 16:39:1915-16赛季CBA联赛之广东
日期:2016-01-19 13:33:372015亚冠之山东鲁能
日期:2015-10-13 09:39:062015亚冠之西悉尼流浪者
日期:2015-09-21 08:27:57
6 [报告]
发表于 2008-08-28 18:35 |只看该作者
原帖由 jiangxue1327 于 2008-8-28 18:12 发表
多谢,大致明白了,那我的脚本这样写没错吧:

#!/usr/bin/perl -w
use strict;

`bash a.sh`;
my $temp=`echo $?`;
if($temp!=0) {die "test";}


my $temp=`echo $?`; #这句何用?

`bash a.sh`;
if($?!=0) {die "test";}

论坛徽章:
0
7 [报告]
发表于 2008-08-28 18:37 |只看该作者
``

论坛徽章:
0
8 [报告]
发表于 2011-12-25 13:02 |只看该作者
查看如下地址我想应该是你所想要的结果。
http://blog.csdn.net/code805772847/article/details/7100600

aaa.jpg (34.4 KB, 下载次数: 12)

aaa.jpg
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP