2

How is it possible to get the exit code of the grep command launched in a find:

eg:

find ./export-`date +%Y%m%d`-*/instance.log -type f -exec grep -i -m1 error {} \;

Whether the word "error" is found in the file or not it return the exit code 0

The content of instance.log is as follows:

2016-09-30 09:01:20 : ERROR2 (16)

1 Answer 1

2

The command find used with -exec option will return the status code of the grep command. Example:

find . -type f  -exec grep 201 {} +

Will return 0 when I execute it in a folder with some log files containing the pattern "201". You can check it with echo $? in your console (that gives 0 if the pattern is found).

If I execute the same command in another folder with files that don't contain "201", the status code of the find command will be 1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .