From d48801c676283593600751d67ef85b332c8f5ce7 Mon Sep 17 00:00:00 2001 From: Jerry Lee Date: Thu, 25 Jan 2024 13:38:12 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20improve=20comments;=20use=20`||/&&`?= =?UTF-8?q?=20for=20one=20branch=20`if`=20=F0=9F=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 ++-- bin/a2l | 4 ++-- bin/c | 2 +- bin/coat | 4 +--- bin/cp-into-docker-run | 2 +- bin/find-in-jars | 10 +++------- bin/show-busy-java-threads | 2 +- bin/taoc | 4 +--- 8 files changed, 12 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 535b6391..3f32367e 100644 --- a/README.md +++ b/README.md @@ -142,10 +142,10 @@ PS: `Shell`用`Bash`的原因是: - 目前仍然是主流的`Shell`,并且在不同环境基本上都缺省部署了。 -- 在[`Google`的`Shell`风格指南](https://zh-google-styleguide.readthedocs.io/en/latest/google-shell-styleguide/background/)中,明确说明了:`Bash`是**唯一**被允许执行的`shell`脚本语言。 +- 在[`Google`的`Shell`风格指南](https://zh-google-styleguide.readthedocs.io/en/latest/google-shell-styleguide/background/)中,明确说到了:`Bash`是**唯一**被允许执行的`shell`脚本语言。 +- 统一用`Bash`,可以避免差异带来的风险与没有收益的复杂性。 - 有大量的`Shell`实现,`sh`、`bash`、`zsh`、`fish`、`csh`、`tcsh`、`ksh`、`ash`、`dash`…… - 不同的`Shell`有各种差异,深坑勿入。 - - 统一用`Bash`,可以避免差异带来的风险与没有收益的复杂性。 - 个人系统学习过的是`Bash`,比较理解熟悉。 PS: 虽然交互`Shell`个人已经使用`Zsh` + [`oh-my-zsh`](https://ohmyz.sh/),但在严谨的`Shell`脚本开发时还是使用`Bash`。 diff --git a/bin/a2l b/bin/a2l index 01612197..81cfcc21 100755 --- a/bin/a2l +++ b/bin/a2l @@ -60,12 +60,12 @@ while [ $# -gt 0 ]; do break ;; -*) - # if unrecognized option, treat it and all follow args as args + # if unrecognized option, treat it and all follow arguments as args args=(${args[@]:+"${args[@]}"} "$@") break ;; *) - # if not option, treat all follow args as args + # if not option, treat all follow arguments as args args=(${args[@]:+"${args[@]}"} "$@") break ;; diff --git a/bin/c b/bin/c index d844196a..3aba6a03 100755 --- a/bin/c +++ b/bin/c @@ -111,7 +111,7 @@ while [ $# -gt 0 ]; do usage 2 "unrecognized option '$1'" ;; *) - # if not option, treat all follow args as command + # if not option, treat all follow arguments as command target_command=(${target_command[@]:+"${target_command[@]}"} "$@") break ;; diff --git a/bin/coat b/bin/coat index 9eff9922..d98d50e0 100755 --- a/bin/coat +++ b/bin/coat @@ -44,9 +44,7 @@ colorLines() { # How to use `while read` (Bash) to read the last line in a file # if there’s no newline at the end of the file? # https://stackoverflow.com/questions/4165135 - if [ -n "$line" ]; then - rotateColorPrint "$line" - fi + [ -z "$line" ] || rotateColorPrint "$line" } if [ $# == 0 ]; then diff --git a/bin/cp-into-docker-run b/bin/cp-into-docker-run index d4841e67..c5a0d4e2 100755 --- a/bin/cp-into-docker-run +++ b/bin/cp-into-docker-run @@ -164,7 +164,7 @@ while (($# > 0)); do usage 2 "$PROG: unrecognized option '$1'" ;; *) - # if not option, treat all follow args as command + # if not option, treat all follow arguments as command args=(${args[@]:+"${args[@]}"} "$@") break ;; diff --git a/bin/find-in-jars b/bin/find-in-jars index fc5e198c..05d23aec 100755 --- a/bin/find-in-jars +++ b/bin/find-in-jars @@ -349,7 +349,7 @@ searchJarFiles() { 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:]]} + total_jar_count=${total_jar_count//[[:space:]]/} echo "$total_jar_count" printf '%s\n' "$jar_files" @@ -370,13 +370,9 @@ __outputResultOfJarFile() { # - https://stackoverflow.com/questions/19120263/why-exit-code-141-with-grep-q # - https://unix.stackexchange.com/questions/305547/broken-pipe-when-grepping-output-but-only-with-i-flag # - http://www.pixelbeat.org/programming/sigpipe_handling.html - if grep -c "${grep_opt_args[@]}" &>/dev/null; then - matched=true - fi + grep -c "${grep_opt_args[@]}" &>/dev/null && matched=true - if [ "$print_matched_files" != "$matched" ]; then - return - fi + [ "$print_matched_files" != "$matched" ] && return clearResponsiveMessage if [ -t 1 ]; then diff --git a/bin/show-busy-java-threads b/bin/show-busy-java-threads index da419b90..27f056ea 100755 --- a/bin/show-busy-java-threads +++ b/bin/show-busy-java-threads @@ -303,7 +303,7 @@ isNaturalNumber "$update_count" || die "update count($update_count) is not a nat readonly update_count if [ -n "$pid_list" ]; then - pid_list=${pid_list//[[:space:]]} # delete white space + pid_list=${pid_list//[[:space:]]/} # delete white space isNaturalNumberList "$pid_list" || die "pid(s)($pid_list) is illegal! example: 42 or 42,99,67" fi readonly pid_list diff --git a/bin/taoc b/bin/taoc index 7a0ebe23..adf7e663 100755 --- a/bin/taoc +++ b/bin/taoc @@ -44,9 +44,7 @@ colorLines() { # How to use `while read` (Bash) to read the last line in a file # if there’s no newline at the end of the file? # https://stackoverflow.com/questions/4165135 - if [ -n "$line" ]; then - rotateColorPrint "$line" - fi + [ -z "$line" ] || rotateColorPrint "$line" } tac "$@" | colorLines