@@ -53,59 +53,66 @@ readonly ec=$'\033' # escape char
5353readonly eend=$' \033 [0m' # escape end
5454readonly nl=$' \n ' # new line
5555
56- colorEcho () {
56+ colorPrint () {
5757 local color=$1
5858 shift
5959
6060 # if stdout is console, turn on color output.
61- [ -t 1 ] && echo " ${ec} [1;${color} m$* $eend " || echo " $@ "
61+ if [ -t 1 ]; then
62+ printf " \033[1;${color} m%s\033[0m\n" " $* "
63+ else
64+ printf ' %s\n' " $* "
65+ fi
6266}
6367
64- colorPrint () {
68+ __appendToFile () {
69+ [[ -n " $append_file " && -w " $append_file " ]] && printf ' %s\n' " $* " >> " $append_file "
70+ [[ -n " $store_dir " && -w " $store_dir " ]] && printf ' %s\n' " $* " >> " ${store_file_prefix} $PROG "
71+ }
72+
73+ colorOutput () {
6574 local color=$1
6675 shift
6776
68- colorEcho " $color " " $@ "
69- [[ -n " $append_file " && -w " $append_file " ]] && echo " $@ " >> " $append_file "
70- [[ -n " $store_dir " && -w " $store_dir " ]] && echo " $@ " >> " ${store_file_prefix} $PROG "
77+ colorPrint " $color " " $* "
78+ __appendToFile " $* "
7179}
7280
7381# shellcheck disable=SC2120
74- normalPrint () {
75- echo " $@ "
76- [[ -n " $append_file " && -w " $append_file " ]] && echo " $@ " >> " $append_file "
77- [[ -n " $store_dir " && -w " $store_dir " ]] && echo " $@ " >> " ${store_file_prefix} $PROG "
82+ normalOutput () {
83+ printf ' %s\n' " $* "
84+ __appendToFile " $* "
7885}
7986
80- redPrint () {
81- colorPrint 31 " $@ "
87+ redOutput () {
88+ colorOutput 31 " $* "
8289}
8390
84- greenPrint () {
85- colorPrint 32 " $@ "
91+ greenOutput () {
92+ colorOutput 32 " $* "
8693}
8794
88- yellowPrint () {
89- colorPrint 33 " $@ "
95+ yellowOutput () {
96+ colorOutput 33 " $* "
9097}
9198
92- bluePrint () {
93- colorPrint 36 " $@ "
99+ blueOutput () {
100+ colorOutput 36 " $* "
94101}
95102
96103die () {
97- redPrint " Error: $* " 1>&2
104+ redOutput " Error: $* " 1>&2
98105 exit 1
99106}
100107
101108logAndRun () {
102- echo " $@ "
109+ printf ' %s\n ' " $* "
103110 echo
104111 " $@ "
105112}
106113
107114logAndCat () {
108- echo " $@ "
115+ printf ' %s\n ' " $* "
109116 echo
110117 cat
111118}
@@ -147,7 +154,7 @@ usage() {
147154 # shellcheck disable=SC2015
148155 [ " $exit_code " != 0 ] && local -r out=/dev/stderr || local -r out=/dev/stdout
149156
150- (( $# > 0 )) && colorEcho 31 " $* $nl " > $out
157+ (( $# > 0 )) && colorPrint 31 " $* $nl " > $out
151158
152159 cat > $out << EOF
153160Usage: ${PROG} [OPTION]... [delay [count]]
202209}
203210
204211progVersion () {
205- echo " $PROG $PROG_VERSION "
212+ printf ' %s\n ' " $PROG $PROG_VERSION "
206213 exit
207214}
208215
@@ -386,9 +393,9 @@ cleanupWhenExit() {
386393trap cleanupWhenExit EXIT
387394
388395headInfo () {
389- colorEcho " 0;34;42" ================================================================================
390- echo " $( date " +%Y-%m-%d %H:%M:%S.%N" ) [$(( update_round_num + 1 )) /$update_count ]: $( printCallingCommandLine) "
391- colorEcho " 0;34;42" ================================================================================
396+ colorPrint " 0;34;42" ================================================================================
397+ printf ' %s\n ' " $( date " +%Y-%m-%d %H:%M:%S.%N" ) [$(( update_round_num + 1 )) /$update_count ]: $( printCallingCommandLine) "
398+ colorPrint " 0;34;42" ================================================================================
392399 echo
393400}
394401
@@ -434,13 +441,13 @@ findBusyJavaThreadsByPs() {
434441 [ -n " $ps_out " ] || __die_when_no_java_process_found
435442
436443 if [ -n " $store_dir " ]; then
437- echo " $ps_out " | logAndCat " ${ps_cmd_line[*]} | sort -k3,3nr" > " ${store_file_prefix} $(( update_round_num + 1 )) _ps"
444+ printf ' %s\n ' " $ps_out " | logAndCat " ${ps_cmd_line[*]} | sort -k3,3nr" > " ${store_file_prefix} $(( update_round_num + 1 )) _ps"
438445 fi
439446
440447 if (( count > 0 )) ; then
441- echo " $ps_out " | head -n " ${count} "
448+ printf ' %s\n ' " $ps_out " | head -n " ${count} "
442449 else
443- echo " $ps_out "
450+ printf ' %s\n ' " $ps_out "
444451 fi
445452}
446453
@@ -473,13 +480,13 @@ __top_threadId_cpu() {
473480 local top_out
474481 top_out=$( HOME=" $tmp_store_dir " " ${top_cmd_line[@]} " )
475482 if [ -n " $store_dir " ]; then
476- echo " $top_out " | logAndCat " ${top_cmd_line[@]} " > " ${store_file_prefix} $(( update_round_num + 1 )) _top"
483+ printf ' %s\n ' " $top_out " | logAndCat " ${top_cmd_line[@]} " > " ${store_file_prefix} $(( update_round_num + 1 )) _top"
477484 fi
478485
479486 # DO NOT combine var result_threads_top_info declaration and assignment in ONE line!
480487 local result_threads_top_info
481488 result_threads_top_info=$(
482- echo " $top_out " | awk ' {
489+ printf ' %s\n ' " $top_out " | awk ' {
483490 # from text line to empty line, increase block index
484491 if (previousLine && !$0) blockIndex++
485492 # only print 4th text block(blockIndex == 3), aka. process info of second top update
@@ -490,7 +497,7 @@ __top_threadId_cpu() {
490497 )
491498 [ -n " $result_threads_top_info " ] || __die_when_no_java_process_found
492499
493- echo " $result_threads_top_info " | sort -k2,2nr
500+ printf ' %s\n ' " $result_threads_top_info " | sort -k2,2nr
494501}
495502
496503__complete_pid_user_by_ps () {
@@ -501,21 +508,21 @@ __complete_pid_user_by_ps() {
501508 local ps_out
502509 ps_out=" $( " ${ps_cmd_line[@]} " ) "
503510 if [ -n " $store_dir " ]; then
504- echo " $ps_out " | logAndCat " ${ps_cmd_line[@]} " > " ${store_file_prefix} $(( update_round_num + 1 )) _ps"
511+ printf ' %s\n ' " $ps_out " | logAndCat " ${ps_cmd_line[@]} " > " ${store_file_prefix} $(( update_round_num + 1 )) _ps"
505512 fi
506513
507514 local idx=0 threadId pcpu output_fields
508515 while read -r threadId pcpu; do
509516 (( count <= 0 || idx < count)) || break
510517
511518 # output field: pid, threadId, pcpu, user
512- output_fields=" $( echo " $ps_out " |
519+ output_fields=" $( printf ' %s\n ' " $ps_out " |
513520 awk -v " threadId=$threadId " -v " pcpu=$pcpu " ' $2==threadId {
514521 print $1, threadId, pcpu, $3; exit
515522 }' ) "
516523 if [ -n " $output_fields " ]; then
517524 (( idx++ ))
518- echo " $output_fields "
525+ printf ' %s\n ' " $output_fields "
519526 fi
520527 done
521528}
@@ -543,20 +550,20 @@ printStackOfThreads() {
543550 logAndRun sudo -u " ${user} " " ${jstack_cmd_line[@]} " > " ${jstackFile} "
544551 else
545552 # current user is not root user, so can not run with sudo; print error message and rerun suggestion
546- redPrint " [$idx ] Fail to jstack busy(${pcpu} %) thread(${threadId} /${threadId0x} ) stack of java process(${pid} ) under user(${user} )."
547- redPrint " User of java process($user ) is not current user($USER ), need sudo to rerun:"
548- yellowPrint " sudo $( printCallingCommandLine) "
549- normalPrint
553+ redOutput " [$idx ] Fail to jstack busy(${pcpu} %) thread(${threadId} /${threadId0x} ) stack of java process(${pid} ) under user(${user} )."
554+ redOutput " User of java process($user ) is not current user($USER ), need sudo to rerun:"
555+ yellowOutput " sudo $( printCallingCommandLine) "
556+ normalOutput
550557 continue
551558 fi || {
552- redPrint " [$idx ] Fail to jstack busy(${pcpu} %) thread(${threadId} /${threadId0x} ) stack of java process(${pid} ) under user(${user} )."
553- normalPrint
559+ redOutput " [$idx ] Fail to jstack busy(${pcpu} %) thread(${threadId} /${threadId0x} ) stack of java process(${pid} ) under user(${user} )."
560+ normalOutput
554561 rm " ${jstackFile} " & > /dev/null
555562 continue
556563 }
557564 }
558565
559- bluePrint " [$idx ] Busy(${pcpu} %) thread(${threadId} /${threadId0x} ) stack of java process(${pid} ) under user(${user} ):"
566+ blueOutput " [$idx ] Busy(${pcpu} %) thread(${threadId} /${threadId0x} ) stack of java process(${pid} ) under user(${user} ):"
560567
561568 if [ -n " $mix_native_frames " ]; then
562569 local sed_script=" /--------------- $threadId ---------------/,/^---------------/ {
0 commit comments