From 2a5ff80264825e21adb5ff107a3d01237b86f559 Mon Sep 17 00:00:00 2001 From: Eli Schwartz Date: Wed, 13 Dec 2023 00:01:39 -0500 Subject: [PATCH] use portable "command -v" to detect installed programs, part 4 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 updates a couple build scripts to not rely on fixed paths calculated upfront. Checking the location of a tool with cmd=$(which foo) just to run it as `$cmd` is pointless. It's exactly equivalent to run it as `foo`, but less error-prone and easier to read. For one of the scripts, it also simplifies checking for their existence. Personally, I am skeptical it even makes sense to check at all. POSIX mandates they exist, and it's exceedingly unlikely they will not be installed. However, unlike the shell interpreter itself, we don't *know* they are installed, so leave the existing checks in but simplified. Change-Id: I4703c1165eb3a5aeb45fbab18df3222e8f5f9551 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/160665 Tested-by: Jenkins Tested-by: Ilmari Lauhakangas Reviewed-by: Ilmari Lauhakangas --- bin/removetooltip_markups.sh | 8 +++----- solenv/bin/add-modelines | 33 +++++++++++---------------------- 2 files changed, 14 insertions(+), 27 deletions(-) diff --git a/bin/removetooltip_markups.sh b/bin/removetooltip_markups.sh index 5699fce99263..056c9753e510 100755 --- a/bin/removetooltip_markups.sh +++ b/bin/removetooltip_markups.sh @@ -10,13 +10,11 @@ # Run the script in the core directory to remove all tooltip_markup # properties from the .ui files -SED_BIN=`which sed` -CUT_BIN=`which cut` LOG_FILE="modified-$(date +%s).log" removeTooltipMarkup() { - LINE=$(grep -n " temp && mv temp $1 echo "removed $TEXT from $1 at line $LINE" >> $LOG_FILE @@ -24,8 +22,8 @@ removeTooltipMarkup() changeTooltipMarkup() { - LINE=$(grep -n " temp && mv temp $1 + LINE=$(grep -n " temp && mv temp $1 echo "renamed tooltip_markup from $1 at line $LINE" >> $LOG_FILE } diff --git a/solenv/bin/add-modelines b/solenv/bin/add-modelines index a3f59fe8ead4..d3205ac7eee1 100755 --- a/solenv/bin/add-modelines +++ b/solenv/bin/add-modelines @@ -43,24 +43,13 @@ ModelineReplace="false" function SetEnvironment() { - if [ -n "$(which tail)" ] && [ -n "$(which head)" ]; then - { - headCMD=$(which head) - tailCMD=$(which tail) - } - else - { - echo "Missing head or tail, exiting..." - exit 1 - } + if ! command -v tail || ! command -v head; then + echo "Missing head or tail, exiting..." + exit 1 fi - if [ -n "$(which find)" ]; then - findCMD=$(which find) - else - { - echo "Missing find, exiting..." - exit 1 - } + if ! command -v find; then + echo "Missing find, exiting..." + exit 1 fi } @@ -72,15 +61,15 @@ function EditFile() FileToEdit="$1" - currentFirstLine=$($headCMD -1 "$FileToEdit") - currentLastLine=$($tailCMD -1 "$FileToEdit") + currentFirstLine=$(head -1 "$FileToEdit") + currentLastLine=$(tail -1 "$FileToEdit") case "$ModelineReplace" in "true" ) if [ "${currentFirstLine:0:6}" = "${FirstLine:0:6}" ]; then { echo "$FirstLine" > "$FileToEdit".new - $tailCMD -n +2 "$FileToEdit" >> "$FileToEdit".new + tail -n +2 "$FileToEdit" >> "$FileToEdit".new } fi if [ -e "$FileToEdit.new" ]; then @@ -90,7 +79,7 @@ function EditFile() fi if [ "${currentLastLine:0:6}" = "${LastLine:0:6}" ]; then { - $headCMD -n -1 "$FileToEdit" > "$FileToEdit".new + head -n -1 "$FileToEdit" > "$FileToEdit".new echo "$LastLine" >> "$FileToEdit".new } fi @@ -161,7 +150,7 @@ done # This gets rid of the final " -a " in the find argument list findArgs=(${findArgs:0:(${#findArgs}-3)}) -for file in $($findCMD "${findArgs[@]}"); do +for file in $(find "${findArgs[@]}"); do EditFile "$file" echo "Completed: " "$file" done