Skip to contentSkip to conversation

grub-mkconfig: use portable 'command -v' to detect installed programs

PATCH In review Eli Schwartz 2 participants 3 messages Oct 17, 2019 – Oct 23, 2019

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 grub-mkconfig: use portable 'command -v' to detect installed programs

The 'which' utility is not guaranteed to be installed either, and if it is, its behavior is not portable either.

Conversely, the 'command -v' shell builtin is required to exist in all POSIX 2008 compliant shells, and is thus guaranteed to work everywhere.

Examples of open-source shells likely to be installed as /bin/sh on linux, which implement the 11-year-old standard:
bash, dash, busybox ash, ksh, mksh, zsh

A side benefit of using the POSIX portable option is that it requires neither an external disk executable, nor (because unlike 'which', the exit code is reliable) a subshell fork. This therefore represents a mild speedup.

---
 util/grub-mkconfig_lib.in   | 2 +-
 util/grub.d/30_os-prober.in | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
util/grub-mkconfig_lib.in +1 -1
index 0f801cab3..50f2908d2 100644
@@ -34,7 +34,7 @@ if test "x$grub_mkrelpath" = x; then
   grub_mkrelpath="${bindir}/@grub_mkrelpath@"
 fi
 
-if which gettext >/dev/null 2>/dev/null; then
+if command -v gettext >/dev/null; then
   :
 else
   gettext () {
util/grub.d/30_os-prober.in +1 -1
index 515a68c7a..ed3171417 100644
@@ -30,7 +30,7 @@ if [ "x${GRUB_DISABLE_OS_PROBER}" = "xtrue" ]; then
   exit 0
 fi
 
-if [ -z "`which os-prober 2> /dev/null`" ] || [ -z "`which linux-boot-prober 2> /dev/null`" ] ; then
+if ! command -v os-prober > /dev/null || ! command -v linux-boot-prober > /dev/null ; then
   # missing os-prober and/or linux-boot-prober
   exit 0
 fi
Signature · 1 line
2.23.0
#1 lore raw
Quoted 6 lines · Eli Schwartz

The 'which' utility is not guaranteed to be installed either, and if it is, its behavior is not portable either.

Conversely, the 'command -v' shell builtin is required to exist in all POSIX 2008 compliant shells, and is thus guaranteed to work everywhere.

Quoted 8 lines

Examples of open-source shells likely to be installed as /bin/sh on linux, which implement the 11-year-old standard:
bash, dash, busybox ash, ksh, mksh, zsh

A side benefit of using the POSIX portable option is that it requires neither an external disk executable, nor (because unlike 'which', the exit code is reliable) a subshell fork. This therefore represents a mild speedup.

Missing SOB. I can add it in your behalf if this not a problem for you. Otherwise Reviewed-by: Daniel Kiper <[email protected]>

Daniel

#2 lore raw
Quoted 11 lines · Daniel Kiper
Quoted 10 lines · Eli Schwartz

The 'which' utility is not guaranteed to be installed either, and if it is, its behavior is not portable either.

Conversely, the 'command -v' shell builtin is required to exist in all POSIX 2008 compliant shells, and is thus guaranteed to work everywhere.

Examples of open-source shells likely to be installed as /bin/sh on linux, which implement the 11-year-old standard:
bash, dash, busybox ash, ksh, mksh, zsh

Quoted 7 lines
Quoted 4 lines

A side benefit of using the POSIX portable option is that it requires neither an external disk executable, nor (because unlike 'which', the exit code is reliable) a subshell fork. This therefore represents a mild speedup.

Missing SOB. I can add it in your behalf if this not a problem for you. Otherwise Reviewed-by: Daniel Kiper <[email protected]>

My apologies for the missing Signed-off-by, I did not realize that this project expects to see them and I usually configure that per-repository.

You are more than welcome to add it on my behalf, and I hereby confirm my Developer Certificate of Origin

Signature · 2 lines
Eli Schwartz
Arch Linux Bug Wrangler and Trusted User