shell: replace which with command -v for speed and POSIX compatibility

https://stackoverflow.com/a/677212/5132088

Signed-off-by: Yury V. Zaytsev <yury@shurup.com>
This commit is contained in:
Yury V. Zaytsev 2024-09-11 08:45:25 +02:00
parent 572312bd88
commit 50976cec62
9 changed files with 31 additions and 31 deletions

View File

@ -25,9 +25,9 @@ staroffice_console() {
} }
get_ooffice_executable() { get_ooffice_executable() {
if which loffice >/dev/null 2>&1; then if command -v loffice >/dev/null 2>&1; then
echo "loffice" echo "loffice"
elif which ooffice >/dev/null 2>&1; then elif command -v ooffice >/dev/null 2>&1; then
echo "ooffice" echo "ooffice"
else else
echo -n echo -n
@ -52,35 +52,35 @@ do_view_action() {
fi fi
;; ;;
msdoc) msdoc)
if which wvHtml >/dev/null 2>&1; then if command -v wvHtml >/dev/null 2>&1; then
tmp=`mktemp -d ${TMPDIR:-/tmp}/%p.XXXXXX` tmp=`mktemp -d ${TMPDIR:-/tmp}/%p.XXXXXX`
wvHtml "${MC_EXT_FILENAME}" --targetdir="$tmp" page.html wvHtml "${MC_EXT_FILENAME}" --targetdir="$tmp" page.html
elinks -dump "$tmp/page.html" elinks -dump "$tmp/page.html"
rm -rf "$tmp" rm -rf "$tmp"
elif which antiword >/dev/null 2>&1; then elif command -v antiword >/dev/null 2>&1; then
antiword -t "${MC_EXT_FILENAME}" antiword -t "${MC_EXT_FILENAME}"
elif which catdoc >/dev/null 2>&1; then elif command -v catdoc >/dev/null 2>&1; then
catdoc -w "${MC_EXT_FILENAME}" catdoc -w "${MC_EXT_FILENAME}"
elif which word2x >/dev/null 2>&1; then elif command -v word2x >/dev/null 2>&1; then
word2x -f text "${MC_EXT_FILENAME}" - word2x -f text "${MC_EXT_FILENAME}" -
else else
strings "${MC_EXT_FILENAME}" strings "${MC_EXT_FILENAME}"
fi fi
;; ;;
msxls) msxls)
if which xlhtml >/dev/null 2>&1; then if command -v xlhtml >/dev/null 2>&1; then
tmp=`mktemp -d ${TMPDIR:-/tmp}/%p.XXXXXX` tmp=`mktemp -d ${TMPDIR:-/tmp}/%p.XXXXXX`
xlhtml -a "${MC_EXT_FILENAME}" > "$tmp/page.html" xlhtml -a "${MC_EXT_FILENAME}" > "$tmp/page.html"
elinks -dump "$tmp/page.html" elinks -dump "$tmp/page.html"
rm -rf "$tmp" rm -rf "$tmp"
elif which xls2csv >/dev/null 2>&1; then elif command -v xls2csv >/dev/null 2>&1; then
xls2csv "${MC_EXT_FILENAME}" xls2csv "${MC_EXT_FILENAME}"
else else
strings "${MC_EXT_FILENAME}" strings "${MC_EXT_FILENAME}"
fi fi
;; ;;
dvi) dvi)
which dvi2tty >/dev/null 2>&1 && \ command -v dvi2tty >/dev/null 2>&1 && \
dvi2tty "${MC_EXT_FILENAME}" || \ dvi2tty "${MC_EXT_FILENAME}" || \
catdvi "${MC_EXT_FILENAME}" catdvi "${MC_EXT_FILENAME}"
;; ;;

View File

@ -18,13 +18,13 @@ do_view_action() {
[ -n "$DISPLAY" ] && sxpm "${MC_EXT_FILENAME}" [ -n "$DISPLAY" ] && sxpm "${MC_EXT_FILENAME}"
;; ;;
*) *)
if which exif >/dev/null 2>&1; then if command -v exif >/dev/null 2>&1; then
exif "${MC_EXT_FILENAME}" 2>/dev/null exif "${MC_EXT_FILENAME}" 2>/dev/null
E=$? E=$?
else else
E=1 E=1
fi fi
if [ $E != 0 ] && which exiftool >/dev/null 2>&1; then if [ $E != 0 ] && command -v exiftool >/dev/null 2>&1; then
exiftool "${MC_EXT_FILENAME}" 2>/dev/null exiftool "${MC_EXT_FILENAME}" 2>/dev/null
fi fi
identify "${MC_EXT_FILENAME}" identify "${MC_EXT_FILENAME}"
@ -47,20 +47,20 @@ do_open_action() {
;; ;;
*) *)
if [ -n "$DISPLAY" ]; then if [ -n "$DISPLAY" ]; then
if which geeqie >/dev/null 2>&1; then if command -v geeqie >/dev/null 2>&1; then
(geeqie "${MC_EXT_FILENAME}" &) (geeqie "${MC_EXT_FILENAME}" &)
else else
(gqview "${MC_EXT_FILENAME}" &) (gqview "${MC_EXT_FILENAME}" &)
fi fi
# no backgrounding for console viewers # no backgrounding for console viewers
elif which fim >/dev/null 2>&1; then elif command -v fim >/dev/null 2>&1; then
fim "${MC_EXT_FILENAME}" fim "${MC_EXT_FILENAME}"
elif which fbi >/dev/null 2>&1; then elif command -v fbi >/dev/null 2>&1; then
fbi "${MC_EXT_FILENAME}" fbi "${MC_EXT_FILENAME}"
elif which zgv >/dev/null 2>&1; then elif command -v zgv >/dev/null 2>&1; then
zgv "${MC_EXT_FILENAME}" zgv "${MC_EXT_FILENAME}"
# run-mailcap as a last resort # run-mailcap as a last resort
elif which see >/dev/null 2>&1; then elif command -v see >/dev/null 2>&1; then
(see "${MC_EXT_FILENAME}" &) (see "${MC_EXT_FILENAME}" &)
fi fi
;; ;;

View File

@ -13,7 +13,7 @@ do_view_action() {
case "${filetype}" in case "${filetype}" in
iso9660) iso9660)
if which isoinfo > /dev/null 2>&1; then if command -v isoinfo >/dev/null 2>&1; then
isoinfo -d -i "${MC_EXT_FILENAME}" && isoinfo -l -R -J -i "${MC_EXT_FILENAME}" isoinfo -d -i "${MC_EXT_FILENAME}" && isoinfo -l -R -J -i "${MC_EXT_FILENAME}"
else else
7za l "${MC_EXT_FILENAME}" 7za l "${MC_EXT_FILENAME}"
@ -74,7 +74,7 @@ do_open_action() {
sqlite3 "${MC_EXT_FILENAME}" sqlite3 "${MC_EXT_FILENAME}"
;; ;;
glade) glade)
if which glade-3 >/dev/null 2>&1; then if command -v glade-3 >/dev/null 2>&1; then
(glade-3 "${MC_EXT_FILENAME}" >/dev/null 2>&1 &) (glade-3 "${MC_EXT_FILENAME}" >/dev/null 2>&1 &)
else else
(glade-2 "${MC_EXT_FILENAME}" >/dev/null 2>&1 &) (glade-2 "${MC_EXT_FILENAME}" >/dev/null 2>&1 &)

View File

@ -129,7 +129,7 @@ do_open_action() {
;; ;;
chm) chm)
if [ -n "$DISPLAY" ]; then if [ -n "$DISPLAY" ]; then
which kchmviewer > /dev/null 2>&1 \ command -v kchmviewer >/dev/null 2>&1 \
&& (kchmviewer "${MC_EXT_FILENAME}" &) \ && (kchmviewer "${MC_EXT_FILENAME}" &) \
|| (xchm "${MC_EXT_FILENAME}" &) || (xchm "${MC_EXT_FILENAME}" &)
else else

View File

@ -15,10 +15,10 @@ do_view_action() {
case "${filetype}" in case "${filetype}" in
*) *)
if which mplayer >/dev/null 2>&1; then if command -v mplayer >/dev/null 2>&1; then
mplayer -identify -vo null -ao null -frames 0 "${MC_EXT_FILENAME}" 2>&1 | \ mplayer -identify -vo null -ao null -frames 0 "${MC_EXT_FILENAME}" 2>&1 | \
sed -n 's/^ID_//p' sed -n 's/^ID_//p'
elif which mpv_identify.sh >/dev/null 2>&1; then elif command -v mpv_identify.sh >/dev/null 2>&1; then
mpv_identify.sh "${MC_EXT_FILENAME}" mpv_identify.sh "${MC_EXT_FILENAME}"
else else
echo "Please install either mplayer or mpv to get information for this file" echo "Please install either mplayer or mpv to get information for this file"
@ -30,7 +30,7 @@ do_view_action() {
do_open_action() { do_open_action() {
filetype=$1 filetype=$1
if which mpv >/dev/null 2>&1; then if command -v mpv >/dev/null 2>&1; then
PLAYER="mpv --really-quiet" PLAYER="mpv --really-quiet"
else else
PLAYER="mplayer -really-quiet" PLAYER="mplayer -really-quiet"

View File

@ -32,7 +32,7 @@
#*** file scope functions ********************************************** #*** file scope functions **********************************************
XORRISO=$(which xorriso 2>/dev/null) XORRISO=$(command -v xorriso >/dev/null 2>&1)
# This snippet is used to undo xorriso's Text_shellsafe(). Pseudocode for the # This snippet is used to undo xorriso's Text_shellsafe(). Pseudocode for the
# function: # function:
@ -144,7 +144,7 @@ xorriso_rm() {
# tested to comply with isoinfo 2.0's output # tested to comply with isoinfo 2.0's output
test_iso () { test_iso () {
ISOINFO=$(which isoinfo 2>/dev/null) ISOINFO=$(command -v isoinfo >/dev/null 2>&1)
if test -z "$ISOINFO"; then if test -z "$ISOINFO"; then
echo "isoinfo not found" >&2 echo "isoinfo not found" >&2
return 1 return 1

View File

@ -17,10 +17,10 @@
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
P7ZIP=`which 7z 2>/dev/null` \ P7ZIP=`command -v 7z >/dev/null 2>&1` \
|| P7ZIP=`which 7zz 2>/dev/null` \ || P7ZIP=`command -v 7zz >/dev/null 2>&1` \
|| P7ZIP=`which 7za 2>/dev/null` \ || P7ZIP=`command -v 7za >/dev/null 2>&1` \
|| P7ZIP=`which 7zr 2>/dev/null` \ || P7ZIP=`command -v 7zr >/dev/null 2>&1` \
|| P7ZIP="" || P7ZIP=""
# Let the test framework hook in: # Let the test framework hook in:

View File

@ -16,7 +16,7 @@
RAR=rar RAR=rar
# Prefer unrar (freeware). # Prefer unrar (freeware).
UNRAR=`which unrar 2>/dev/null` UNRAR=`command -v unrar >/dev/null 2>&1`
[ -z $UNRAR ] && UNRAR=$RAR [ -z $UNRAR ] && UNRAR=$RAR
[ ! -x $UNRAR -a -x $RAR ] && UNRAR=$RAR [ ! -x $UNRAR -a -x $RAR ] && UNRAR=$RAR

View File

@ -10,8 +10,8 @@
# On Debian/Ubuntu wimtools can be installed via: # On Debian/Ubuntu wimtools can be installed via:
# apt install wimtools # apt install wimtools
which wimlib-imagex 2>/dev/null > /dev/null || exit 1 command -v wimlib-imagex >/dev/null 2>&1 || exit 1
WIM=`which wimlib-imagex` WIM=`command -v wimlib-imagex >/dev/null 2>&1`
AWK=@AWK@ AWK=@AWK@
[ -n "$2" ] || exit 1 [ -n "$2" ] || exit 1