简体   繁体   English

从bash脚本运行的perl脚本的状态

[英]Status of perl scripts run from bash scripts

As far as I know we can use $? 据我所知,我们可以使用$? to get the result of a command we executed and it will return a non-zero result on error and a 0 result on success provided that the programmer kept the convention. 要获得我们执行的命令的结果,并且在程序员遵守约定的前提下,如果出错,它将返回非零结果,如果成功,则将返回0结果。
But what about when we run perl scripts? 但是当我们运行perl脚本时呢?
I am new in perl but I think that perl scripts return 1 if they are part of a module otherwise they don't return anything. 我是perl的新手,但我认为perl脚本如果是模块的一部分则返回1 ,否则它们不会返回任何内容。
So how can I know what happened to a perl script I run? 那么我怎么知道我运行的Perl脚本发生了什么?

Perl scripts behave the same when run from the shell. 从shell运行时,Perl脚本的行为相同。 Having test.pl : test.pl

#!/usr/bin/env perl
exit(0); # or just exit

running: 运行:

./test.pl && echo "OK" || echo $?
OK

Having

#!/usr/bin/env perl
exit(123);

running: 运行:

./test.pl && echo "OK" || echo $?
123

Having

#!/usr/bin/env perl
die;

running: 运行:

./test.pl && echo "OK" || echo $?
Died at ./test.pl line 2.
255

So: 所以:

  • Exiting from perl script with 0 (or nothing) will be true in shell. 在shell中,从perl脚本退出(为0(或不包含任何值))将为true
  • Exiting from perl script with a (byte) value other than 0 will be false in shell and $? 退出perl脚本的(字节)值不是0将在shell和$?false $? will give you the exit value. 会给你退出值。
  • Exiting from perl script with die will be false in shell and $? 在shell和$?从带有die perl脚本退出将是false $? will be 255 (in my case - I checked it). 将是255(在我的情况下 - 我检查了它)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM