Last active
August 8, 2024 18:05
-
-
Save mattmc3/b9051aba9e9d70778adcdaa940ed5d53 to your computer and use it in GitHub Desktop.
POSIX helper functions
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
# 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 | |
)} |
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
# 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