Skip to content

Instantly share code, notes, and snippets.

@mattmc3
Last active August 8, 2024 18:05
Show Gist options
  • Save mattmc3/b9051aba9e9d70778adcdaa940ed5d53 to your computer and use it in GitHub Desktop.
Save mattmc3/b9051aba9e9d70778adcdaa940ed5d53 to your computer and use it in GitHub Desktop.
POSIX helper functions
# Fish-like contains (https://fishshell.com/docs/current/cmds/contains.html)
contains() {(
while getopts "i" opt; do
case "$opt" in
i) o_index=1 ;;
*) return 1 ;;
esac
done
shift $(( OPTIND - 1 ))
if [ "$#" -eq 0 ]; then
echo >&2 "contains: key not specified"
return 1
fi
key="$1"; shift
index=0
for val in "$@"; do
index=$(( index + 1 ))
if [ "$val" = "$key" ]; then
[ -n "$o_index" ] && echo $index
return 0
fi
done
return 1
)}
# POSIX test for function existance.
is_function() {
[ "$#" -eq 1 ] || return 1
type "$1" | sed "s/$1//" | grep -qwi function
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment