bash gitlab ci 脚本在哪个 $? 允许非零

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39466770/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 15:09:58  来源:igfitidea点击:

gitlab ci scripts during which $? is allowed to be non-zero

bashgitlab-ci

提问by pseyfert

In our project we have a shell script which is to be sourced to set up environment variables for the subsequent build process or to run the built applications.

在我们的项目中,我们有一个 shell 脚本,用于为后续构建过程设置环境变量或运行构建的应用程序。

It contains a block which checks the already set variables and does some adjustment.

它包含一个检查已经设置的变量并进行一些调整的块。

# part of setup.sh
for LIBRARY in "${LIBRARIES_WE_NEED[@]}"
do
  echo $LD_LIBRARY_PATH | \grep $LIBRARY > /dev/null
  if [ $? -ne 0 ]
  then
   echo Adding $LIBRARY
   LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$LIBRARY
  else
   echo Not adding $LIBRARY
  fi
done

i.e. it checks if a path to a library is already in $LD_LIBRARY_PATHand if not, adds it. (To be fair, this could be written differently (like here), but assume the script is supposed to achieve something which is very hard to do without calling a program, checking $?and then either doing one thing or doing another thing).

即它检查库的路径是否已经存在$LD_LIBRARY_PATH,如果没有,则添加它。(公平地说,这可以用不同的方式编写(就像这里),但假设脚本应该实现一些事情,如果不调用程序,检查$?然后做一件事或做另一件事,这是很难做到的)。

The .gitlab-ci.ymlthen contains

.gitlab-ci.yml则包含

before_script:
  - yum install -y <various packages>
  - source setup.sh

but the runner decides to stop the before script the very moment $?is non-zero, i.e. when the if-statement decides to add a path to $LD_LIBRARY_PATH. Now it is nice that the gitlab runner checks $?after each line of my script, but here it'd be great if the lines in .gitlab-ci.ymlwere considered atomic.

但是跑步者决定在$?非零时刻停止 before 脚本,即当 if 语句决定向$LD_LIBRARY_PATH. 现在很高兴 gitlab runner$?在我的脚本的每一行之后检查,但如果这些行.gitlab-ci.yml被认为是原子的,那就太好了。

Is there a way to avoid the intermediate checks of $?in a script that's sourced in .gitlab-ci.yml?

有没有办法避免在$?源自 的脚本中进行中间检查.gitlab-ci.yml

回答by andlrc

Use command_that_might_fail || trueto mask the exit status of said command.

使用command_that_might_fail || true来掩盖所述命令的退出状态。

Also note that you can use grep -qto prevent output:

另请注意,您可以使用grep -q来防止输出:

echo "$LD_LIBRARY_PATH" | grep -q "$LIBRARY" || true

This will however also mask $?which you might not want. If you want to check if the command exits correct you might use:

然而,这也将掩盖$?您可能不想要的。如果您想检查命令退出是否正确,您可以使用:

if echo "$LD_LIBRARY_PATH" | grep -q "$LIBRARY"; then
  echo "Adding $LIBRARY"
else
  ...
fi

I suspect that gitlab-cisets -ewhich you can disabled with set +e:

我怀疑gitlab-ci-e,你可以禁用set +e

set +e # Disable exit on error
for library in "${LIBRARIES_WE_NEED[@]}"; do
  ...
done
set -e # Enable exit on error

Future reading: Why double quotes matterand Pitfalls with set -e

未来阅读:为什么双引号很重要陷阱set -e