Checking if output of a command contains a certain string in a shell script

后端 未结 4 756
谎友^
谎友^ 2020-11-29 18:14

I\'m writing a shell script, and I\'m trying to check if the output of a command contains a certain string. I\'m thinking I probably have to use grep, but I\'m not sure how.

4条回答
  •  悲哀的现实
    2020-11-29 18:32

    Another option is to check for regular expression match on the command output.

    For example:

    [[ "$(./somecommand)" =~ "sub string" ]] && echo "Output includes 'sub string'"
    

提交回复
热议问题