Created
September 21, 2015 12:13
-
-
Save sanzaru/b174a2ee0e4ca5541960 to your computer and use it in GitHub Desktop.
Bash function: Search for a string in a given array of strings
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# 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