在perl中,同时获取shell脚本返回值和标准输出。

http://www.cnblogs.com/agostop/archive/2012/04/03/2431548.html

使用perl调用外名命令有很多方法,这里就不详细讲解。

如有shell脚本a.sh

#!/bin/bash

echo "script out put"

exit 2

有perl脚本test.pl调用a.sh

#!/bin/perl -w

$output = qx(/usr/local/a.sh);

$exitcode = $?;

print "output = " . $output;

print "exitcode = " . $exitcode;

执行此perl脚本./test.pl

你会发现输出的内容为

output = script out put

exitcode = 512

明显就不是你所期望的结果。输出应该是

 

 output = script out put

exitcode = 2

 

这才是我们期待的。

原因是,perl内部$?的返回值,会将shell的值乘以8,我们给他除以8,便可以解决。

 

#!/bin/perl -w

$output = qx(/usr/local/a.sh);

$exitcode = $? >>8;

print "output = " . $output;

print "exitcode = " . $exitcode;

再次运行脚本你会发现OK了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值