88# @online-doc https://github.com/oldratlee/useful-scripts/blob/dev-2.x/docs/java.md#-show-busy-java-threads
99# @author Jerry Lee (oldratlee at gmail dot com)
1010# @author superhj1987 (superhj1987 at 126 dot com)
11- #
12- # NOTE about Bash Traps and Pitfalls:
13- #
14- # 1. DO NOT combine var declaration and assignment which value supplied by subshell in ONE line!
15- # for example: readonly var1=$(echo value1)
16- # local var2=$(echo value1)
17- #
18- # Because the combination make exit code of assignment to be always 0,
19- # aka. the exit code of command in subshell is discarded.
20- # tested on bash 3.2.57/4.2.46
21- #
22- # solution is separation of var declaration and assignment:
23- # var1=$(echo value1)
24- # readonly var1
25- # local var2
26- # var2=$(echo value1)
2711
2812readonly PROG=${0##*/ }
2913readonly PROG_VERSION=' 2.x-dev'
@@ -38,7 +22,8 @@ readonly -a COMMAND_LINE=("${BASH_SOURCE[0]}" "$@")
3822# See https://www.lifewire.com/current-linux-user-whoami-command-3867579
3923# Because if run command by `sudo -u`, env var $USER is not rewritten/correct, just inherited from outside!
4024#
41- # NOTE: DO NOT declare var USER as readonly in ONE line!
25+ # DO NOT declare and assign var USER(as readonly) in ONE line!
26+ # more info see https://github.com/koalaman/shellcheck/wiki/SC2155
4227USER=$( whoami)
4328readonly USER
4429
@@ -220,7 +205,7 @@ uname | grep '^Linux' -q || die "$PROG only support Linux, not support $(uname)
220205# parse options
221206# ###############################################################################
222207
223- # DO NOT declare var ARGS as readonly in ONE line!
208+ # DO NOT declare and assign var ARGS( as readonly) in ONE line!
224209ARGS=$(
225210 getopt -n " $PROG " -a -o c:p:a:s:S:i:Pd:FmlhV \
226211 -l count:,pid:,append-file:,jstack-path:,store-dir:,cpu-sample-interval:,use-ps,top-delay:,force,mix-native-frames,lock-info,help,version \
@@ -371,7 +356,7 @@ readonly jstack_path
371356# biz logic
372357# ###############################################################################
373358
374- # NOTE: DO NOT declare var run_timestamp as readonly in ONE line!
359+ # DO NOT declare and assign var run_timestamp( as readonly) in ONE line!
375360run_timestamp=$( date " +%Y-%m-%d_%H:%M:%S.%N" )
376361readonly run_timestamp
377362readonly uuid=" ${PROG} _${run_timestamp} _${$} _${RANDOM} "
@@ -433,6 +418,7 @@ findBusyJavaThreadsByPs() {
433418 # shellcheck disable=SC2206
434419 local -a ps_cmd_line=(ps $ps_process_select_options -wwLo ' pid,lwp,pcpu,user' --no-headers)
435420 # DO NOT combine var ps_out declaration and assignment in ONE line!
421+ # more info see https://github.com/koalaman/shellcheck/wiki/SC2155
436422 local ps_out
437423 ps_out=$( " ${ps_cmd_line[@]} " | sort -k3,3nr)
438424 [ -n " $ps_out " ] || __die_when_no_java_process_found
@@ -473,7 +459,7 @@ __top_threadId_cpu() {
473459 # 4. top v3.3, there is 1 black line between 2 update;
474460 # but top v3.2, there is 2 blank lines between 2 update!
475461 local -a top_cmd_line=(top -H -b -d " $cpu_sample_interval " -n 2 -p " $java_pid_list " )
476- # DO NOT combine var ps_out declaration and assignment in ONE line!
462+ # DO NOT combine var top_out declaration and assignment in ONE line!
477463 local top_out
478464 top_out=$( HOME=$tmp_store_dir " ${top_cmd_line[@]} " )
479465 if [ -n " $store_dir " ]; then
0 commit comments