1
0
Fork 0
mirror of https://github.com/notwa/rc synced 2024-06-01 15:33:07 -07:00
rc/sh/has

32 lines
1.2 KiB
Bash
Executable File

#!/usr/bin/env sh
# YES_ZSH
# YES_BASH
# YES_DASH
# YES_ASH
has() ( ### @- print the result of `which` if the program is found, else simply return 1.
### ```
### export SOLVER="$(has kissat || has picosat || has cadical || has minisat)"
### ```
fancy=0
[ -z "$ZSH_VERSION" ] && [ -z "$BASH_VERSION" ] || fancy=1
if [ "${1:-/}" = "${1#*[!A-Za-z_-]}" ]; then # has a simple name?
if [ $fancy = 0 ] || [ "${1:-/}" = "${1#-}" ]; then
# only ash and dash support aliases beginning with hyphens.
# forcefully unalias it (regardless of if it even was an alias):
alias "$1"=
unalias "$1"
fi
if [ $fancy = 1 ] || [ "${1:-/}" = "${1#*-}" ]; then
# only bash and zsh support function names containing hyphens.
# forcefully unset it (regardless of if it even was a function):
eval "$1() (:)"
unset -f "$1"
fi
fi
which -- "$1" >/dev/null 2>&1 && which -- "$1"
) # this uses parentheses instead of braces so that the function is always run
# in a subshell. otherwise, the user's aliases and functions may be overwritten.
[ -n "${preload+-}" ] || has "$@"