Created
February 15, 2013 01:12
-
-
Save anderseknert/4957901 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env bash | |
# As passing an array by value is not possible (that I know) this function | |
# requires that the array passed has been expanded (@) | |
function in_array() { | |
# Require both needle and haystack | |
if [ -z "$1" -o -z "$2" ]; then | |
return; | |
fi | |
# Use the first argument as needle | |
local needle="${1}" | |
# Then shift the arguments so that only the haystack remains | |
shift | |
# Create a new array from all remaining arguments | |
local array=(`echo "${@}"`) | |
local i | |
# Iterate through the array and return true (0) on match | |
for i in "${array[@]}" | |
do | |
if [ "${i}" = "${needle}" ]; then | |
return 0 | |
fi | |
done | |
return 1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment