我正在写一个shell脚本,需要检查终端应用程序是否已安装。我想使用TRY/CATCH命令来做到这一点,除非有更整洁的方法。
当前回答
你可以使用trap:
try {block A} catch {block B} finally {block C}
翻译:
(
set -Ee
function _catch {
block B
exit 0 # optional; use if you don't want to propagate (rethrow) error to outer shell
}
function _finally {
block C
}
trap _catch ERR
trap _finally EXIT
block A
)
其他回答
Bash不会在检测到错误状态时中止正在运行的执行(除非您设置了-e标志)。提供try/catch的编程语言这样做是为了抑制由于这种特殊情况而产生的“保释”(因此通常称为“异常”)。
相反,在bash中,只有有问题的命令将退出,退出码大于0,表示错误状态。当然,您可以检查这一点,但由于没有任何自动保释,因此try/catch没有意义。它只是缺乏背景。
然而,你可以通过使用子壳来模拟一个保释,它可以在你决定的点终止:
(
echo "Do one thing"
echo "Do another thing"
if some_condition
then
exit 3 # <-- this is our simulated bailing out
fi
echo "Do yet another thing"
echo "And do a last thing"
) # <-- here we arrive after the simulated bailing out, and $? will be 3 (exit code)
if [ $? = 3 ]
then
echo "Bail out detected"
fi
而不是some_condition的if你也可以尝试一个命令,如果它失败(退出码大于0),保释:
(
echo "Do one thing"
echo "Do another thing"
some_command || exit 3
echo "Do yet another thing"
echo "And do a last thing"
)
...
不幸的是,使用这种技术只能使用255个不同的退出码(1..255),而且不能使用像样的异常对象。
如果你需要更多的信息来传递模拟异常,你可以使用subshell的stdout,但这有点复杂,可能是另一个问题;-)
使用上面提到的-e标志,你甚至可以剥离显式的退出语句:
(
set -e
echo "Do one thing"
echo "Do another thing"
some_command
echo "Do yet another thing"
echo "And do a last thing"
)
...
Bash中有TRY CATCH命令吗?
不。
Bash不像在许多编程语言中那样拥有那么多奢侈的东西。
bash中没有try/catch;但是,可以使用&&或||实现类似的行为。
使用| |:
如果command1失败,则command2按如下方式运行
command1 || command2
类似地,使用&&,如果command1成功,command2将运行
try/catch最接近的近似如下
{ # try
command1 &&
#save your output
} || { # catch
# save log for exception
}
此外,bash还包含一些错误处理机制
set -e
如果任何简单的命令失败,它将停止脚本。
还有为什么不是if…else。它是你最好的朋友。
我建议在“bash -ue”模式下这样做:
set -ue
false && RET=$? || RET=$?
echo "expecting 1, got ${RET}"
true && RET=$? || RET=$?
echo "expecting 0, got ${RET}"
echo "test try...catch"
false && RET=$? || RET=$?
if [ ${RET} -ne 0 ]; then
echo "caught error ${RET}"
fi
echo "beware, using '||' before '&&' fails"
echo " -> memory aid: [A]nd before [O]r in the alphabet"
false || RET=$? && RET=$?
echo "expecting 1, got ${RET}"
true || RET=$? && RET=$?
echo "expecting 0, got ${RET}"
我用了一个非常简单的方法:
try() {
"$@" || (e=$?; echo "$@" > /dev/stderr; exit $e)
}
我已经在bash中开发了一个几乎完美无缺的try & catch实现,它允许您编写如下代码:
try
echo 'Hello'
false
echo 'This will not be displayed'
catch
echo "Error in $__EXCEPTION_SOURCE__ at line: $__EXCEPTION_LINE__!"
您甚至可以将try-catch块嵌套在它们自己内部!
try {
echo 'Hello'
try {
echo 'Nested Hello'
false
echo 'This will not execute'
} catch {
echo "Nested Caught (@ $__EXCEPTION_LINE__)"
}
false
echo 'This will not execute too'
} catch {
echo "Error in $__EXCEPTION_SOURCE__ at line: $__EXCEPTION_LINE__!"
}
代码是bash样板文件/框架的一部分。它进一步扩展了try & catch的思想,例如使用回溯和异常进行错误处理(以及其他一些不错的特性)。
下面是负责try & catch的代码:
set -o pipefail
shopt -s expand_aliases
declare -ig __oo__insideTryCatch=0
# if try-catch is nested, then set +e before so the parent handler doesn't catch us
alias try="[[ \$__oo__insideTryCatch -gt 0 ]] && set +e;
__oo__insideTryCatch+=1; ( set -e;
trap \"Exception.Capture \${LINENO}; \" ERR;"
alias catch=" ); Exception.Extract \$? || "
Exception.Capture() {
local script="${BASH_SOURCE[1]#./}"
if [[ ! -f /tmp/stored_exception_source ]]; then
echo "$script" > /tmp/stored_exception_source
fi
if [[ ! -f /tmp/stored_exception_line ]]; then
echo "$1" > /tmp/stored_exception_line
fi
return 0
}
Exception.Extract() {
if [[ $__oo__insideTryCatch -gt 1 ]]
then
set -e
fi
__oo__insideTryCatch+=-1
__EXCEPTION_CATCH__=( $(Exception.GetLastException) )
local retVal=$1
if [[ $retVal -gt 0 ]]
then
# BACKWARDS COMPATIBILE WAY:
# export __EXCEPTION_SOURCE__="${__EXCEPTION_CATCH__[(${#__EXCEPTION_CATCH__[@]}-1)]}"
# export __EXCEPTION_LINE__="${__EXCEPTION_CATCH__[(${#__EXCEPTION_CATCH__[@]}-2)]}"
export __EXCEPTION_SOURCE__="${__EXCEPTION_CATCH__[-1]}"
export __EXCEPTION_LINE__="${__EXCEPTION_CATCH__[-2]}"
export __EXCEPTION__="${__EXCEPTION_CATCH__[@]:0:(${#__EXCEPTION_CATCH__[@]} - 2)}"
return 1 # so that we may continue with a "catch"
fi
}
Exception.GetLastException() {
if [[ -f /tmp/stored_exception ]] && [[ -f /tmp/stored_exception_line ]] && [[ -f /tmp/stored_exception_source ]]
then
cat /tmp/stored_exception
cat /tmp/stored_exception_line
cat /tmp/stored_exception_source
else
echo -e " \n${BASH_LINENO[1]}\n${BASH_SOURCE[2]#./}"
fi
rm -f /tmp/stored_exception /tmp/stored_exception_line /tmp/stored_exception_source
return 0
}
你可以自由使用,分叉和贡献——它在GitHub上。