Skip to content

Instantly share code, notes, and snippets.

@crazyzlj
Created November 24, 2017 09:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save crazyzlj/77236b3716e2cc97fe33c232dfcf7f4e to your computer and use it in GitHub Desktop.
Save crazyzlj/77236b3716e2cc97fe33c232dfcf7f4e to your computer and use it in GitHub Desktop.
useful_shell
# contains(string, substring)
#
# Returns 0 if the specified string contains the specified substring,
# otherwise returns 1.
# https://stackoverflow.com/questions/2829613/how-do-you-tell-if-a-string-contains-another-string-in-unix-shell-scripting
contains() {
string="$1"
substring="$2"
if test "${string#*$substring}" != "$string"
then
return 0 # $substring is in $string
else
return 1 # $substring is not in $string
fi
}
ANY_CHANGES=$(git commit -m "Any changes?")
contains "$ANY_CHANGES" "nothing to commit" && echo "Yes"
contains "$ANY_CHANGES" "nothing to commit" || echo "No"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment