Skip to content

Instantly share code, notes, and snippets.

@sanzaru
Created September 21, 2015 12:13
Show Gist options
  • Save sanzaru/b174a2ee0e4ca5541960 to your computer and use it in GitHub Desktop.
Save sanzaru/b174a2ee0e4ca5541960 to your computer and use it in GitHub Desktop.
Bash function: Search for a string in a given array of strings
#
# Search for a string in a given array of strings
#
# Argumnents:
# $1: String | required | Defines the query string to search for
# $2: String | required | Array of strings to look for the query
#
# Return:
# 0: Success, query string was found
# 1: Error, query string was not found or any other error occured
#
function strSearch() {
local search=$1
local data=$2
if [ "$search" != "" ] && [ "$data" != "" ]
then
# Iterate through array items
for item in $data
do
# Return if the string was found
if [ "$item" == "$search" ]; then return 0; fi
done
fi
# Default return / String was not found
return 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment