Skip to content

Commit c2b8b2b

Browse files
committed
use `satisfy condition or die' pattern
this pattern from perl: satisfy xxx or die Yes, `or` is the boolean operator of perl In shell, `or` is `||` if you get familiar with this pattern, it's very readable!
1 parent 1d1257c commit c2b8b2b

File tree

2 files changed

+72
-74
lines changed

2 files changed

+72
-74
lines changed

find-in-jars

Lines changed: 55 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,52 @@
1313

1414
readonly PROG="`basename "$0"`"
1515

16+
################################################################################
17+
# util functions
18+
################################################################################
19+
20+
[ -t 1 ] && readonly is_console=true || readonly is_console=false
21+
22+
# NOTE: $'foo' is the escape sequence syntax of bash
23+
readonly ec=$'\033' # escape char
24+
readonly eend=$'\033[0m' # escape end
25+
readonly cr=$'\r' # carriage return
26+
27+
redEcho() {
28+
$is_console && echo "$ec[1;31m$@$eend" || echo "$@"
29+
}
30+
31+
die() {
32+
redEcho "Error: $@" 1>&2
33+
exit 1
34+
}
35+
36+
# Getting console width using a bash script
37+
# https://unix.stackexchange.com/questions/299067
38+
$is_console && readonly columns=$(stty size | awk '{print $2}')
39+
40+
printResponsiveMessage() {
41+
$is_console || return
42+
43+
local message="$*"
44+
# http://www.linuxforums.org/forum/red-hat-fedora-linux/142825-how-truncate-string-bash-script.html
45+
echo -n "${message:0:columns}"
46+
}
47+
48+
clearResponsiveMessage() {
49+
$is_console || return
50+
51+
# How to delete line with echo?
52+
# https://unix.stackexchange.com/questions/26576
53+
#
54+
# terminal escapes: http://ascii-table.com/ansi-escape-sequences.php
55+
# In particular, to clear from the cursor position to the beginning of the line:
56+
# echo -e "\033[1K"
57+
# Or everything on the line, regardless of cursor position:
58+
# echo -e "\033[2K"
59+
echo -n "$ec[2K$cr"
60+
}
61+
1662
usage() {
1763
local -r exit_code="$1"
1864
shift
@@ -61,8 +107,6 @@ EOF
61107

62108
declare -a args=()
63109
declare -a dirs=()
64-
regex_mode=-E
65-
use_absolute_path=false
66110
while [ $# -gt 0 ]; do
67111
case "$1" in
68112
-d|--dir)
@@ -118,8 +162,12 @@ while [ $# -gt 0 ]; do
118162
;;
119163
esac
120164
done
165+
121166
dirs=${dirs:-.}
122167
extension=${extension:-jar}
168+
regex_mode=${regex_mode:--E}
169+
170+
use_absolute_path=${use_absolute_path:-false}
123171
seperator="${seperator:-!}"
124172

125173
[ "${#args[@]}" -eq 0 ] && usage 1 "No find file pattern!"
@@ -144,7 +192,6 @@ for e in "${extension[@]}"; do
144192
find_iname_options=( "${find_iname_options[@]}" -o -iname "*.$e" )
145193
done
146194

147-
148195
################################################################################
149196
# Check the existence of command for listing zip entry!
150197
################################################################################
@@ -159,70 +206,21 @@ elif which unzip &> /dev/null; then
159206
readonly command_for_list_zip='unzip -Z1'
160207
else
161208
if ! which jar &> /dev/null; then
162-
[ -z "$JAVA_HOME" ] && {
163-
echo "Error: jar not found on PATH!" 1>&2
164-
exit 1
165-
}
166-
! [ -f "$JAVA_HOME/bin/jar" ] && {
167-
echo "Error: jar not found on PATH and \$JAVA_HOME/bin/jar($JAVA_HOME/bin/jar) file does NOT exists!" 1>&2
168-
exit 1
169-
}
170-
! [ -x "$JAVA_HOME/bin/jar" ] && {
171-
echo "Error: jar not found on PATH and \$JAVA_HOME/bin/jar($JAVA_HOME/bin/jar) is NOT executalbe!" 1>&2
172-
exit 1
173-
}
209+
[ -n "$JAVA_HOME" ] || die "jar not found on PATH and JAVA_HOME env var is blank!"
210+
[ -f "$JAVA_HOME/bin/jar" ] || die "jar not found on PATH and \$JAVA_HOME/bin/jar($JAVA_HOME/bin/jar) file does NOT exists!"
211+
[ -x "$JAVA_HOME/bin/jar" ] || die "jar not found on PATH and \$JAVA_HOME/bin/jar($JAVA_HOME/bin/jar) is NOT executalbe!"
174212
export PATH="$JAVA_HOME/bin:$PATH"
175213
fi
176-
177214
readonly command_for_list_zip='jar tf'
178215
fi
179216

180-
################################################################################
181-
# util funtions
182-
################################################################################
183-
184-
[ -t 1 ] && readonly is_console=true || readonly is_console=false
185-
186-
# Getting console width using a bash script
187-
# https://unix.stackexchange.com/questions/299067
188-
$is_console && readonly columns=$(stty size | awk '{print $2}')
189-
190-
readonly ec=$'\033' # escape char
191-
readonly eend=$'\033[0m' # escape end
192-
readonly cr=$'\r' # carriage return
193-
194-
printResponsiveMessage() {
195-
$is_console || return
196-
197-
local message="$*"
198-
# http://www.linuxforums.org/forum/red-hat-fedora-linux/142825-how-truncate-string-bash-script.html
199-
echo -n "${message:0:columns}"
200-
}
201-
202-
clearResponsiveMessage() {
203-
$is_console || return
204-
205-
# How to delete line with echo?
206-
# https://unix.stackexchange.com/questions/26576
207-
#
208-
# terminal escapes: http://ascii-table.com/ansi-escape-sequences.php
209-
# In particular, to clear from the cursor position to the beginning of the line:
210-
# echo -e "\033[1K"
211-
# Or everything on the line, regardless of cursor position:
212-
# echo -e "\033[2K"
213-
echo -n "$ec[2K$cr"
214-
}
215-
216217
################################################################################
217218
# find logic
218219
################################################################################
219220

220221
readonly jar_files="$(find "${dirs[@]}" "${find_iname_options[@]}" -type f)"
221222
readonly total_count="$(echo $(echo "$jar_files" | wc -l))"
222-
[ -z "$jar_files" ] && {
223-
echo "No jar found!" 1>&2
224-
exit 1
225-
}
223+
[ -n "$jar_files" ] || die "No ${extension[@]} file found!"
226224

227225
findInJarFiles() {
228226
$is_console && local -r grep_color_option='--color=always'
@@ -238,7 +236,7 @@ findInJarFiles() {
238236
clearResponsiveMessage
239237

240238
$is_console &&
241-
echo "$ec[1;31m${jar_file}${eend}${ec}[1;32m${seperator}${eend}${file}" ||
239+
echo "$ec[1;35m${jar_file}${eend}${ec}[1;32m${seperator}${eend}${file}" ||
242240
echo "${jar_file}${seperator}${file}"
243241
done
244242

show-busy-java-threads

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ normalPrint() {
6767
[ -n "$store_dir" ] && echo "$@" >> "${store_file_prefix}$PROG"
6868
}
6969

70-
fatal() {
71-
redPrint "$@" 1>&2
70+
die() {
71+
redPrint "Error: $@" 1>&2
7272
exit 1
7373
}
7474

@@ -127,7 +127,7 @@ EOF
127127
# Check os support
128128
################################################################################
129129

130-
uname | grep '^Linux' -q || fatal "Error: $PROG only support Linux, not support `uname` yet!"
130+
uname | grep '^Linux' -q || die "$PROG only support Linux, not support `uname` yet!"
131131

132132
################################################################################
133133
# parse options
@@ -204,26 +204,26 @@ use_ps=${use_ps:-false}
204204
# check the directory of append-file(-a) mode, create if not exsit.
205205
if [ -n "$append_file" ]; then
206206
if [ -e "$append_file" ]; then
207-
[ ! -f "$append_file" ] && fatal "Error: $append_file(specified by option -a, for storing run output files) exists but is not a file!"
208-
[ ! -w "$append_file" ] && fatal "Error: file $append_file(specified by option -a, for storing run output files) exists but is not writable!"
207+
[ -f "$append_file" ] || die "$append_file(specified by option -a, for storing run output files) exists but is not a file!"
208+
[ -w "$append_file" ] || die "file $append_file(specified by option -a, for storing run output files) exists but is not writable!"
209209
else
210210
append_file_dir="$(dirname "$append_file")"
211211
if [ -e "$append_file_dir" ]; then
212-
[ ! -d "$append_file_dir" ] && fatal "Error: directory $append_file_dir(specified by option -a, for storing run output files) exists but is not a directory!"
213-
[ ! -w "$append_file_dir" ] && fatal "Error: directory $append_file_dir(specified by option -a, for storing run output files) exists but is not writable!"
212+
[ -d "$append_file_dir" ] || die "directory $append_file_dir(specified by option -a, for storing run output files) exists but is not a directory!"
213+
[ -w "$append_file_dir" ] || die "directory $append_file_dir(specified by option -a, for storing run output files) exists but is not writable!"
214214
else
215-
mkdir -p "$append_file_dir" || fatal "Error: fail to create directory $append_file_dir(specified by option -a, for storing run output files)!"
215+
mkdir -p "$append_file_dir" || die "fail to create directory $append_file_dir(specified by option -a, for storing run output files)!"
216216
fi
217217
fi
218218
fi
219219

220220
# check store directory(-S) mode, create directory if not exsit.
221221
if [ -n "$store_dir" ]; then
222222
if [ -e "$store_dir" ]; then
223-
[ ! -d "$store_dir" ] && fatal "Error: $store_dir(specified by option -S, for storing output files) exists but is not a directory!"
224-
[ ! -w "$store_dir" ] && fatal "Error: directory $store_dir(specified by option -S, for storing output files) exists but is not writable!"
223+
[ -d "$store_dir" ] || die "$store_dir(specified by option -S, for storing output files) exists but is not a directory!"
224+
[ -w "$store_dir" ] || die "directory $store_dir(specified by option -S, for storing output files) exists but is not writable!"
225225
else
226-
mkdir -p "$store_dir" || fatal "Error: fail to create directory $store_dir(specified by option -S, for storing output files)!"
226+
mkdir -p "$store_dir" || die "fail to create directory $store_dir(specified by option -S, for storing output files)!"
227227
fi
228228
fi
229229

@@ -232,14 +232,14 @@ fi
232232
################################################################################
233233

234234
if [ -n "$jstack_path" ]; then
235-
[ -f "$jstack_path" ] || fatal "Error: $jstack_path is NOT found!"
236-
[ -x "$jstack_path" ] || fatal "Error: $jstack_path is NOT executalbe!"
235+
[ -f "$jstack_path" ] || die "$jstack_path is NOT found!"
236+
[ -x "$jstack_path" ] || die "$jstack_path is NOT executalbe!"
237237
elif which jstack &> /dev/null; then
238238
jstack_path="`which jstack`"
239239
else
240-
[ -z "$JAVA_HOME" ] && fatal "Error: jstack not found on PATH and No JAVA_HOME setting! Use -s option set jstack path manually."
241-
[ -f "$JAVA_HOME/bin/jstack" ] || fatal "Error: jstack not found on PATH and \$JAVA_HOME/bin/jstack($JAVA_HOME/bin/jstack) file does NOT exists! Use -s option set jstack path manually."
242-
[ -x "$JAVA_HOME/bin/jstack" ] || fatal "Error: jstack not found on PATH and \$JAVA_HOME/bin/jstack($JAVA_HOME/bin/jstack) is NOT executalbe! Use -s option set jstack path manually."
240+
[ -n "$JAVA_HOME" ] || die "jstack not found on PATH and No JAVA_HOME setting! Use -s option set jstack path manually."
241+
[ -f "$JAVA_HOME/bin/jstack" ] || die "jstack not found on PATH and \$JAVA_HOME/bin/jstack($JAVA_HOME/bin/jstack) file does NOT exists! Use -s option set jstack path manually."
242+
[ -x "$JAVA_HOME/bin/jstack" ] || die "jstack not found on PATH and \$JAVA_HOME/bin/jstack($JAVA_HOME/bin/jstack) is NOT executalbe! Use -s option set jstack path manually."
243243
jstack_path="$JAVA_HOME/bin/jstack"
244244
fi
245245

@@ -460,4 +460,4 @@ main() {
460460
done
461461
}
462462

463-
main
463+
main

0 commit comments

Comments
 (0)