| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
The "which" utility is not guaranteed to be installed, and if it is, its behavior is not portable. 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 12-year-old standard: ash, bash, busybox, dash, ksh, mksh and zsh. Signed-off-by: Eli Schwartz <eschwartz@archlinux.org>
|
This seems a high-risk change, in that verifying it across all our user's systems is not possible, we'll know if its a problem only when it breaks something that used to work, and its not clear - what actual problem is it fixing? Is there a system on which the current script doesn't work? |
Sorry, something went wrong.
|
Exactly the problem which I described: my system does not have the "which" thirdparty utility installed. (My system happens to be Arch Linux. The /usr/bin/which program is only a required operating system component on Debian, which I'm not running. I could optionally install GNU which, but it is not available by default and requires manual intervention to install it. This script fails to find any type of python3.* program, and eventually does the fallback on python which does happen to exist.) ... Furthermore, if it does exist, it might not do what you think it does. For some in-depth discussions on the topic, see https://mywiki.wooledge.org/BashFAQ/081 or https://unix.stackexchange.com/questions/85249/why-not-use-which-what-to-use-then/85250#85250 ... This is not high-risk, since the configure script must be run with /bin/sh and every single /bin/sh on any system anywhere has a builtin shell feature called command -v, as long as it was produced within the last 12 years, and a number of them have had it for much, much longer.
On any POSIX 2008 compliant /bin/sh, a very old standard indeed, command -v must work, and must return a successful exit code when the searched-for command exists as a path or shell function or builtin. You will know if it is a shell function, because it will be defined in your script, which is under your control... you also know that python3.8 is not a shell builtin. It is literally the definition of portable. |
Sorry, something went wrong.
There was a problem hiding this comment.
I've seen command -v being used as a more portable approach in other projects, so LGTM. If we want to be extra careful, we could have a double-check, something like:
WHICH=($(command -v command >/dev/null && echo "command -v" || echo "which"))
"@{WHICH[@]}" python3.8 >/dev/null && exec python3.8 "$0" "$@"This would ensure we'll use command -v or which, depending on platform availability.
Sorry, something went wrong.
Sorry, something went wrong.
|
Using arrays is utterly unacceptable, since this is a /bin/sh script and arrays are not part of the shell command language. They're a bashism, which might work on some systems iff the system /bin/sh happens to be a symlink to bash or ksh or zsh, but this is much less likely to work than the which program... GNU autotools has a complex boilerplate routine which uses pure shell to find programs, including routines for detecting whether the $PATH uses the : character or the ; character for splitting elements, and, for kicks, includes support for finding the program with an optional extension such as ".bat" or ".exe" (but since such extensions cannot be reliably probed, it's the responsibility of the system vendor to define a suitable $ac_executable_extensions in their config.site; autotools is common enough that those vendors do indeed create the well-documented config.site). It's only a dozen or two lines. :p But none of this is necessary unless your actual goal is, like autotools configure scripts, to support building on systems that predate POSIX. And autotools takes their portability very seriously -- they also scrupulously go to pains to use the confusingly ambiguous, deprecated `command` form of command substitution, instead of $(command), since it's the only way to ensure autotools works on "the very oldest of non-POSIX-compatible bourne-shells". |
Sorry, something went wrong.
The "which" utility is not guaranteed to be installed, and if it is, its behavior is not portable. 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 12-year-old standard: ash, bash, busybox, dash, ksh, mksh and zsh. Signed-off-by: Eli Schwartz <eschwartz@archlinux.org> PR-URL: nodejs#32925 Reviewed-By: Richard Lau <riclau@uk.ibm.com> Reviewed-By: Matheus Marchini <mat@mmarchini.me> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
The "which" utility is not guaranteed to be installed, and if it is, its behavior is not portable. 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 12-year-old standard: ash, bash, busybox, dash, ksh, mksh and zsh. Signed-off-by: Eli Schwartz <eschwartz@archlinux.org> PR-URL: #32925 Reviewed-By: Richard Lau <riclau@uk.ibm.com> Reviewed-By: Matheus Marchini <mat@mmarchini.me> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
The "which" utility is not guaranteed to be installed, and if it is, its behavior is not portable. 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 12-year-old standard: ash, bash, busybox, dash, ksh, mksh and zsh. Signed-off-by: Eli Schwartz <eschwartz@archlinux.org> PR-URL: #32925 Reviewed-By: Richard Lau <riclau@uk.ibm.com> Reviewed-By: Matheus Marchini <mat@mmarchini.me> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
| Back | FazBrowse Home | New Git URL |
The "which" utility is not guaranteed to be installed, and if it is, its behavior is not portable.
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 12-year-old standard: ash, bash, busybox, dash, ksh, mksh and zsh.
Checklist