【发布时间】:2018-05-14 12:50:39
【问题描述】:
我有下面的脚本,我将通过 jenkins 运行:
#!/bin/bash
function getToken
{
echo "function to get token"
}
function call_init
{
echo "Creating a config file"
}
function call_list
{
echo "calling list*"
}
#Starting execution
if [[ -z "$TOKEN" ]]; then
TOKEN=$(getToken)
if [ $? -ne 0 ]; then
exit 1
fi
fi
echo "Creating a config file and populating it"
call_init
if [ $? -ne 0 ]; then
exit 1
fi
if [ -n $ACTION ]; then
case "$ACTION" in
'list') echo "Action is list"
call_list
if [ $? -ne 0 ]; then
exit 1
fi
;;
'update') echo "Section is update"
;;
'delete') echo "Section is delete"
;;
*) echo "This is a default message"
;;
esac
fi
如您所见,下面的代码有很多重复,这有助于我通过抛出错误代码 1 使 jenkins 工作失败:
if [ $? -ne 0 ]; then
exit 1
fi
处理此问题的最有效方法是什么?我需要它始终以 1 退出代码。
P.S:我通过了Checking Bash exit status of several commands efficiently,但无法使其适用于上述脚本。
【问题讨论】:
-
好吧。这是一项任务吗?不知何故,我的函数正在执行。
-
只需使用构造
call_list || exit 1。 -
请注意,您无法获得您引用的解决方案的原因主要是该问题的公认答案不太理想。 (我正在尝试外交。这是一个糟糕的解决方案。)