使用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了。

相关文章:

  • 2021-08-19
  • 2022-12-23
  • 2021-05-16
  • 2021-08-21
  • 2021-11-18
  • 2022-12-23
  • 2021-12-28
  • 2021-08-20
猜你喜欢
  • 2021-07-21
  • 2022-12-23
  • 2022-12-23
  • 2021-11-22
  • 2021-11-20
  • 2021-11-20
相关资源
相似解决方案