我如何在bash中抛出错误以获取catch子句(我不确定此表达式的实际调用方式)
{
# ...
if [ "$status" -ne "200" ]
# throw error
fi
} || {
# on error / where I want to get if status != 200
}
我知道我可以使用一个函数,但是这种情况使我好奇是否可以这样做
有多种方法可以执行类似的操作:
使用子外壳(如果要设置参数等,可能不是最佳解决方案...)
(
if [[ "$status" -ne "200" ]]
then
exit 1
fi
) || (
# on error / where I want to get if status != 200
echo "error thrown"
)
使用中间错误变量(您可以通过设置不同的数字来捕获多个错误。此外:缩进深度较小)
if [[ "$status" -ne "200" ]]
then
error=1
fi
if [ $error != 0 ]
then
echo "error $error thrown"
fi
立即使用测试的退出值(请注意,我更改-ne
为-eq
)
[[ "$status" -eq "200" ]] || echo "error thrown"
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句