如何从失败的命令返回退出代码0 [英] How to return exit code 0 from a failed command
本文介绍了如何从失败的命令返回退出代码0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想从失败的命令返回退出代码"0".有没有比这更简单的方法了?
I would like to return exit code "0" from a failed command. Is there any easier way of doing this, rather than:
function a() {
ls aaaaa 2>&1;
}
if ! $(a); then
return 0
else
return 5
fi
推荐答案
只需将return 0
附加到函数以强制函数始终成功退出.
Simply append return 0
to the function to force a function to always exit successful.
function a() {
ls aaaaa 2>&1
return 0
}
a
echo $? # prints 0
如果出于任何原因希望内联,可以将|| true
附加到命令:
If you wish to do it inline for any reason you can append || true
to the command:
ls aaaaa 2>&1 || true
echo $? # prints 0
如果要反转退出状态,只需在命令前加上!
If you wish to invert the exit status simple prepend the command with !
! ls aaaaa 2>&1
echo $? # prints 0
! ls /etc/resolv.conf 2>&1
echo $? # prints 1
此外,如果您说出要实现的总体目标,我们也许可以指导您获得更好的答案.
Also if you state what you are trying to achieve overall we might be able to guide you to better answers.
这篇关于如何从失败的命令返回退出代码0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文