Ben Grande d13a21a734
fix: avoid echo usage
Echo can interpret operand as an option and checking every variable to
be echoed is troublesome while with printf, if the format specifier is
present before the operand, printing as string can be enforced.
2024-08-06 18:12:46 +02:00

38 lines
897 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;;
"") printf '%s\n' "Argument required" >&2; 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