2023-11-13 14:11:21 +00:00

36 lines
835 B
Bash
Executable File

#!/bin/sh
## SPDX-FileCopyrightText: 2023 Benjamin Grande M. S. <ben.grande.b@gmail.com>
##
## SPDX-License-Identifier: AGPL-3.0-or-later
## Detect if program exists, fail otherwise.
## With the option '-s', it prints the path to the utility that exists.
## Usage: has program
## Usage: has -s program alternative
## Example: has vim
## Example: has -s vim vim.tiny vi
set -eu
## The 'command' program fails to detect builtin programs.
has_cmd="command -v"
command -v which >/dev/null 2>&1 && has_cmd="which"
action=""
test -n "${1-}" || exit 1
case "$1" in
-s) action=show; shift; test -n "${1-}" || exit 1;;
esac
for prog in "${@}"; do
cmd="$(${has_cmd} "${prog}" 2>/dev/null)" || continue
test -x "${cmd}" || continue
case "${action-}" in
show) printf '%s\n' "${cmd}"; exit 0;;
*) exit 0;;
esac
done
exit 1