From 3b92d9d11e79c9b5e36360680701e9bf055cdf4a Mon Sep 17 00:00:00 2001 From: Jerry Lee Date: Tue, 21 Nov 2023 14:11:27 +0800 Subject: [PATCH] =?UTF-8?q?refactor/robust=20=F0=9F=92=AA=20:=20fix=20Bash?= =?UTF-8?q?=20Pitfalls=20No.5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Bash Pitfalls No.5 http://mywiki.wooledge.org/BashPitfalls#cd_.24.28dirname_.22.24f.22.29 --- bin/a2l | 2 +- bin/ap | 6 +++--- bin/c | 2 +- bin/cp-into-docker-run | 10 +++++----- bin/find-in-jars | 2 +- bin/rp | 4 ++-- bin/show-busy-java-threads | 4 ++-- bin/tcp-connection-state-counter | 2 +- bin/uq | 2 +- bin/xpf | 8 ++++---- bin/xpl | 6 +++--- legacy-bin/cp-svn-url | 2 +- legacy-bin/svn-merge-stop-on-copy | 2 +- lib/console-text-color-themes.sh | 2 +- test-cases/integration-test.sh | 2 +- test-cases/lint.sh | 2 +- test-cases/parseOpts_test.sh | 2 +- test-cases/uq_test.sh | 2 +- 18 files changed, 31 insertions(+), 31 deletions(-) diff --git a/bin/a2l b/bin/a2l index 9f8faf44..d751aeda 100755 --- a/bin/a2l +++ b/bin/a2l @@ -11,7 +11,7 @@ set -eEuo pipefail # NOTE: DO NOT declare var PROG as readonly in ONE line! -PROG="$(basename "$0")" +PROG="$(basename -- "$0")" readonly PROG readonly PROG_VERSION='2.6.0-dev' diff --git a/bin/ap b/bin/ap index 355163dc..51c587a5 100755 --- a/bin/ap +++ b/bin/ap @@ -13,7 +13,7 @@ set -eEuo pipefail # NOTE: DO NOT declare var PROG as readonly in ONE line! -PROG="$(basename "$0")" +PROG="$(basename -- "$0")" readonly PROG readonly PROG_VERSION='2.6.0-dev' @@ -50,12 +50,12 @@ portableReadLink() { uname="$(uname)" case "$uname" in Linux* | CYGWIN* | MINGW*) - readlink -f "$file" + readlink -f -- "$file" ;; Darwin*) local py_args=(-c 'import os, sys; print(os.path.realpath(sys.argv[1]))' "$file") if command -v greadlink >/dev/null; then - greadlink -f "$file" + greadlink -f -- "$file" elif command -v python3 >/dev/null; then python3 "${py_args[@]}" elif command -v python >/dev/null; then diff --git a/bin/c b/bin/c index aaaf4d89..9ebd0e75 100755 --- a/bin/c +++ b/bin/c @@ -27,7 +27,7 @@ set -eEuo pipefail # NOTE: DO NOT declare var PROG as readonly in ONE line! -PROG="$(basename "$0")" +PROG="$(basename -- "$0")" readonly PROG readonly PROG_VERSION='2.6.0-dev' diff --git a/bin/cp-into-docker-run b/bin/cp-into-docker-run index fc4af8f8..bf1dd578 100755 --- a/bin/cp-into-docker-run +++ b/bin/cp-into-docker-run @@ -9,7 +9,7 @@ # @author Jerry Lee (oldratlee at gmail dot com) set -eEuo pipefail -PROG="$(basename "$0")" +PROG="$(basename -- "$0")" readonly PROG readonly PROG_VERSION='2.6.0-dev' @@ -43,12 +43,12 @@ portableReadLink() { uname="$(uname)" case "$uname" in Linux* | CYGWIN* | MINGW*) - readlink -f "$file" + readlink -f -- "$file" ;; Darwin*) local py_args=(-c 'import os, sys; print(os.path.realpath(sys.argv[1]))' "$file") if command -v greadlink >/dev/null; then - greadlink -f "$file" + greadlink -f -- "$file" elif command -v python3 >/dev/null; then python3 "${py_args[@]}" elif command -v python >/dev/null; then @@ -205,7 +205,7 @@ if [ ! -f "$specified_run_command" ]; then run_command="$(which "$specified_run_command")" fi run_command="$(portableReadLink "$run_command")" -run_command_base_name="$(basename "$run_command")" +run_command_base_name="$(basename -- "$run_command")" readonly run_command run_command_base_name run_timestamp="$(date "+%Y%m%d_%H%M%S")" @@ -218,7 +218,7 @@ if [ -n "${docker_command_cp_path}" ]; then else readonly run_command_in_docker="${docker_workdir:+"$docker_workdir/"}$docker_command_cp_path" fi - run_command_dir_in_docker="$(dirname "$run_command_in_docker")" + run_command_dir_in_docker="$(dirname -- "$run_command_in_docker")" readonly run_command_dir_in_docker else readonly work_tmp_dir_in_docker="$docker_tmpdir/$uuid" diff --git a/bin/find-in-jars b/bin/find-in-jars index cd69bee5..9805e714 100755 --- a/bin/find-in-jars +++ b/bin/find-in-jars @@ -32,7 +32,7 @@ set -eEuo pipefail # NOTE: DO NOT declare var PROG as readonly in ONE line! -PROG="$(basename "$0")" +PROG="$(basename -- "$0")" readonly PROG readonly PROG_VERSION='2.6.0-dev' diff --git a/bin/rp b/bin/rp index a9f7e859..960f2bda 100755 --- a/bin/rp +++ b/bin/rp @@ -13,7 +13,7 @@ set -eEuo pipefail # NOTE: DO NOT declare var PROG as readonly in ONE line! -PROG="$(basename "$0")" +PROG="$(basename -- "$0")" readonly PROG readonly PROG_VERSION='2.6.0-dev' @@ -140,7 +140,7 @@ else files=("${files[@]:0:argc-1}") fi -[ -f "$relativeTo" ] && relativeTo="$(dirname "$relativeTo")" +[ -f "$relativeTo" ] && relativeTo="$(dirname -- "$relativeTo")" [ -e "$relativeTo" ] || die "relativeTo dir($relativeTo) does NOT exists!" readonly files relativeTo diff --git a/bin/show-busy-java-threads b/bin/show-busy-java-threads index 777660ae..a7f2e30c 100755 --- a/bin/show-busy-java-threads +++ b/bin/show-busy-java-threads @@ -26,7 +26,7 @@ # var2=$(echo value1) # NOTE: DO NOT declare var PROG as readonly in ONE line! -PROG="$(basename "$0")" +PROG="$(basename -- "$0")" readonly PROG readonly PROG_VERSION='2.6.0-dev' # choosing between $0 and BASH_SOURCE @@ -312,7 +312,7 @@ if [ -n "$append_file" ]; then [ -f "$append_file" ] || die "$append_file(specified by option -a, for storing run output files) exists but is not a file!" [ -w "$append_file" ] || die "file $append_file(specified by option -a, for storing run output files) exists but is not writable!" else - append_file_dir="$(dirname "$append_file")" + append_file_dir="$(dirname -- "$append_file")" if [ -e "$append_file_dir" ]; then [ -d "$append_file_dir" ] || die "directory $append_file_dir(specified by option -a, for storing run output files) exists but is not a directory!" [ -w "$append_file_dir" ] || die "directory $append_file_dir(specified by option -a, for storing run output files) exists but is not writable!" diff --git a/bin/tcp-connection-state-counter b/bin/tcp-connection-state-counter index 64d78601..ec0440b7 100755 --- a/bin/tcp-connection-state-counter +++ b/bin/tcp-connection-state-counter @@ -11,7 +11,7 @@ set -eEuo pipefail # NOTE: DO NOT declare var PROG as readonly in ONE line! -PROG="$(basename "$0")" +PROG="$(basename -- "$0")" readonly PROG readonly PROG_VERSION='2.6.0-dev' diff --git a/bin/uq b/bin/uq index 712bbc46..51447e1f 100755 --- a/bin/uq +++ b/bin/uq @@ -29,7 +29,7 @@ set -eEuo pipefail # NOTE: DO NOT declare var PROG as readonly in ONE line! -PROG="$(basename "$0")" +PROG="$(basename -- "$0")" readonly PROG readonly PROG_VERSION='2.6.0-dev' diff --git a/bin/xpf b/bin/xpf index 8baf4d02..a0c52cd3 100755 --- a/bin/xpf +++ b/bin/xpf @@ -22,12 +22,12 @@ portableReadLink() { uname="$(uname)" case "$uname" in Linux* | CYGWIN* | MINGW*) - readlink -f "$file" + readlink -f -- "$file" ;; Darwin*) local py_args=(-c 'import os, sys; print(os.path.realpath(sys.argv[1]))' "$file") if command -v greadlink >/dev/null; then - greadlink -f "$file" + greadlink -f -- "$file" elif command -v python3 >/dev/null; then python3 "${py_args[@]}" elif command -v python >/dev/null; then @@ -49,9 +49,9 @@ portableReadLink() { ################################################################################ # DO NOT inline THIS_SCRIPT into BASE_DIR, because sub-shell: -# BASE_DIR="$(dirname "$(portableReadLink "${BASH_SOURCE[0]}")")" +# BASE_DIR="$(dirname -- "$(portableReadLink "${BASH_SOURCE[0]}")")" THIS_SCRIPT="$(portableReadLink "${BASH_SOURCE[0]}")" -BASE_DIR="$(dirname "$THIS_SCRIPT")" +BASE_DIR="$(dirname -- "$THIS_SCRIPT")" # shellcheck disable=SC1091 source "$BASE_DIR/xpl" "$@" diff --git a/bin/xpl b/bin/xpl index 4c1b7a28..7ae388ba 100755 --- a/bin/xpl +++ b/bin/xpl @@ -26,7 +26,7 @@ set -eEuo pipefail # NOTE: DO NOT declare var PROG as readonly in ONE line! -PROG="$(basename "$0")" +PROG="$(basename -- "$0")" readonly PROG readonly PROG_VERSION='2.6.0-dev' @@ -129,12 +129,12 @@ openOneFile() { ;; *) if [ -d "${file}" ]; then - nautilus "$(dirname "${file}")" + nautilus "$(dirname -- "${file}")" else if $slt; then nautilus "${file}" else - nautilus "$(dirname "${file}")" + nautilus "$(dirname -- "${file}")" fi fi ;; diff --git a/legacy-bin/cp-svn-url b/legacy-bin/cp-svn-url index 064428b0..d0534517 100755 --- a/legacy-bin/cp-svn-url +++ b/legacy-bin/cp-svn-url @@ -26,7 +26,7 @@ # var2=$(echo value1) # NOTE: DO NOT declare var PROG as readonly in ONE line! -PROG="$(basename "$0")" +PROG="$(basename -- "$0")" readonly PROG_VERSION='2.6.0-dev' usage() { diff --git a/legacy-bin/svn-merge-stop-on-copy b/legacy-bin/svn-merge-stop-on-copy index 8821bcc4..28c04fa1 100755 --- a/legacy-bin/svn-merge-stop-on-copy +++ b/legacy-bin/svn-merge-stop-on-copy @@ -11,7 +11,7 @@ # @author jiangjizhong(@jzwlqx) # @author Jerry Lee (oldratlee at gmail dot com) -PROG="$(basename "$0")" +PROG="$(basename -- "$0")" usage() { cat </dev/null; then fi # NOTE: DO NOT declare var _ctct_PROG as readonly in ONE line! -_ctct_PROG="$(basename "$($_ctct_READLINK_CMD -f "${BASH_SOURCE[0]}")")" +_ctct_PROG="$(basename -- "$($_ctct_READLINK_CMD -f -- "${BASH_SOURCE[0]}")")" [ "$_ctct_PROG" == 'console-text-color-themes.sh' ] && readonly _ctct_is_direct_run=true readonly _ctct_ec=$'\033' # escape char diff --git a/test-cases/integration-test.sh b/test-cases/integration-test.sh index ae909c84..e7e5e8e7 100755 --- a/test-cases/integration-test.sh +++ b/test-cases/integration-test.sh @@ -6,7 +6,7 @@ if command -v greadlink &>/dev/null; then READLINK_CMD=greadlink fi -cd "$(dirname "$($READLINK_CMD -f "${BASH_SOURCE[0]}")")" +cd "$(dirname -- "$($READLINK_CMD -f -- "${BASH_SOURCE[0]}")")" ################################################################################ # constants diff --git a/test-cases/lint.sh b/test-cases/lint.sh index ab2691aa..9cef7d3a 100755 --- a/test-cases/lint.sh +++ b/test-cases/lint.sh @@ -2,7 +2,7 @@ set -eEuo pipefail # cd to the root of the project -cd "$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")"/.. +cd "$(dirname -- "$(readlink -f -- "${BASH_SOURCE[0]}")")"/.. find bin lib legacy-bin -type f | grep -Pv '/show-duplicate-java-classes$' | diff --git a/test-cases/parseOpts_test.sh b/test-cases/parseOpts_test.sh index 93e5704d..03a2acac 100755 --- a/test-cases/parseOpts_test.sh +++ b/test-cases/parseOpts_test.sh @@ -1,6 +1,6 @@ #!/bin/bash -BASE="$(dirname "${BASH_SOURCE[0]}")" +BASE="$(dirname -- "${BASH_SOURCE[0]}")" source "$BASE/../lib/parseOpts.sh" diff --git a/test-cases/uq_test.sh b/test-cases/uq_test.sh index 242f79b8..93aa69cf 100755 --- a/test-cases/uq_test.sh +++ b/test-cases/uq_test.sh @@ -6,7 +6,7 @@ if command -v greadlink &>/dev/null; then READLINK_CMD=greadlink fi -BASE="$(dirname "$($READLINK_CMD -f "${BASH_SOURCE[0]}")")" +BASE="$(dirname -- "$($READLINK_CMD -f -- "${BASH_SOURCE[0]}")")" cd "$BASE" #################################################