Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

grep in bash script + Jenkins

Tags:

grep

bash

jenkins

I have a grep command that works in a bash script:

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?

like image 598
gjw80 Avatar asked Jun 03 '13 19:06

gjw80


People also ask

Can I use Grep in bash script?

the grep Command in Bash For searching a specific keyword, phrase, or pattern in a file, there is a specialized command in a Bash script, grep . We can use this command to display the lines before and after the keyword matched in a specific file. This command uses flags like -A , -B , and -C .

Can Jenkins run shell script?

These are the steps to execute a shell script in Jenkins: In the main page of Jenkins select New Item. Enter an item name like "my shell script job" and chose Freestyle project. Press OK.


2 Answers

Add the following line as the first line in your "Execute Shell" command

#!/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.

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.

like image 152
sandeepkunkunuru Avatar answered Nov 10 '22 03:11

sandeepkunkunuru


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.

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).

@Gonen's comment to add 'ls -l outFile.txt' should have been followed up on to see what the real reason for failure was.

TLDR; if catches the exit code of commands inside the if clause:

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.

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).

From man 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.

To illustrate, try this (same result in bash or 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
like image 42
gaoithe Avatar answered Nov 10 '22 03:11

gaoithe