grep: add option for better usability in pipelines

Michael Conrad mconrad at intellitree.com
Thu Feb 17 04:50:09 UTC 2022


On 2/16/22 19:32, Kang-Che Sung wrote:
>
>> Now, for an example where it makes a difference. Consider a Bash script
>> like this:
>>
>>    # enable automatic error handling
>>    set -eo pipefail
>>    # check for string "issues" in a logfile
>>    cat logfile | grep issue | sort --unique
>>
>> If there are no issues in the logs, grep return exit code 1 and the
>> shell interprets this as an error and exits itself.
>>
> Why do we need to implement a workaround in grep while you can
> do this in shell to ignore the exit code of grep?
>
> { grep issue <logfile || :; } | sort --unique

In order to implement his suggestion, you need to only ignore exit code 
1, while still allowing other exit codes to abort the script. (like a 
system error while reading the file)

But also, I've never seen ":" used as a command... where is that 
documented?  Is it equivalent to 'true'?

-Mike C


More information about the busybox mailing list