问题
I have a simple bash-script. It finds a string in the file.
#/bin/bash
set -o errexit
grep 'findedstring' $file.
echo "was founded string on file"
<...>
If the string was found in the file the script was successfully executed and I can see "was founded string on file" in the output . But if the file doesn't have 'findedstring' then the script exits and didn't work further, so I don't see "was founded string on file" in the output If I will try to delete the following string in my script
'set -o errexit'
then the script will work further and I can see string "was founded string on file" in the output.
How can I save the string in script
'set -o errexit'
and my script will keep working when the string is not founded in the file?
Help me, please.
回答1:
grep 'findedstring' $file. || true
回答2:
#/bin/bash
set -o errexit
[[ $(grep 'findedstring' "$file") ]] && echo "found string on file"
echo "I will be reached even if grep fails"
来源:https://stackoverflow.com/questions/29935388/grep-and-set-o-errexit