加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

grep in bash script Jenkins

发布时间:2020-12-15 21:33:12 所属栏目:安全 来源:网络整理
导读:我有一个在bash脚本中工作的grep命令: if grep 'stackoverflow' outFile.txt; then exit 1fi 这在我的主机上运行时工作正常.然而,当我从Jenkins构建步骤调用它时,它每次都退出0,而不是看到’stackoverflow’.出了什么问题? 解决方法 添加以下行作为“执行S
我有一个在bash脚本中工作的grep命令:

if grep 'stackoverflow' outFile.txt; then
 exit 1
fi

这在我的主机上运行时工作正常.然而,当我从Jenkins构建步骤调用它时,它每次都退出0,而不是看到’stackoverflow’.出了什么问题?

解决方法

添加以下行作为“执行Shell”命令中的第一行

#!/bin/sh

grep命令在找不到匹配时以非零代码退出,并导致jenkins将作业标记为失败.见下文.

在“执行Shell”的帮助部分

Runs a shell script (defaults to sh,but this is configurable) for building the project. The script will be run with the workspace as the current directory. Type in the contents of your shell script. If your shell script has no header line like #!/bin/sh —,then the shell configured system-wide will be used,but you can also use the header line to write script in another language (like #!/bin/perl) or control the options that shell uses.

By default,the shell will be invoked with the “-ex” option. So all of the commands are printed before being executed,and the build is considered a failure if any of the commands exits with a non-zero exit code. Again,add the #!/bin/… line to change this behavior.

As a best practice,try not to put a long shell script in here. Instead,consider adding the shell script in SCM and simply call that shell script from Jenkins (via bash -ex myscript.sh or something like that),so that you can track changes in your shell script.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读