Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e9f69f7

Browse files
refactor/robust(find-in-jars): use printf 💪 instead of echo; use if-else instead of &&-||
NOTE: - the `echo` option(e.g. -e -n) may effect correctness, `printf` is more robust 💪 - about `&&-||` see shell check: https://www.shellcheck.net/wiki/SC2015
1 parent ddb91c7 commit e9f69f7

File tree

1 file changed

+37
-26
lines changed

1 file changed

+37
-26
lines changed

‎bin/find-in-jars‎

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,9 @@ readonly PROG_VERSION='2.5.0-dev'
4040
# util functions
4141
################################################################################
4242

43-
# NOTE: $'foo' is the escape sequence syntax of bash
44-
readonly ec=$'033円' # escape char
45-
readonly eend=$'033円[0m' # escape end
46-
readonly nl=$'\n' # new line
43+
readonly red='033円[1;31m' normal='033円[0m'
44+
readonly jar_color='033円[1;35m' sep_color='033円[1;32m'
45+
4746
# How to delete line with echo?
4847
# https://unix.stackexchange.com/questions/26576
4948
#
@@ -52,11 +51,15 @@ readonly nl=$'\n' # new line
5251
# echo -e "033円[1K"
5352
# Or everything on the line, regardless of cursor position:
5453
# echo -e "033円[2K"
55-
readonly clear_line=$'033円[2K\r'
54+
readonly clear_line='033円[2K\r'
5655

57-
redEcho() {
56+
redPrint() {
5857
# -t check: is a terminal device?
59-
[ -t 1 ] && echo "${ec}[1;31m$*$eend" || echo "$*"
58+
if [ -t 1 ]; then
59+
printf "${red}%s${normal}\n" "$*"
60+
else
61+
printf '%s\n' "$*"
62+
fi
6063
}
6164

6265
# Getting console width using a bash script
@@ -72,20 +75,20 @@ printResponsiveMessage() {
7275

7376
local message="$*"
7477
# http://www.linuxforums.org/forum/red-hat-fedora-linux/142825-how-truncate-string-bash-script.html
75-
echo -n "$clear_line${message:0:columns}" >&2
78+
printf"${clear_line}%s""${message:0:columns}" >&2
7679
}
7780

7881
clearResponsiveMessage() {
7982
if ! $show_responsive || [ ! -t 2 ]; then
8083
return
8184
fi
8285

83-
echo -n "$clear_line" >&2
86+
printf"%b" "$clear_line" >&2
8487
}
8588

8689
die() {
8790
clearResponsiveMessage
88-
redEcho "Error: $*" >&2
91+
redPrint "Error: $*" >&2
8992
exit 1
9093
}
9194

@@ -95,7 +98,9 @@ usage() {
9598
# shellcheck disable=SC2015
9699
[ "$exit_code" != 0 ] && local -r out=/dev/stderr || local -r out=/dev/stdout
97100

98-
(($# > 0)) && redEcho "$*$nl" >$out
101+
# NOTE: $'foo' is the escape sequence syntax of bash
102+
local -r nl=$'\n' # new line
103+
(($# > 0)) && redPrint "$*$nl" >$out
99104

100105
cat >$out <<EOF
101106
Usage: ${PROG} [OPTION]... PATTERN
@@ -144,7 +149,7 @@ EOF
144149
}
145150

146151
progVersion() {
147-
echo "$PROG $PROG_VERSION"
152+
printf'%s\n' "$PROG $PROG_VERSION"
148153
exit
149154
}
150155

@@ -227,7 +232,7 @@ while (($# > 0)); do
227232
break
228233
;;
229234
-*)
230-
usage 2 "${PROG}: unrecognized option '1ドル'"
235+
usage 2 "Error: unrecognized option '1ドル'"
231236
;;
232237
*)
233238
args=(${args[@]:+"${args[@]}"} "1ドル")
@@ -319,15 +324,15 @@ listZipEntries() {
319324
# exit code is 1, and print 'Empty zipfile.'
320325
if [ "$msg" != 'Empty zipfile.' ]; then
321326
clearResponsiveMessage
322-
redEcho "fail to list zip entries of $zip_file, ignored: $msg" >&2
327+
redPrint "fail to list zip entries of $zip_file, ignored: $msg" >&2
323328
fi
324329
return 0
325330
}
326331
fi
327332

328333
"${command_to_list_zip_entries[@]}" "$zip_file" || {
329334
clearResponsiveMessage
330-
redEcho "fail to list zip entries of $zip_file, ignored!" >&2
335+
redPrint "fail to list zip entries of $zip_file, ignored!" >&2
331336
return 0
332337
}
333338
}
@@ -344,17 +349,18 @@ searchJarFiles() {
344349
jar_files="$(find "${dirs[@]}" "${find_iname_options[@]}" -type f)"
345350
[ -n "$jar_files" ] || die "No ${extensions[*]} file found!"
346351

347-
total_jar_count="$(echo "$jar_files" | wc -l)"
348-
# delete white space
349-
# because the output of mac system command `wc -l` contains white space!
352+
total_jar_count="$(printf '%s\n' "$jar_files" | wc -l)"
353+
# remove white space, because the `wc -l` output on mac contains white space!
350354
total_jar_count="${total_jar_count//[[:space:]]/}"
351355

352356
echo "$total_jar_count"
353-
echo "$jar_files"
357+
printf'%s\n' "$jar_files"
354358
}
355359

356360
__outputResultOfJarFile() {
357361
local jar_file="1ドル" file
362+
# shellcheck disable=SC2206
363+
local grep_opt_args=($regex_mode ${ignore_case_option:-} ${grep_color_option:-} -- "$pattern")
358364

359365
if $only_print_file_name; then
360366
local matched=false
@@ -366,7 +372,7 @@ __outputResultOfJarFile() {
366372
# - https://stackoverflow.com/questions/19120263/why-exit-code-141-with-grep-q
367373
# - https://unix.stackexchange.com/questions/305547/broken-pipe-when-grepping-output-but-only-with-i-flag
368374
# - http://www.pixelbeat.org/programming/sigpipe_handling.html
369-
if grep $regex_mode${ignore_case_option:-} -c -- "$pattern" &>/dev/null; then
375+
if grep -c "${grep_opt_args[@]}" &>/dev/null; then
370376
matched=true
371377
fi
372378

@@ -375,18 +381,23 @@ __outputResultOfJarFile() {
375381
fi
376382

377383
clearResponsiveMessage
378-
[ -t 1 ] && echo "${ec}[1;35m${jar_file}${eend}" || echo "${jar_file}"
384+
if [ -t 1 ]; then
385+
printf "${jar_color}%s${normal}\n" "${jar_file}"
386+
else
387+
printf '%s\n' "${jar_file}"
388+
fi
379389
else
380390
{
381391
# Prevent grep from exiting in case of no match
382392
# https://unix.stackexchange.com/questions/330660
383-
# shellcheck disable=SC2086
384-
grep $regex_mode ${ignore_case_option:-} ${grep_color_option:-} -- "$pattern" || true
393+
grep "${grep_opt_args[@]}" || true
385394
} | while read -r file; do
386395
clearResponsiveMessage
387-
[ -t 1 ] &&
388-
echo "${ec}[1;35m${jar_file}${eend}${ec}[1;32m${separator}${eend}${file}" ||
389-
echo "${jar_file}${separator}${file}"
396+
if [ -t 1 ]; then
397+
printf "${jar_color}%s${sep_color}%s${normal}%s\n" "$jar_file" "$separator" "$file"
398+
else
399+
printf '%s\n' "${jar_file}${separator}${file}"
400+
fi
390401
done
391402
fi
392403
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /