Skip to content

Commit 4ad6b5b

Browse files
committed
refactor: small code cleanup
- merge `colorPrint` function if only one caller - remove `local nl=$'\n'` declaration if only one usage - remove unnecessary `{}` when use var - use upper-case var name for global readonly vars - use `=` instead of `==` in `Conditional Expressions` - improve/fix/add code comments - remove section comments for simple section
1 parent ebb62cd commit 4ad6b5b

19 files changed

+89
-114
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<a href="https://github.com/oldratlee/useful-scripts"><img src="https://img.shields.io/github/repo-size/oldratlee/useful-scripts" alt="GitHub repo size"></a>
1212
</p>
1313

14-
🐌 useful scripts for making developer's everyday life easier and happier, involved java, shell etc.
14+
🐌 useful scripts for making developer's everyday life easier and happier, involved java, shell etc.
1515

1616
👉 平时有用的手动操作做成脚本,以便捷地使用,让开发的日常生活更轻松些。 💕
1717

@@ -143,7 +143,7 @@ PS:
143143

144144
- 目前仍然是主流的`Shell`,并且在不同环境基本上都缺省部署了。
145145
-[`Google``Shell`风格指南](https://zh-google-styleguide.readthedocs.io/en/latest/google-shell-styleguide/background/)中,明确说到了:`Bash`**唯一**被允许执行的`shell`脚本语言。
146-
- 统一用`Bash`可以避免差异带来的风险与没有收益的复杂性
146+
- 统一用`Bash`可以避免不同`Shell`之间差异所带来的风险与没有收益的复杂性
147147
- 有大量的`Shell`实现,`sh``bash``zsh``fish``csh``tcsh``ksh``ash``dash`……
148148
- 不同的`Shell`有各种差异,深坑勿入。
149149
- 个人系统学习过的是`Bash`,比较理解熟悉。
@@ -154,14 +154,14 @@ PS: 虽然交互`Shell`个人已经使用`Zsh` + [`oh-my-zsh`](https://ohmyz.sh/
154154

155155
> 更多资料参见 [子文档](docs/developer-guide.md)
156156
157-
- 开发规范与工具
158-
- [**_`Google Shell Style Guide`_**](https://google.github.io/styleguide/shell.xml) | [中文版](https://zh-google-styleguide.readthedocs.io/en/latest/google-shell-styleguide/background/)
157+
- 🛠️ 开发规范与工具
158+
- [`Google Shell Style Guide`](https://google.github.io/styleguide/shell.xml) | [中文版](https://zh-google-styleguide.readthedocs.io/en/latest/google-shell-styleguide/background/)
159159
- [`koalaman/shellcheck`](https://github.com/koalaman/shellcheck): `ShellCheck`, a static analysis tool for shell scripts
160160
- [`mvdan/sh(shfmt)`](https://github.com/mvdan/sh): `shfmt` formats shell programs
161161
- 👷 **`Bash/Shell`最佳实践与安全编程**文章
162162
- [Use the Unofficial Bash Strict Mode (Unless You Looove Debugging)](http://redsymbol.net/articles/unofficial-bash-strict-mode/)
163163
- Bash Pitfalls: 编程易犯的错误 - 团子的小窝:[Part 1](http://kodango.com/bash-pitfalls-part-1) | [Part 2](http://kodango.com/bash-pitfalls-part-2) | [Part 3](http://kodango.com/bash-pitfalls-part-3) | [Part 4](http://kodango.com/bash-pitfalls-part-4) | [英文原文:Bash Pitfalls](http://mywiki.wooledge.org/BashPitfalls)
164-
- [不要自己去指定sh的方式去执行脚本](https://github.com/oldratlee/useful-scripts/issues/57#issuecomment-326485965)
164+
- [不要自己去指定`sh`的方式去执行脚本](https://github.com/oldratlee/useful-scripts/issues/57#issuecomment-326485965)
165165
- 🎶 **Tips**
166166
- [让你提升命令行效率的 Bash 快捷键 【完整版】](https://linuxtoy.org/archives/bash-shortcuts.html)
167167
补充:`ctrl + x, ctrl + e` 就地打开文本编辑器来编辑当前命令行,对于复杂命令行特别有用

bin/a2l

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ readonly PROG=${0##*/}
1414
readonly PROG_VERSION='2.x-dev'
1515

1616
################################################################################
17-
# util functions
17+
# parse options
1818
################################################################################
1919

2020
usage() {
@@ -39,10 +39,6 @@ progVersion() {
3939
exit
4040
}
4141

42-
################################################################################
43-
# parse options
44-
################################################################################
45-
4642
args=()
4743
while (($# > 0)); do
4844
case "$1" in
@@ -63,13 +59,12 @@ while (($# > 0)); do
6359
break
6460
;;
6561
*)
66-
# if not option, treat all follow arguments as args
62+
# if not option, treat it and all follow arguments as args
6763
args=(${args[@]:+"${args[@]}"} "$@")
6864
break
6965
;;
7066
esac
7167
done
72-
7368
readonly args
7469

7570
################################################################################

bin/ap

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,17 @@ readonly PROG_VERSION='2.x-dev'
1919
# util functions
2020
################################################################################
2121

22-
colorPrint() {
23-
local color=$1
24-
shift
22+
redPrint() {
2523
# if stdout is a terminal, turn on color output.
2624
# '-t' check: is a terminal?
2725
# check isatty in bash https://stackoverflow.com/questions/10022323
2826
if [ -t 1 ]; then
29-
printf '\e[1;%sm%s\e[0m\n' "$color" "$*"
27+
printf '\e[1;31m%s\e[0m\n' "$*"
3028
else
3129
printf '%s\n' "$*"
3230
fi
3331
}
3432

35-
redPrint() {
36-
colorPrint 31 "$*"
37-
}
38-
3933
die() {
4034
redPrint "Error: $*" >&2
4135
exit 1
@@ -75,8 +69,7 @@ usage() {
7569
local -r out=$(((exit_code != 0) + 1))
7670

7771
# NOTE: $'foo' is the escape sequence syntax of bash
78-
local nl=$'\n' # new line
79-
(($# > 0)) && redPrint "$*$nl" >&"$out"
72+
(($# > 0)) && redPrint "$*"$'\n' >&"$out"
8073

8174
cat >&"$out" <<EOF
8275
Usage: $PROG [OPTION]... [FILE]...

bin/coat

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ rotateColorPrint() {
7272
}
7373

7474
rotateColorPrintln() {
75+
# NOTE: $'foo' is the escape sequence syntax of bash
7576
rotateColorPrint "$*"$'\n'
7677
}
7778

bin/cp-into-docker-run

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,7 @@ usage() {
7070
local -r out=$(((exit_code != 0) + 1))
7171

7272
# NOTE: $'foo' is the escape sequence syntax of bash
73-
local nl=$'\n' # new line
74-
(($# > 0)) && redPrint "$*$nl" >&"$out"
73+
(($# > 0)) && redPrint "$*"$'\n' >&"$out"
7574

7675
cat >&"$out" <<EOF
7776
Usage: $PROG [OPTION]... command [command-args]...

bin/find-in-jars

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,7 @@ usage() {
7979
local -r out=$(((exit_code != 0) + 1))
8080

8181
# NOTE: $'foo' is the escape sequence syntax of bash
82-
local -r nl=$'\n' # new line
83-
(($# > 0)) && redPrint "$*$nl" >&"$out"
82+
(($# > 0)) && redPrint "$*"$'\n' >&"$out"
8483

8584
cat >&"$out" <<EOF
8685
Usage: $PROG [OPTION]... PATTERN

bin/rp

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,17 @@ readonly PROG_VERSION='2.x-dev'
1919
# util functions
2020
################################################################################
2121

22-
colorPrint() {
23-
local color=$1
24-
shift
22+
redPrint() {
2523
# if stdout is a terminal, turn on color output.
2624
# '-t' check: is a terminal?
2725
# check isatty in bash https://stackoverflow.com/questions/10022323
2826
if [ -t 1 ]; then
29-
printf '\e[1;%sm%s\e[0m\n' "$color" "$*"
27+
printf '\e[1;31m%s\e[0m\n' "$*"
3028
else
3129
printf '%s\n' "$*"
3230
fi
3331
}
3432

35-
redPrint() {
36-
colorPrint 31 "$@"
37-
}
38-
3933
die() {
4034
redPrint "Error: $*" >&2
4135
exit 1
@@ -73,17 +67,16 @@ usage() {
7367
local -r out=$(((exit_code != 0) + 1))
7468

7569
# NOTE: $'foo' is the escape sequence syntax of bash
76-
local nl=$'\n' # new line
77-
(($# > 0)) && redPrint "$*$nl" >&"$out"
70+
(($# > 0)) && redPrint "$*"$'\n' >&"$out"
7871

7972
cat >&"$out" <<EOF
80-
Usage: ${PROG} [OPTION]... [FILE]...
73+
Usage: $PROG [OPTION]... [FILE]...
8174
convert to Relative Path.
8275
8376
Example:
84-
${PROG} path # relative to current dir
85-
${PROG} path1 relativeToPath
86-
${PROG} */*.c relativeToPath
77+
$PROG path # relative to current dir
78+
$PROG path1 relativeToPath
79+
$PROG */*.c relativeToPath
8780
8881
Options:
8982
-h, --help display this help and exit

bin/show-busy-java-threads

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -139,14 +139,14 @@ usage() {
139139
(($# > 0)) && colorPrint 31 "$*$NL" >&"$out"
140140

141141
cat >&"$out" <<EOF
142-
Usage: ${PROG} [OPTION]... [delay [count]]
142+
Usage: $PROG [OPTION]... [delay [count]]
143143
Find out the highest cpu consumed threads of java processes,
144144
and print the stack of these threads.
145145
146146
Example:
147-
${PROG} # show busy java threads info
148-
${PROG} 1 # update every 1 second, (stop by eg: CTRL+C)
149-
${PROG} 3 10 # update every 3 seconds, update 10 times
147+
$PROG # show busy java threads info
148+
$PROG 1 # update every 1 second, (stop by eg: CTRL+C)
149+
$PROG 3 10 # update every 3 seconds, update 10 times
150150
151151
Output control:
152152
-p, --pid <java pid(s)> find out the highest cpu consumed threads from
@@ -215,7 +215,7 @@ ARGS=$(
215215
usage 1
216216
}
217217
readonly ARGS
218-
eval set -- "${ARGS}"
218+
eval set -- "$ARGS"
219219

220220
count=5
221221
cpu_sample_interval=0.5
@@ -525,20 +525,20 @@ printStackOfThreads() {
525525
[ -f "$jstackFile" ] || {
526526
# shellcheck disable=SC2206
527527
local -a jstack_cmd_line=("$jstack_path" $force $mix_native_frames $lock_info $pid)
528-
if [ "$user" == "$USER" ]; then
528+
if [ "$user" = "$USER" ]; then
529529
# run without sudo, when java process user is current user
530530
logAndRun "${jstack_cmd_line[@]}" >"$jstackFile"
531-
elif [ $UID == 0 ]; then
531+
elif ((UID == 0)); then
532532
# if java process user is not current user, must run jstack with sudo
533533
logAndRun sudo -u "$user" "${jstack_cmd_line[@]}" >"$jstackFile"
534534
else
535535
# current user is not root user, so can not run with sudo; print error message and rerun suggestion
536-
redOutput "[$idx] Fail to jstack busy(${pcpu}%) thread($threadId/$threadId0x) stack of java process($pid) under user($user)."
536+
redOutput "[$idx] Fail to jstack busy($pcpu%) thread($threadId/$threadId0x) stack of java process($pid) under user($user)."
537537
redOutput "User of java process($user) is not current user($USER), need sudo to rerun:"
538538
yellowOutput " sudo $(printCallingCommandLine)"
539539
continue
540540
fi || {
541-
redOutput "[$idx] Fail to jstack busy(${pcpu}%) thread(${threadId}/${threadId0x}) stack of java process(${pid}) under user(${user})."
541+
redOutput "[$idx] Fail to jstack busy($pcpu%) thread($threadId/$threadId0x) stack of java process($pid) under user($user)."
542542
rm "$jstackFile" &>/dev/null
543543
continue
544544
}
@@ -582,7 +582,7 @@ main() {
582582
tee ${append_file:+-a "$append_file"} ${store_dir:+-a "$store_file_prefix$PROG"} >/dev/null
583583
((update_count != 1)) && headInfo
584584

585-
if [ "$cpu_sample_interval" == 0 ]; then
585+
if [ "$cpu_sample_interval" = 0 ]; then
586586
findBusyJavaThreadsByPs
587587
else
588588
findBusyJavaThreadsByTop

bin/show-duplicate-java-classes

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ from glob import glob
2424
from io import BytesIO
2525
from optparse import OptionParser
2626
from os import walk
27-
from os.path import relpath, isdir, exists
28-
from zipfile import ZipFile, BadZipfile
27+
from os.path import exists, isdir, relpath
28+
from zipfile import BadZipfile, ZipFile
2929

3030
################################################################################
3131
# utils functions

bin/taoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ rotateColorPrint() {
7272
}
7373

7474
rotateColorPrintln() {
75+
# NOTE: $'foo' is the escape sequence syntax of bash
7576
rotateColorPrint "$*"$'\n'
7677
}
7778

0 commit comments

Comments
 (0)