summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEli Schwartz <eschwartz93@gmail.com>2023-12-13 00:01:39 -0500
committerIlmari Lauhakangas <ilmari.lauhakangas@libreoffice.org>2024-01-22 13:46:45 +0100
commita1c854fffe0bf0f177535df320b60a328530ac70 (patch)
tree4ec483aa935a48bfcae53c4670d7a7ae245f01ea
parent27a1eccae1763b8efa17c909820f57f84361d308 (diff)
use portable "command -v" to detect installed programs, part 1
The "which" utility is not guaranteed to be installed either, and if it is, its behavior is not portable either. This means that when various programs are installed, the `which` check will report a fatal error because the which tool did not exist and the shell returned a nonzero status when attempting to fork+exec. If it did exist, it might not be an implementation of `which` that returns nonzero when commands do not exist. The general scripting suggestion is to use the "command -v" shell builtin; this is required to exist in all POSIX 2008 compliant shells, and is thus guaranteed to work everywhere. For some in-depth discussions on the topic, see: - https://mywiki.wooledge.org/BashFAQ/081 - https://unix.stackexchange.com/questions/85249/why-not-use-which-what-to-use-then/85250#85250 Examples of open-source shells likely to be installed as /bin/sh on Linux, which implement the 15-year-old standard: ash, bash, busybox, dash, ksh, mksh and zsh. This commit changes two programs installed to end-user systems. Change-Id: I6013965bb914f5b0d593a876866b991e210ef5b8 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/160662 Tested-by: Jenkins Tested-by: Ilmari Lauhakangas <ilmari.lauhakangas@libreoffice.org> Reviewed-by: Ilmari Lauhakangas <ilmari.lauhakangas@libreoffice.org>
-rwxr-xr-xdesktop/scripts/soffice.sh8
-rwxr-xr-xshell/source/unix/misc/senddoc.sh15
2 files changed, 5 insertions, 18 deletions
diff --git a/desktop/scripts/soffice.sh b/desktop/scripts/soffice.sh
index 8866a2cc8cf1..90f5ec784027 100755
--- a/desktop/scripts/soffice.sh
+++ b/desktop/scripts/soffice.sh
@@ -77,7 +77,7 @@ test -n "$RR" && EXTRAOPT="--record"
for arg in "$@" $EXTRAOPT ; do
case "$arg" in
--record)
- if which rr >/dev/null 2>&1 ; then
+ if command -v rr >/dev/null ; then
# smoketest may already be recorded => ignore nested
RRCHECK="rr record --nested=ignore"
checks="c$checks"
@@ -87,7 +87,7 @@ for arg in "$@" $EXTRAOPT ; do
fi
;;
--backtrace)
- if which gdb >/dev/null 2>&1 ; then
+ if command -v gdb >/dev/null ; then
GDBTRACECHECK="gdb -nx --command=$sd_prog/gdbtrace --args"
checks="c$checks"
else
@@ -96,7 +96,7 @@ for arg in "$@" $EXTRAOPT ; do
fi
;;
--strace)
- if which strace >/dev/null 2>&1 ; then
+ if command -v strace >/dev/null ; then
STRACECHECK="strace -o strace.log -f -tt -s 256"
checks="c$checks"
else
@@ -106,7 +106,7 @@ for arg in "$@" $EXTRAOPT ; do
;;
--valgrind)
test -n "$VALGRINDCHECK" && continue;
- if which valgrind >/dev/null 2>&1 ; then
+ if command -v valgrind >/dev/null ; then
# another valgrind tool might be forced via the environment variable
test -z "$VALGRIND" && VALGRIND="memcheck"
# --trace-children-skip is pretty useful but supported only with valgrind >= 3.6.0
diff --git a/shell/source/unix/misc/senddoc.sh b/shell/source/unix/misc/senddoc.sh
index f70251ecf310..d4bc20176b5b 100755
--- a/shell/source/unix/misc/senddoc.sh
+++ b/shell/source/unix/misc/senddoc.sh
@@ -29,25 +29,12 @@ fi
# do not confuse the system mail clients with OOo and Java libraries
unset LD_LIBRARY_PATH
-# tries to locate the executable specified
-# as first parameter in the user's path.
-which() {
- if [ ! -z "$1" ]; then
- for i in $(echo "$PATH" | sed -e 's/^:/.:/g' -e 's/:$/:./g' -e 's/::/:.:/g' -e 's/:/ /g'); do
- if [ -x "$i/$1" -a ! -d "$i/$1" ]; then
- echo "$i/$1"
- break;
- fi
- done
- fi
-}
-
# checks for the original mozilla start script(s)
# and restrict the "-remote" semantics to those.
run_mozilla() {
# find mozilla script in PATH if necessary
if [ "$(basename "$1")" = "$1" ]; then
- moz=$(which "$1")
+ moz=$(command -v "$1")
else
moz=$1
fi