Skip to content

Commit b2a6bb6

Browse files
committed
refactor/robust(uq): use printf 💪 instead of echo; use if-else instead of &&-||; use ${var#} instead of awk
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 e5f5fac commit b2a6bb6

File tree

1 file changed

+21
-13
lines changed

1 file changed

+21
-13
lines changed

bin/uq

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,20 +38,28 @@ readonly PROG_VERSION='2.5.0-dev'
3838
################################################################################
3939

4040
# NOTE: $'foo' is the escape sequence syntax of bash
41-
readonly ec=$'\033' # escape char
42-
readonly eend=$'\033[0m' # escape end
43-
readonly nl=$'\n' # new line
44-
45-
redEcho() {
46-
[ -t 1 ] && echo "${ec}[1;31m$*$eend" || echo "$*"
41+
readonly nl=$'\n' # new line
42+
43+
redPrint() {
44+
# -t check: is a terminal device?
45+
if [ -t 1 ]; then
46+
printf "\033[1;31m%s\033[0m\n" "$*"
47+
else
48+
printf '%s\n' "$*"
49+
fi
4750
}
4851

49-
yellowEcho() {
50-
[ -t 1 ] && echo "${ec}[1;33m$*$eend" || echo "$*"
52+
yellowPrint() {
53+
# -t check: is a terminal device?
54+
if [ -t 1 ]; then
55+
printf "\033[1;33m%s\033[0m\n" "$*"
56+
else
57+
printf '%s\n' "$*"
58+
fi
5159
}
5260

5361
die() {
54-
redEcho "Error: $*" 1>&2
62+
redPrint "Error: $*" 1>&2
5563
exit 1
5664
}
5765

@@ -82,7 +90,7 @@ usage() {
8290
# shellcheck disable=SC2015
8391
[ "$exit_code" != 0 ] && local -r out=/dev/stderr || local -r out=/dev/stdout
8492

85-
(($# > 0)) && redEcho "$*$nl" >$out
93+
(($# > 0)) && redPrint "$*$nl" >$out
8694

8795
cat >$out <<EOF
8896
Usage: ${PROG} [OPTION]... [INPUT [OUTPUT]]
@@ -121,7 +129,7 @@ EOF
121129
}
122130

123131
progVersion() {
124-
echo "$PROG $PROG_VERSION"
132+
printf '%s\n' "$PROG $PROG_VERSION"
125133
exit
126134
}
127135

@@ -156,7 +164,7 @@ while (($# > 0)); do
156164
--all-repeated=*)
157165
uq_opt_all_repeated=1
158166

159-
uq_opt_repeated_method=$(echo "$1" | awk -F= '{print $2}')
167+
uq_opt_repeated_method=${1#--all-repeated=}
160168
[[ $uq_opt_repeated_method == 'none' || $uq_opt_repeated_method == 'prepend' || $uq_opt_repeated_method == 'separate' ]] ||
161169
usage 1 "$PROG: invalid argument ‘${uq_opt_repeated_method}’ for ‘--all-repeated’${nl}Valid arguments are:$nl - ‘none’$nl - ‘prepend’$nl - ‘separate’"
162170

@@ -209,7 +217,7 @@ done
209217
usage 2 "printing all duplicate lines(-D, --all-repeated) and unique lines(-u, --unique) is meaningless"
210218

211219
[[ $uq_opt_all_repeated == 1 && $uq_opt_repeated_method == none && ($uq_opt_count == 0 && $uq_opt_only_repeated == 0) ]] &&
212-
yellowEcho "[$PROG] WARN: -D/--all-repeated=none option without -c/-d option, just cat input simply!" >&2
220+
yellowPrint "[$PROG] WARN: -D/--all-repeated=none option without -c/-d option, just cat input simply!" >&2
213221

214222
# NOTE: DO NOT declare var uq_max_input_size as readonly in ONE line!
215223
uq_max_input_size="$(convertHumanReadableSizeToSize "$uq_max_input_human_readable_size")" ||

0 commit comments

Comments
 (0)