bash 脚本中的 grep + Jenkins
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16904237/
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
grep in bash script + Jenkins
提问by gjw80
I have a grep command that works in a bash script:
我有一个在 bash 脚本中工作的 grep 命令:
if grep 'stackoverflow' outFile.txt; then
exit 1
fi
This works fine when run on my host. When I call this from a Jenkins build step however, it exits 0 everytime, not seeing 'stackoverflow'. What is going wrong?
这在我的主机上运行时工作正常。然而,当我从 Jenkins 构建步骤调用它时,它每次都退出 0,没有看到“stackoverflow”。出了什么问题?
回答by sandeepkunkunuru
Add the following line as the first line in your "Execute Shell" command
将以下行添加为“执行 Shell”命令的第一行
#!/bin/sh
grep command exits with a non zero code when it does not find match and that causes jenkins to mark the job as failed. See Below.
grep 命令在找不到匹配项时以非零代码退出,这会导致 jenkins 将作业标记为失败。见下文。
In the help section of "Execute 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.
运行 shell 脚本(默认为 sh,但这是可配置的)以构建项目。该脚本将以工作区作为当前目录运行。输入您的 shell 脚本的内容。如果您的 shell 脚本没有像 #!/bin/sh — 这样的标题行,那么将使用系统范围内配置的 shell,但您也可以使用标题行以另一种语言编写脚本(例如 #!/bin/perl ) 或控制 shell 使用的选项。
默认情况下,shell 将使用“-ex”选项调用。因此,所有命令在执行之前都会打印出来,如果任何命令以非零退出代码退出,则构建将被视为失败。再次添加 #!/bin/... 行以更改此行为。
作为最佳实践,尽量不要在此处放置很长的 shell 脚本。相反,请考虑在 SCM 中添加 shell 脚本,并简单地从 Jenkins 调用该 shell 脚本(通过 bash -ex myscript.sh 或类似方法),以便您可以跟踪 shell 脚本中的更改。
回答by gaoithe
I am a bit confused by the answers on this question! i.e. Sorry, but the answers here are incorrect for this question. The question is good/interesting as plain grep in scripts does cause scripts to exit with failure if the grep is not successful (which can be unexpected), whereas a grep inside an if will not cause exit with failure.
我对这个问题的答案有点困惑!即对不起,但这里的答案对于这个问题是不正确的。这个问题很好/很有趣,因为如果 grep 不成功(这可能是意外的),脚本中的普通 grep 确实会导致脚本失败退出,而 if 中的 grep 不会导致退出失败。
For the example shown in the question exit 1 will be done IF the grep command runs successfully(file exists) AND if the string is found in file.(grep command returns 0 exit code to if).
对于问题出口 1 中显示的示例,如果 grep 命令运行成功(文件存在)并且在文件中找到字符串,则将完成退出 1。(grep 命令将 0 退出代码返回给 if)。
@Gonen's comment to add 'ls -l outFile.txt' should have been followed up on to see what the real reason for failure was.
应该跟进@Gonen 添加“ls -l outFile.txt”的评论,看看失败的真正原因是什么。
TLDR; if catches the exit code of commands inside the if clause:
TLDR;if 捕获 if 子句中命令的退出代码:
A grep command that 'fails'(no match or error) inside an if statement in jenkins will not cause jenkins script to stop. Whereas a grep command that fails not inside an if will cause jenkins to stop and exit with fail.
在 jenkins 的 if 语句中“失败”(没有匹配或错误)的 grep 命令不会导致 jenkins 脚本停止。而不在 if 内失败的 grep 命令将导致 jenkins 停止并退出失败。
The exit/return code handling is different for commands inside an if statement in shell. if catches the return code and no matter if command was successful or failed the if will return success to $0(after if) (and do actions in if or else).
对于 shell 中 if 语句中的命令,退出/返回代码处理是不同的。if 捕获返回码,无论命令是成功还是失败,if 都会将成功返回到 $0(if 之后)(并在 if 或 else 中执行操作)。
From man bash:
从人bash:
if list; then list; [ elif list; then list; ] ... [ else list; ] fi The if list is executed. If its exit status is zero, the then list is executed. Otherwise, each elif list is executed in turn, and if its exit status is zero, the corresponding then list is executed and the command completes. Otherwise, the else list is executed, if present. The exit status is the exit status of the last command executed, or zero if no condition tested true.
如果列出;然后列出;[ elif 列表; 然后列出;] ... [ 其他列表;] fi if 列表被执行。如果其退出状态为零,则执行 then 列表。否则,依次执行每个 elif 列表,如果其退出状态为零,则执行相应的 then 列表并完成命令。否则,执行 else 列表(如果存在)。退出状态是最后执行的命令的退出状态,如果没有条件测试为 true 则为零。
To illustrate, try this (same result in bash or sh):
为了说明这一点,试试这个(bash 或 sh 的结果相同):
$ if grep foo bar ; then echo got it; fi; echo $?
grep: bar: No such file or directory
0
$ touch bar
$ if grep foo bar ; then echo got it; fi; echo $?
0
$ echo foo >bar
$ if grep foo bar ; then echo got it; fi; echo $?
foo
got it
0
$ if grep foo bar ; then echo gotit; grep gah mah; fi; echo $?
foo
gotit
grep: mah: No such file or directory
2
回答by akozin
I think you have error in your script. You must add 'fi' at the end of 'if' block:
我认为你的脚本有错误。您必须在“if”块的末尾添加“fi”:
if grep 'stackoverflow' outFile.txt; then
exit 1
fi
回答by cforbish
If the two were exactly the same it should work. Is your current directory or user different in the two environments? You might not be able to read the file.
如果两者完全相同,它应该可以工作。您当前的目录或用户在两种环境中是否不同?您可能无法读取该文件。

