在编写 perl 脚本时经常会调用外部命令,由于种种原因,会遇到外部命令执行异常,而导致整个脚本终止运行。这对于大的项目来说很不经济,因此,可以想办法越过这个异常并继续执行后续脚本。

Examples:


假设有一个外部命令“command”需要执行,将该命令存到一个数组当中:
```bash my @arr = ("command"); ```
将外部命令的返回值赋给一个变量:
```bash my $return = sysstem(@arr); ```
判断返回值是否为“0”,0代表运行正常,任何非0的值均表示有运行异常。如果返回值不为0,则记录错误,然后继续运行后续代码:
```bash if (!($return == 0)){ print "There are some errors when running the command!\n"; command2;#继续运行其他命令 } ```

** 注:** 本方案仅在 Linux 系统下测试,不晓得 Windows 和 Mac 的返回值与 Linux 是否一致,Linux 中的 “core dump” 就可以如此来捕获。

Edited on Views times

Give me a cup of [coffee]~( ̄▽ ̄)~*

Hualin Liu WeChat Pay

WeChat Pay

Hualin Liu Alipay

Alipay