Perl 中的错误级别

发布于 2024-10-04 00:18:40 字数 224 浏览 2 评论 0原文

我需要根据上一步的返回代码终止 perl 脚本。
类似于

  IF ERRORLEVEL 1 goto ERROR  

批处理。

$PROG = `spu_comp 2>&1 $a 1 1`;  

需要如果此步骤出现错误,程序应该终止。
预先感谢您的投入。

I need to terminate the perl script according to the return code from the previous step.
something like

  IF ERRORLEVEL 1 goto ERROR  

in batch processing.
i have

$PROG = `spu_comp 2>&1 $a 1 1`;  

i need if this step gives error, program should terminate.
thanks in advance for your inputs.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

夏至、离别 2024-10-11 00:18:40

在分配给 $PROG 的行之后,立即添加以下行:

($? >> 8) and die "spu_comp exited with non-zero return value";

Immediately after the line where you assign to $PROG, add this line:

($? >> 8) and die "spu_comp exited with non-zero return value";
山色无中 2024-10-11 00:18:40
$ perl -le'`sh -c "exit 0"`;($?>>8) and die "exited with non-zero: ", ($?>>8)'
$ perl -le'`sh -c "exit 1"`;($?>>8) and die "exited with non-zero: ", ($?>>8)'
exited with non-zero: 1 at -e line 1.
$ perl -le'`sh -c "exit 0"`;($?>>8) and die "exited with non-zero: ", ($?>>8)'
$ perl -le'`sh -c "exit 1"`;($?>>8) and die "exited with non-zero: ", ($?>>8)'
exited with non-zero: 1 at -e line 1.
神回复 2024-10-11 00:18:40

看来 ERRORLEVEL 并不是 perl 的真正退出代码。

我也有同样的问题。一个bat文件

@Echo OFF
echo setting error level 1
EXIT /B 1

一个perl文件

#!/usr/bin/perl
$command = `C:\foo.bat`;
print "Error Level: " .$? ."\n";
print "Command: " . $command . "\n";

产量输出

Error Level: 0
Command:

一个perl文件

#!/usr/bin/perl

my $command = `dir`;#try both dir and dri to test real exit codes against batch exit codes
print "Error Level: " .$? ."\n";
print "Command: " . $command . "\n";

将产量

C:\>back.pl
'dri' is not recognized as an internal or external command,
operable program or batch file.
Error Level: 256
Command:

C:\>back.pl
Error Level: 0
Command:  Volume in drive C has no label.
 Volume Serial Number is 8068-BE74

 Directory of C:\

12/13/2010  11:02 AM                 7 8
06/02/2010  01:13 PM                 0 AUTOEXEC.BAT
06/04/2010  01:00 PM    <DIR>          AutoSGN
12/13/2010  12:03 PM               111 back.pl
06/02/2010  01:13 PM                 0 CONFIG.SYS
06/03/2010  07:37 PM    <DIR>          Documents and Settings
12/13/2010  12:01 PM                46 foo.bat
06/04/2010  03:17 PM    <DIR>          HorizonTemp
06/02/2010  02:41 PM    <DIR>          Intel
06/04/2010  02:19 PM    <DIR>          league
06/04/2010  12:31 PM    <DIR>          Perl
12/10/2010  03:28 PM    <DIR>          Program Files
12/08/2010  04:13 PM    <DIR>          Quarantine
12/13/2010  08:14 AM    <DIR>          WINDOWS
               5 File(s)            164 bytes
               9 Dir(s)  18,949,783,552 bytes free


C:\>

It appears that ERRORLEVEL is not a true exit code to perl.

i have the same issue. A bat file of

@Echo OFF
echo setting error level 1
EXIT /B 1

With a perl file of

#!/usr/bin/perl
$command = `C:\foo.bat`;
print "Error Level: " .$? ."\n";
print "Command: " . $command . "\n";

Yields output of

Error Level: 0
Command:

A perl file of

#!/usr/bin/perl

my $command = `dir`;#try both dir and dri to test real exit codes against batch exit codes
print "Error Level: " .$? ."\n";
print "Command: " . $command . "\n";

will yield

C:\>back.pl
'dri' is not recognized as an internal or external command,
operable program or batch file.
Error Level: 256
Command:

C:\>back.pl
Error Level: 0
Command:  Volume in drive C has no label.
 Volume Serial Number is 8068-BE74

 Directory of C:\

12/13/2010  11:02 AM                 7 8
06/02/2010  01:13 PM                 0 AUTOEXEC.BAT
06/04/2010  01:00 PM    <DIR>          AutoSGN
12/13/2010  12:03 PM               111 back.pl
06/02/2010  01:13 PM                 0 CONFIG.SYS
06/03/2010  07:37 PM    <DIR>          Documents and Settings
12/13/2010  12:01 PM                46 foo.bat
06/04/2010  03:17 PM    <DIR>          HorizonTemp
06/02/2010  02:41 PM    <DIR>          Intel
06/04/2010  02:19 PM    <DIR>          league
06/04/2010  12:31 PM    <DIR>          Perl
12/10/2010  03:28 PM    <DIR>          Program Files
12/08/2010  04:13 PM    <DIR>          Quarantine
12/13/2010  08:14 AM    <DIR>          WINDOWS
               5 File(s)            164 bytes
               9 Dir(s)  18,949,783,552 bytes free


C:\>
满栀 2024-10-11 00:18:40

您可以通过添加以下行从 $PROG 获取正确的返回代码。

my $ret = $?/256   #/

或者更简洁的方法

my $ret = $? >> 8;

然后将 $ret 与您可以检索的可能值进行比较

if ($ret == 0)
{
   # Do something if finished successfully
}
elsif($ret == 1)
{
   error();
}
else
{
   # Return something else that was nor 0 nor 1
}

You can get the correct return code from $PROG by adding the following line.

my $ret = $?/256   #/

or a cleaner way

my $ret = $? >> 8;

Then compare the $ret with the possible values you can retrieve

if ($ret == 0)
{
   # Do something if finished successfully
}
elsif($ret == 1)
{
   error();
}
else
{
   # Return something else that was nor 0 nor 1
}
腹黑女流氓 2024-10-11 00:18:40

除了 @husker 的回答之外,值得注意的是 $? 仅适用于 255 或更少的代码。 Windows 错误代码通常会超过这个值。然而, IPC::System::Simple 模块提供了类似的方法可以正确检索代码的capture()> 255.

例如

use Test::More;
use IPC::System::Simple qw(capture $EXITVAL EXIT_ANY);
my $modeTest = capture(EXIT_ANY, "some command that sets error code 5020");
is( $EXITVAL , 5020, "Expect error code 5020" );

Further to @husker's answer, it's worth noting $? only works for codes of 255 or less. Windows error codes typically exceed this. The IPC::System::Simple module, however, provides methods like capture() that can correctly retrieve codes > 255.

e.g.

use Test::More;
use IPC::System::Simple qw(capture $EXITVAL EXIT_ANY);
my $modeTest = capture(EXIT_ANY, "some command that sets error code 5020");
is( $EXITVAL , 5020, "Expect error code 5020" );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文