【发布时间】:2013-01-17 11:51:25
【问题描述】:
我想在出错时完全终止/退出 bash shell 脚本,但使用函数error 让我在终止前显示调试输出。现在我遇到的问题是,如果函数的输出是通过反引号或$() 捕获的,则错误函数中的exit 1 语句将不终止shell 脚本。
这是我的示例脚本:
#!/bin/bash
function error ()
{
echo "An error has occured: $1"
exit 1
}
function do_sth ()
{
if [ $1 -eq 0 ]; then
error "First param must be greater than 0!"
else
echo "OK!"
fi
}
RESULT=`do_sth 0`
echo "This line should never be printed"
如何立即终止 error() 函数中的脚本?
【问题讨论】: