Skip to content

Commit

Permalink
refactor: remove quote for simple assignment, simplify code and impro…
Browse files Browse the repository at this point in the history
…ve readability 🛠️

more info about assignment see
https://www.gnu.org/software/bash/manual/html_node/Shell-Parameters.html

If value is not given, the variable is assigned the null string. All values undergo...
  • Loading branch information
oldratlee committed Jan 10, 2024
1 parent 080b385 commit a22100b
Show file tree
Hide file tree
Showing 17 changed files with 96 additions and 96 deletions.
8 changes: 4 additions & 4 deletions 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 All @@ -20,7 +20,7 @@ readonly PROG_VERSION='2.6.0-dev'
################################################################################

colorPrint() {
local color="$1"
local color=$1
shift
# if stdout is a terminal, turn on color output.
# '-t' check: is a terminal?
Expand Down Expand Up @@ -94,13 +94,13 @@ readonly args
readonly -a ROTATE_COLORS=(33 35 36 31 32 37 34)
COUNT=0
rotateColorPrint() {
local content="$*"
local content=$*

# skip color for white space
if [[ "$content" =~ ^[[:space:]]*$ ]]; then
printf '%s\n' "$content"
else
local color="${ROTATE_COLORS[COUNT++ % ${#ROTATE_COLORS[@]}]}"
local color=${ROTATE_COLORS[COUNT++ % ${#ROTATE_COLORS[@]}]}
colorPrint "$color" "$content"
fi
}
Expand Down
10 changes: 5 additions & 5 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 All @@ -22,7 +22,7 @@ readonly PROG_VERSION='2.6.0-dev'
################################################################################

colorPrint() {
local color="$1"
local color=$1
shift
# if stdout is a terminal, turn on color output.
# '-t' check: is a terminal?
Expand All @@ -46,9 +46,9 @@ die() {
# How can I get the behavior of GNU's readlink -f on a Mac?
# https://stackoverflow.com/questions/1055671
portableReadLink() {
local file="$1" uname
local file=$1 uname

uname="$(uname)"
uname=$(uname)
case "$uname" in
Linux* | CYGWIN* | MINGW*)
readlink -f -- "$file"
Expand All @@ -72,7 +72,7 @@ portableReadLink() {
}

usage() {
local -r exit_code="${1:-0}"
local -r exit_code=${1:-0}
(($# > 0)) && shift
local -r out=$(((exit_code != 0) + 1))

Expand Down
6 changes: 3 additions & 3 deletions 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 All @@ -47,7 +47,7 @@ printErrorMsg() {
}

usage() {
local -r exit_code="${1:-0}"
local -r exit_code=${1:-0}
(($# > 0)) && shift
local -r out=$(((exit_code != 0) + 1))

Expand Down Expand Up @@ -140,7 +140,7 @@ copy() {

catThenCopy() {
local content
content="$(cat)"
content=$(cat)
if $keep_eol; then
printf '%s\n' "$content"
else
Expand Down
4 changes: 2 additions & 2 deletions bin/coat
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ set -eEuo pipefail
readonly -a ROTATE_COLORS=(33 35 36 31 32 37 34)
COUNT=0
rotateColorPrint() {
local content="$*"
local content=$*

# skip color for white space
if [[ "$content" =~ ^[[:space:]]*$ ]]; then
printf '%s\n' "$content"
else
local color="${ROTATE_COLORS[COUNT++ % ${#ROTATE_COLORS[@]}]}"
local color=${ROTATE_COLORS[COUNT++ % ${#ROTATE_COLORS[@]}]}
printf '\e[1;%sm%s\e[0m\n' "$color" "$content"
fi
}
Expand Down
38 changes: 19 additions & 19 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 @@ -40,9 +40,9 @@ isAbsolutePath() {
# How can I get the behavior of GNU's readlink -f on a Mac?
# https://stackoverflow.com/questions/1055671
portableReadLink() {
local file="$1" uname
local file=$1 uname

uname="$(uname)"
uname=$(uname)
case "$uname" in
Linux* | CYGWIN* | MINGW*)
readlink -f -- "$file"
Expand All @@ -66,7 +66,7 @@ portableReadLink() {
}

usage() {
local -r exit_code="${1:-0}"
local -r exit_code=${1:-0}
(($# > 0)) && shift
local -r out=$(((exit_code != 0) + 1))

Expand Down Expand Up @@ -126,23 +126,23 @@ declare -a args=()
while (($# > 0)); do
case "$1" in
-c | --container)
container_name="$2"
container_name=$2
shift 2
;;
-u | --docker-user)
docker_user="$2"
docker_user=$2
shift 2
;;
-w | --workdir)
docker_workdir="$2"
docker_workdir=$2
shift 2
;;
-t | --tmpdir)
docker_tmpdir="$2"
docker_tmpdir=$2
shift 2
;;
-p | --cp-path)
docker_command_cp_path="$2"
docker_command_cp_path=$2
shift 2
;;
-v | --verbose)
Expand Down Expand Up @@ -198,35 +198,35 @@ command -v docker &>/dev/null || die 'docker command not found!'
# prepare vars for docker operation
########################################

readonly specified_run_command="${args[0]}"
run_command="$specified_run_command"
readonly specified_run_command=${args[0]}
run_command=$specified_run_command
if [ ! -f "$specified_run_command" ]; then
which "$specified_run_command" &>/dev/null ||
die "specified command not exists and not found in PATH: $specified_run_command"

run_command="$(which "$specified_run_command")"
run_command=$(which "$specified_run_command")
fi
run_command="$(portableReadLink "$run_command")"
run_command_base_name="$(basename -- "$run_command")"
run_command=$(portableReadLink "$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")"
run_timestamp=$(date "+%Y%m%d_%H%M%S")
readonly run_timestamp
readonly uuid="${PROG}_${run_timestamp}_${$}_${RANDOM}"

if [ -n "${docker_command_cp_path}" ]; then
if isAbsolutePath "$docker_command_cp_path"; then
readonly run_command_in_docker="$docker_command_cp_path"
readonly run_command_in_docker=$docker_command_cp_path
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"
readonly work_tmp_dir_in_docker=$docker_tmpdir/$uuid

readonly run_command_in_docker="$work_tmp_dir_in_docker/$run_command_base_name"
readonly run_command_dir_in_docker="$work_tmp_dir_in_docker"
readonly run_command_dir_in_docker=$work_tmp_dir_in_docker
fi

cleanupWhenExit() {
Expand Down
4 changes: 2 additions & 2 deletions bin/echo-args
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ set -eEuo pipefail
digitCount() {
# argument 1(num) is always a non-negative integer in this script usage,
# so NO argument validation logic.
local num="$1" count=0
local num=$1 count=0
while ((num != 0)); do
((++count))
((num = num / 10))
Expand All @@ -23,7 +23,7 @@ readonly arg_count=$# digit_count
readonly red='\e[1;31m' blue='\e[1;36m' color_reset='\e[0m'

printArg() {
local idx="$1" value="$2"
local idx=$1 value=$2

# if stdout is a terminal, turn on color output.
# '-t' check: is a terminal?
Expand Down
20 changes: 10 additions & 10 deletions 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 Expand Up @@ -74,7 +74,7 @@ printResponsiveMessage() {
return
fi

local content="$*"
local content=$*
# http://www.linuxforums.org/forum/red-hat-fedora-linux/142825-how-truncate-string-bash-script.html
printf %b%s "${clear_line}" "${content:0:columns}" >&2
}
Expand All @@ -94,7 +94,7 @@ die() {
}

usage() {
local -r exit_code="${1:-0}"
local -r exit_code=${1:-0}
(($# > 0)) && shift
local -r out=$(((exit_code != 0) + 1))

Expand Down Expand Up @@ -203,7 +203,7 @@ while (($# > 0)); do
;;
# support the legacy typo option name --seperator for compatibility
-s | --separator | --seperator)
separator="$2"
separator=$2
shift 2
;;
-L | --files-not-contained-found)
Expand Down Expand Up @@ -250,7 +250,7 @@ readonly extensions=${extensions:-jar}

(("${#args[@]}" == 0)) && usage 1 "Missing file pattern!"
(("${#args[@]}" > 1)) && usage 1 "More than 1 file pattern: ${args[*]}"
readonly pattern="${args[0]}"
readonly pattern=${args[0]}

declare -a tmp_dirs=()
for d in "${dirs[@]}"; do
Expand Down Expand Up @@ -311,12 +311,12 @@ __prepareCommandToListZipEntries() {
__prepareCommandToListZipEntries

listZipEntries() {
local zip_file="$1" msg
local zip_file=$1 msg

if $is_use_zip_cmd_to_list_zip_entries; then
# How to check if zip file is empty in bash
# https://superuser.com/questions/438878
msg="$("${command_to_list_zip_entries[@]}" -t "$zip_file" 2>&1)" || {
msg=$("${command_to_list_zip_entries[@]}" -t "$zip_file" 2>&1) || {
# NOTE:
# if list emtpy zip file by zipinfo/unzip command,
# exit code is 1, and print 'Empty zipfile.'
Expand Down Expand Up @@ -344,10 +344,10 @@ searchJarFiles() {

local jar_files total_jar_count

jar_files="$(find "${dirs[@]}" "${find_iname_options[@]}" -type f)"
jar_files=$(find "${dirs[@]}" "${find_iname_options[@]}" -type f)
[ -n "$jar_files" ] || die "No ${extensions[*]} file found!"

total_jar_count="$(printf '%s\n' "$jar_files" | wc -l)"
total_jar_count=$(printf '%s\n' "$jar_files" | wc -l)
# remove white space, because the `wc -l` output on mac contains white space!
total_jar_count=${total_jar_count//[[:space:]]}

Expand All @@ -356,7 +356,7 @@ searchJarFiles() {
}

__outputResultOfJarFile() {
local jar_file="$1" file jar_color='\e[1;35m' sep_color='\e[1;32m'
local jar_file=$1 file jar_color='\e[1;35m' sep_color='\e[1;32m'
# shellcheck disable=SC2206
local grep_opt_args=("$regex_mode" ${ignore_case_option:-} ${grep_color_option:-} -- "$pattern")

Expand Down
16 changes: 8 additions & 8 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 All @@ -22,7 +22,7 @@ readonly PROG_VERSION='2.6.0-dev'
################################################################################

colorPrint() {
local color="$1"
local color=$1
shift
# if stdout is a terminal, turn on color output.
# '-t' check: is a terminal?
Expand All @@ -44,9 +44,9 @@ die() {
}

portableRelPath() {
local file="$1" relTo="$2" uname
local file=$1 relTo=$2 uname

uname="$(uname)"
uname=$(uname)
case "$uname" in
Linux* | CYGWIN* | MINGW*)
realpath "$f" --relative-to="$relTo"
Expand All @@ -70,7 +70,7 @@ portableRelPath() {
}

usage() {
local -r exit_code="${1:-0}"
local -r exit_code=${1:-0}
(($# > 0)) && shift
local -r out=$(((exit_code != 0) + 1))

Expand Down Expand Up @@ -134,14 +134,14 @@ done
if [ "${#files[@]}" -eq 1 ]; then
relativeTo=.
else
argc="${#files[@]}"
argc=${#files[@]}

# Get last argument
relativeTo="${files[argc - 1]}"
relativeTo=${files[argc - 1]}
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
Loading

0 comments on commit a22100b

Please sign in to comment.