Skip to content

Commit 7e03b4f

Browse files
committed
refactor: simplify and uniform the codes
- remove declare for global vars, more consistent - use upper-case var name for global readonly vars - unset temp global vars after use
1 parent d3224b5 commit 7e03b4f

File tree

13 files changed

+54
-49
lines changed

13 files changed

+54
-49
lines changed

bin/a2l

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ progVersion() {
4343
# parse options
4444
################################################################################
4545

46-
declare -a args=()
46+
args=()
4747
while (($# > 0)); do
4848
case "$1" in
4949
-h | --help)

bin/ap

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ progVersion() {
103103
# parse options
104104
################################################################################
105105

106-
declare -a files=()
106+
files=()
107107
while (($# > 0)); do
108108
case "$1" in
109109
-h | --help)
@@ -129,8 +129,7 @@ while (($# > 0)); do
129129
done
130130

131131
# if files is empty, use "."
132-
files=("${files[@]:-.}")
133-
readonly files
132+
readonly files=("${files[@]:-.}")
134133

135134
################################################################################
136135
# biz logic

bin/c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ progVersion() {
8383

8484
quiet=false
8585
keep_eol=false
86-
declare -a target_command=()
86+
target_command=()
8787
while (($# > 0)); do
8888
case "$1" in
8989
-k | --keep-eol)

bin/coat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ EOF
4040
exit
4141
}
4242

43-
declare -a args=("$@")
43+
args=("$@")
4444
# check arguments in reverse, so last option wins.
4545
for ((idx = $# - 1; idx >= 0; --idx)); do
4646
[ "${args[idx]}" = --help ] && usage

bin/cp-into-docker-run

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ docker_workdir=
120120
docker_tmpdir=/tmp
121121
docker_command_cp_path=
122122
verbose=false
123-
declare -a args=()
123+
args=()
124124

125125
while (($# > 0)); do
126126
case "$1" in

bin/echo-args

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,15 @@ digitCount() {
2020
digit_count=$(digitCount $#)
2121
readonly arg_count=$# digit_count
2222

23-
readonly red='\e[1;31m' blue='\e[1;36m' color_reset='\e[0m'
24-
23+
readonly RED='\e[1;31m' BLUE='\e[1;36m' COLOR_RESET='\e[0m'
2524
printArg() {
2625
local idx=$1 value=$2
2726

2827
# if stdout is a terminal, turn on color output.
2928
# '-t' check: is a terminal?
3029
# check isatty in bash https://stackoverflow.com/questions/10022323
3130
if [ -t 1 ]; then
32-
printf "%${digit_count}s/%s: ${red}[${blue}%s${red}]${color_reset}\n" "$idx" "$arg_count" "$value"
31+
printf "%${digit_count}s/%s: ${RED}[${BLUE}%s${RED}]${COLOR_RESET}\n" "$idx" "$arg_count" "$value"
3332
else
3433
printf "%${digit_count}s/%s: [%s]\n" "$idx" "$arg_count" "$value"
3534
fi

bin/find-in-jars

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -38,34 +38,34 @@ readonly PROG_VERSION='2.x-dev'
3838
# util functions
3939
################################################################################
4040

41-
readonly color_reset='\e[0m'
42-
43-
# How to delete line with echo?
44-
# https://unix.stackexchange.com/questions/26576
45-
#
46-
# terminal escapes: http://ascii-table.com/ansi-escape-sequences.php
47-
# In particular, to clear from the cursor position to the beginning of the line:
48-
# echo -e "\033[1K"
49-
# Or everything on the line, regardless of cursor position:
50-
# echo -e "\033[2K"
51-
readonly clear_line='\e[2K\r'
41+
readonly COLOR_RESET='\e[0m'
5242

5343
redPrint() {
5444
# if stdout is a terminal, turn on color output.
5545
# '-t' check: is a terminal?
5646
# check isatty in bash https://stackoverflow.com/questions/10022323
5747
if [ -t 1 ]; then
58-
printf "\e[1;31m%s$color_reset\n" "$*"
48+
printf "\e[1;31m%s$COLOR_RESET\n" "$*"
5949
else
6050
printf '%s\n' "$*"
6151
fi
6252
}
6353

54+
# How to delete line with echo?
55+
# https://unix.stackexchange.com/questions/26576
56+
#
57+
# terminal escapes: http://ascii-table.com/ansi-escape-sequences.php
58+
# In particular, to clear from the cursor position to the beginning of the line:
59+
# echo -e "\033[1K"
60+
# Or everything on the line, regardless of cursor position:
61+
# echo -e "\033[2K"
62+
readonly LINE_CLEAR='\e[2K\r'
63+
6464
# Getting console width using a bash script
6565
# https://unix.stackexchange.com/questions/299067
6666
#
6767
# NOTE: DO NOT declare columns var as readonly in ONE line!
68-
[ -t 2 ] && columns=$(stty size | awk '{print $2}')
68+
[ -t 2 ] && COLUMNS=$(stty size | awk '{print $2}')
6969

7070
printResponsiveMessage() {
7171
if ! $show_responsive || [ ! -t 2 ]; then
@@ -74,15 +74,15 @@ printResponsiveMessage() {
7474

7575
local content=$*
7676
# http://www.linuxforums.org/forum/red-hat-fedora-linux/142825-how-truncate-string-bash-script.html
77-
printf %b%s "$clear_line" "${content:0:columns}" >&2
77+
printf %b%s "$LINE_CLEAR" "${content:0:COLUMNS}" >&2
7878
}
7979

8080
clearResponsiveMessage() {
8181
if ! $show_responsive || [ ! -t 2 ]; then
8282
return
8383
fi
8484

85-
printf %b "$clear_line" >&2
85+
printf %b "$LINE_CLEAR" >&2
8686
}
8787

8888
die() {
@@ -155,9 +155,9 @@ progVersion() {
155155
# parse options
156156
################################################################################
157157

158-
declare -a dirs=()
159-
declare -a extensions=()
160-
declare -a args=()
158+
dirs=()
159+
extensions=()
160+
args=()
161161

162162
separator='!'
163163
regex_mode=-E
@@ -250,7 +250,7 @@ readonly extensions=${extensions:-jar}
250250
((${#args[@]} > 1)) && usage 1 "More than 1 file pattern: ${args[*]}"
251251
readonly pattern=${args[0]}
252252

253-
declare -a tmp_dirs=()
253+
tmp_dirs=()
254254
for d in "${dirs[@]}"; do
255255
[ -e "$d" ] || die "file $d(specified by option -d) does not exist!"
256256
[ -d "$d" ] || die "file $d(specified by option -d) exists but is not a directory!"
@@ -262,13 +262,15 @@ done
262262
# set dirs to Absolute Path
263263
$use_absolute_path && dirs=("${tmp_dirs[@]}")
264264
readonly dirs
265+
unset d tmp_dirs
265266

266267
# convert extensions to find -iname options
267268
find_iname_options=()
268269
for e in "${extensions[@]}"; do
269270
find_iname_options=(${find_iname_options[@]:+"${find_iname_options[@]}" -o} -iname "*.$e")
270271
done
271272
readonly find_iname_options
273+
unset e
272274

273275
################################################################################
274276
# Check the existence of command for listing zip entry!
@@ -353,8 +355,9 @@ searchJarFiles() {
353355
printf '%s\n' "$jar_files"
354356
}
355357

358+
readonly JAR_COLOR='\e[1;35m' SEP_COLOR='\e[1;32m'
356359
__outputResultOfJarFile() {
357-
local jar_file=$1 file jar_color='\e[1;35m' sep_color='\e[1;32m'
360+
local jar_file=$1 file
358361
# shellcheck disable=SC2206
359362
local grep_opt_args=("$regex_mode" ${ignore_case_option:-} ${grep_color_option:-} -- "$pattern")
360363

@@ -374,7 +377,7 @@ __outputResultOfJarFile() {
374377

375378
clearResponsiveMessage
376379
if [ -t 1 ]; then
377-
printf "$jar_color%s$color_reset\n" "$jar_file"
380+
printf "$JAR_COLOR%s$COLOR_RESET\n" "$jar_file"
378381
else
379382
printf '%s\n' "$jar_file"
380383
fi
@@ -386,7 +389,7 @@ __outputResultOfJarFile() {
386389
} | while read -r file; do
387390
clearResponsiveMessage
388391
if [ -t 1 ]; then
389-
printf "$jar_color%s$sep_color%s$color_reset%s\n" "$jar_file" "$separator" "$file"
392+
printf "$JAR_COLOR%s$SEP_COLOR%s$COLOR_RESET%s\n" "$jar_file" "$separator" "$file"
390393
else
391394
printf '%s\n' "$jar_file$separator$file"
392395
fi

bin/rp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ progVersion() {
102102
# parse options
103103
################################################################################
104104

105-
declare -a files=()
105+
files=()
106106
while (($# > 0)); do
107107
case "$1" in
108108
-h | --help)

bin/show-busy-java-threads

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ readonly USER
4747
################################################################################
4848

4949
# NOTE: $'foo' is the escape sequence syntax of bash
50-
readonly nl=$'\n' # new line
50+
readonly NL=$'\n' # new line
5151

5252
colorPrint() {
5353
local color=$1
@@ -151,7 +151,7 @@ usage() {
151151
(($# > 0)) && shift
152152
local -r out=$(((exit_code != 0) + 1))
153153

154-
(($# > 0)) && colorPrint 31 "$*$nl" >&"$out"
154+
(($# > 0)) && colorPrint 31 "$*$NL" >&"$out"
155155

156156
cat >&"$out" <<EOF
157157
Usage: ${PROG} [OPTION]... [delay [count]]
@@ -352,18 +352,18 @@ if [ -n "$jstack_path" ]; then
352352
elif [ -n "$JAVA_HOME" ]; then
353353
# 2. search jstack under JAVA_HOME
354354
if [ -f "$JAVA_HOME/bin/jstack" ]; then
355-
[ -x "$JAVA_HOME/bin/jstack" ] || die "found \$JAVA_HOME/bin/jstack($JAVA_HOME/bin/jstack) is NOT executable!${nl}Use -s option set jstack path manually."
355+
[ -x "$JAVA_HOME/bin/jstack" ] || die "found \$JAVA_HOME/bin/jstack($JAVA_HOME/bin/jstack) is NOT executable!${NL}Use -s option set jstack path manually."
356356
jstack_path="$JAVA_HOME/bin/jstack"
357357
elif [ -f "$JAVA_HOME/../bin/jstack" ]; then
358-
[ -x "$JAVA_HOME/../bin/jstack" ] || die "found \$JAVA_HOME/../bin/jstack($JAVA_HOME/../bin/jstack) is NOT executable!${nl}Use -s option set jstack path manually."
358+
[ -x "$JAVA_HOME/../bin/jstack" ] || die "found \$JAVA_HOME/../bin/jstack($JAVA_HOME/../bin/jstack) is NOT executable!${NL}Use -s option set jstack path manually."
359359
jstack_path="$JAVA_HOME/../bin/jstack"
360360
fi
361361
elif command -v jstack &>/dev/null; then
362362
# 3. search jstack under PATH
363363
jstack_path=$(command -v jstack)
364-
[ -x "$jstack_path" ] || die "found $jstack_path from PATH is NOT executable!${nl}Use -s option set jstack path manually."
364+
[ -x "$jstack_path" ] || die "found $jstack_path from PATH is NOT executable!${NL}Use -s option set jstack path manually."
365365
else
366-
die "jstack NOT found by JAVA_HOME(${JAVA_HOME:-not set}) setting and PATH!${nl}Use -s option set jstack path manually."
366+
die "jstack NOT found by JAVA_HOME(${JAVA_HOME:-not set}) setting and PATH!${NL}Use -s option set jstack path manually."
367367
fi
368368
readonly jstack_path
369369

bin/taoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ EOF
4040
exit
4141
}
4242

43-
declare -a args=("$@")
43+
args=("$@")
4444
# check arguments in reverse, so last option wins.
4545
for ((idx = $# - 1; idx >= 0; --idx)); do
4646
[ "${args[idx]}" = --help ] && usage

0 commit comments

Comments
 (0)