【问题标题】:Exit script function in bashbash中的退出脚本函数
【发布时间】: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
  • 请注意,您无法获得您引用的解决方案的原因主要是该问题的公认答案不太理想。 (我正在尝试外交。这是一个糟糕的解决方案。)

标签: bash shell unix


【解决方案1】:

最好的方法是使用显式错误检查。

你当前的模式可以精简,以下都是等价的:

run_command
if [ $? -ne 0 ]; then
    print_error
    exit 1
fi
if ! run_command; then
    print_error
    exit 1
fi
run_command || { print_error; exit 1; }

或者以最简单的形式,没有错误信息:

run_command || exit 1

作为替代方案,您可能想要使用set -e。 您可能还对set -o pipefail 感兴趣。

这些不是首选解决方案,正如@William 所指出的,但对于让简单的脚本抛出错误很有用:

请注意,set -e 通常不被视为最佳做法。它的语义在边缘情况下是非常出乎意料的(例如,如果您在函数中调用set -e),更重要的是随着shell 的不同版本发生了巨大变化。最好通过运行 cmd || exit 来显式调用 exit

如果命令返回非零值,您可以使用set -e 使bash 退出,并使用set +e 禁用此行为。

set: set [-abefhkmnptuvxBCHP] [-o option-name] [--] [arg ...]
    Set or unset values of shell options and positional parameters.

    Change the value of shell attributes and positional parameters, or
    display the names and values of shell variables.

    Options:
      [...]
      -e  Exit immediately if a command exits with a non-zero status.
      [...]
      -o option-name
          Set the variable corresponding to option-name:
              [...]
              pipefail     the return value of a pipeline is the status of
                           the last command to exit with a non-zero status,
                           or zero if no command exited with a non-zero status
              [...]

要使用此功能,您必须先启用该选项。

例如:

# disable 'exit immediately' (the default)
set +e

echo "running false..."
false
echo "we're still running"

# enable 'exit immediately'
set -e

echo "running false..."
false
echo "this should never get printed"

set -o pipefail必须与set -e结合使用:

# enable 'exit immediately'
set -e

# disable 'pipefail' (the default)
set +o pipefail

echo "running false | true..."
false | true
echo "we're still running (only the last exit status is considered)"

# enable 'pipefail'
set -o pipefail

echo "running false | true..."
false | true
echo "this should never get printed"

【讨论】:

  • 谢谢...如何在我的脚本中使用它?我试过 set -e,但什么也没发生
  • 我已经添加了关于它的使用的注释。
  • 请注意,set -e 通常不被视为最佳实践。它的语义在边缘情况下是非常出乎意料的(例如,如果您在函数中调用set -e),更重要的是随着shell 的不同版本发生了巨大变化。通过运行cmd || exit 显式调用退出要好得多
  • 哈,我只是添加了一个关于那个的注释......也许我会让set -e 一个注释到显式错误检查......
  • @WilliamPursell 更新
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-03
  • 1970-01-01
  • 2023-01-28
  • 1970-01-01
  • 2012-09-22
  • 1970-01-01
相关资源
最近更新 更多