为什么即使我的 Perl 就地脚本失败了,它也会以零退出代码退出? [英] Why is my Perl in-place script exiting with a zero exit code even though it's failing?
本文介绍了为什么即使我的 Perl 就地脚本失败了,它也会以零退出代码退出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个单行 Perl 搜索和替换,大致如下所示:
I have a one-liner Perl search and replace that looks roughly like this:
perl -p -i -e 's/foo/bar/' non-existent-file.txt
因为文件不存在(这不是故意的,但这是自动构建脚本的一部分,所以我想防止出现这种情况),Perl 退出并出现此错误:
Because the file doesn't exist (which isn't intentional, but this is part of an automated build script, so I want to protect against that), Perl exits with this error:
Can't open non-existent-file.txt: No such file or directory.
然而,退出代码仍然为零:
However, the exit code is still zero:
echo $?
0
我做错了吗?我应该修改我的脚本,还是我调用 Perl 的方式?我天真地假设因为 Perl 找不到该文件,它会以非零代码退出.
Am I doing something wrong? Should I be modifying my script, or the way I'm invoking Perl? I was naively assuming that because Perl couldn't find the file, it would exit with a non-zero code.
推荐答案
你可以通过死亡来强制错误,
You can force error by dying,
perl -p -i -e 'BEGIN{ -f $ARGV[0] or die"no file" } s/foo/bar/' non-existent-file.txt
这篇关于为什么即使我的 Perl 就地脚本失败了,它也会以零退出代码退出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文