Skip to content

Commit

Permalink
refactor/robust 💪 : fix Bash Pitfalls No.5
Browse files Browse the repository at this point in the history
  • Loading branch information
oldratlee committed Nov 21, 2023
1 parent 392ecbe commit 3b92d9d
Show file tree
Hide file tree
Showing 18 changed files with 31 additions and 31 deletions.
2 changes: 1 addition & 1 deletion bin/a2l
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down
6 changes: 3 additions & 3 deletions bin/ap
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion bin/c
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down
10 changes: 5 additions & 5 deletions bin/cp-into-docker-run
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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")"
Expand All @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion bin/find-in-jars
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down
4 changes: 2 additions & 2 deletions bin/rp
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions bin/show-busy-java-threads
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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!"
Expand Down
2 changes: 1 addition & 1 deletion bin/tcp-connection-state-counter
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down
2 changes: 1 addition & 1 deletion bin/uq
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down
8 changes: 4 additions & 4 deletions bin/xpf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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" "$@"
6 changes: 3 additions & 3 deletions bin/xpl
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -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
;;
Expand Down
2 changes: 1 addition & 1 deletion legacy-bin/cp-svn-url
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion legacy-bin/svn-merge-stop-on-copy
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# @author jiangjizhong(@jzwlqx)
# @author Jerry Lee (oldratlee at gmail dot com)

PROG="$(basename "$0")"
PROG="$(basename -- "$0")"

usage() {
cat <<EOF
Expand Down
2 changes: 1 addition & 1 deletion lib/console-text-color-themes.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ if command -v greadlink >/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
Expand Down
2 changes: 1 addition & 1 deletion test-cases/integration-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test-cases/lint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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$' |
Expand Down
2 changes: 1 addition & 1 deletion test-cases/parseOpts_test.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/bash

BASE="$(dirname "${BASH_SOURCE[0]}")"
BASE="$(dirname -- "${BASH_SOURCE[0]}")"

source "$BASE/../lib/parseOpts.sh"

Expand Down
2 changes: 1 addition & 1 deletion test-cases/uq_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"

#################################################
Expand Down

0 comments on commit 3b92d9d

Please sign in to comment.