Skip to content

Commit

Permalink
refactor/robust(uq): use printf 💪 instead of echo; use `if-else…
Browse files Browse the repository at this point in the history
…` 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
  • Loading branch information
oldratlee committed Aug 30, 2023
1 parent 6463f9d commit be09758
Showing 1 changed file with 21 additions and 13 deletions.
34 changes: 21 additions & 13 deletions bin/uq
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,28 @@ readonly PROG_VERSION='2.5.0-dev'
################################################################################

# NOTE: $'foo' is the escape sequence syntax of bash
readonly ec=$'\033' # escape char
readonly eend=$'\033[0m' # escape end
readonly nl=$'\n' # new line

redEcho() {
[ -t 1 ] && echo "${ec}[1;31m$*$eend" || echo "$*"
readonly nl=$'\n' # new line

redPrint() {
# -t check: is a terminal device?
if [ -t 1 ]; then
printf "\033[1;31m%s\033[0m\n" "$*"
else
printf '%s\n' "$*"
fi
}

yellowEcho() {
[ -t 1 ] && echo "${ec}[1;33m$*$eend" || echo "$*"
yellowPrint() {
# -t check: is a terminal device?
if [ -t 1 ]; then
printf "\033[1;33m%s\033[0m\n" "$*"
else
printf '%s\n' "$*"
fi
}

die() {
redEcho "Error: $*" 1>&2
redPrint "Error: $*" 1>&2
exit 1
}

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

(($# > 0)) && redEcho "$*$nl" >$out
(($# > 0)) && redPrint "$*$nl" >$out

cat >$out <<EOF
Usage: ${PROG} [OPTION]... [INPUT [OUTPUT]]
Expand Down Expand Up @@ -121,7 +129,7 @@ EOF
}

progVersion() {
echo "$PROG $PROG_VERSION"
printf '%s\n' "$PROG $PROG_VERSION"
exit
}

Expand Down Expand Up @@ -156,7 +164,7 @@ while (($# > 0)); do
--all-repeated=*)
uq_opt_all_repeated=1

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

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

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

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

0 comments on commit be09758

Please sign in to comment.