Grep and set -o errexit

坚强是说给别人听的谎言 提交于 2021-02-05 12:32:34

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!