Skip to content

Commit dc5b239

Browse files
committed
refactor: use file descriptor number instead of /dev/std*; move var location
1 parent 592719e commit dc5b239

File tree

8 files changed

+27
-34
lines changed

8 files changed

+27
-34
lines changed

bin/ap

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,13 @@ portableReadLink() {
7373
usage() {
7474
local -r exit_code="${1:-0}"
7575
(($# > 0)) && shift
76-
# shellcheck disable=SC2015
77-
[ "$exit_code" != 0 ] && local -r out=/dev/stderr || local -r out=/dev/stdout
76+
local -r out=$(((exit_code != 0) + 1))
7877

7978
# NOTE: $'foo' is the escape sequence syntax of bash
8079
local nl=$'\n' # new line
81-
(($# > 0)) && redPrint "$*$nl" >"$out"
80+
(($# > 0)) && redPrint "$*$nl" >&"$out"
8281

83-
cat >"$out" <<EOF
82+
cat >&"$out" <<EOF
8483
Usage: ${PROG} [OPTION]... [FILE]...
8584
convert to Absolute Path.
8685

bin/c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,11 @@ printErrorMsg() {
4848
usage() {
4949
local -r exit_code="${1:-0}"
5050
(($# > 0)) && shift
51-
# shellcheck disable=SC2015
52-
[ "$exit_code" != 0 ] && local -r out=/dev/stderr || local -r out=/dev/stdout
51+
local -r out=$(((exit_code != 0) + 1))
5352

54-
(($# > 0)) && printErrorMsg "$*" >"$out"
53+
(($# > 0)) && printErrorMsg "$*" >&"$out"
5554

56-
cat >"$out" <<EOF
55+
cat >&"$out" <<EOF
5756
Usage: ${PROG} [OPTION]... [command [command_args ...]]
5857
Run command and put output to system clipper.
5958
If no command is specified, read from stdin(pipe).

bin/cp-into-docker-run

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,13 @@ portableReadLink() {
6666
usage() {
6767
local -r exit_code="${1:-0}"
6868
(($# > 0)) && shift
69-
# shellcheck disable=SC2015
70-
[ "$exit_code" != 0 ] && local -r out=/dev/stderr || local -r out=/dev/stdout
69+
local -r out=$(((exit_code != 0) + 1))
7170

7271
# NOTE: $'foo' is the escape sequence syntax of bash
7372
local nl=$'\n' # new line
74-
(($# > 0)) && redPrint "$*$nl" >"$out"
73+
(($# > 0)) && redPrint "$*$nl" >&"$out"
7574

76-
cat >"$out" <<EOF
75+
cat >&"$out" <<EOF
7776
Usage: ${PROG} [OPTION]... command [command-args]...
7877
7978
Copy the command into docker container

bin/find-in-jars

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ readonly PROG_VERSION='2.6.0-dev'
4141
################################################################################
4242

4343
readonly red='\033[1;31m' normal='\033[0m'
44-
readonly jar_color='\033[1;35m' sep_color='\033[1;32m'
4544

4645
# How to delete line with echo?
4746
# https://unix.stackexchange.com/questions/26576
@@ -95,14 +94,13 @@ die() {
9594
usage() {
9695
local -r exit_code="${1:-0}"
9796
(($# > 0)) && shift
98-
# shellcheck disable=SC2015
99-
[ "$exit_code" != 0 ] && local -r out=/dev/stderr || local -r out=/dev/stdout
97+
local -r out=$(((exit_code != 0) + 1))
10098

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

105-
cat >"$out" <<EOF
103+
cat >&"$out" <<EOF
106104
Usage: ${PROG} [OPTION]... PATTERN
107105
108106
Find files in the jar files under specified directory,
@@ -355,10 +353,12 @@ searchJarFiles() {
355353
printf '%s\n' "$jar_files"
356354
}
357355

356+
readonly jar_color='\033[1;35m' sep_color='\033[1;32m'
357+
358358
__outputResultOfJarFile() {
359359
local jar_file="$1" file
360360
# shellcheck disable=SC2206
361-
local grep_opt_args=($regex_mode ${ignore_case_option:-} ${grep_color_option:-} -- "$pattern")
361+
local grep_opt_args=("$regex_mode" ${ignore_case_option:-} ${grep_color_option:-} -- "$pattern")
362362

363363
if $only_print_file_name; then
364364
local matched=false

bin/rp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,13 @@ portableRelPath() {
7171
usage() {
7272
local -r exit_code="${1:-0}"
7373
(($# > 0)) && shift
74-
# shellcheck disable=SC2015
75-
[ "$exit_code" != 0 ] && local -r out=/dev/stderr || local -r out=/dev/stdout
74+
local -r out=$(((exit_code != 0) + 1))
7675

7776
# NOTE: $'foo' is the escape sequence syntax of bash
7877
local nl=$'\n' # new line
79-
(($# > 0)) && redPrint "$*$nl" >"$out"
78+
(($# > 0)) && redPrint "$*$nl" >&"$out"
8079

81-
cat >"$out" <<EOF
80+
cat >&"$out" <<EOF
8281
Usage: ${PROG} [OPTION]... [FILE]...
8382
convert to Relative Path.
8483

bin/show-busy-java-threads

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,12 +149,11 @@ printCallingCommandLine() {
149149
usage() {
150150
local -r exit_code="${1:-0}"
151151
(($# > 0)) && shift
152-
# shellcheck disable=SC2015
153-
[ "$exit_code" != 0 ] && local -r out=/dev/stderr || local -r out=/dev/stdout
152+
local -r out=$(((exit_code != 0) + 1))
154153

155-
(($# > 0)) && colorPrint 31 "$*$nl" >"$out"
154+
(($# > 0)) && colorPrint 31 "$*$nl" >&"$out"
156155

157-
cat >"$out" <<EOF
156+
cat >&"$out" <<EOF
158157
Usage: ${PROG} [OPTION]... [delay [count]]
159158
Find out the highest cpu consumed threads of java processes,
160159
and print the stack of these threads.

bin/uq

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,11 @@ convertHumanReadableSizeToSize() {
8787
usage() {
8888
local -r exit_code="${1:-0}"
8989
(($# > 0)) && shift
90-
# shellcheck disable=SC2015
91-
[ "$exit_code" != 0 ] && local -r out=/dev/stderr || local -r out=/dev/stdout
90+
local -r out=$(((exit_code != 0) + 1))
9291

93-
(($# > 0)) && redPrint "$*$nl" >"$out"
92+
(($# > 0)) && redPrint "$*$nl" >&"$out"
9493

95-
cat >"$out" <<EOF
94+
cat >&"$out" <<EOF
9695
Usage: ${PROG} [OPTION]... [INPUT [OUTPUT]]
9796
Filter lines from INPUT (or standard input), writing to OUTPUT (or standard output).
9897
Same as \`uniq\` command in core utils,

bin/xpl

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,11 @@ readonly PROG_VERSION='2.6.0-dev'
3737
usage() {
3838
local -r exit_code="${1:-0}"
3939
(($# > 0)) && shift
40-
# shellcheck disable=SC2015
41-
[ "$exit_code" != 0 ] && local -r out=/dev/stderr || local -r out=/dev/stdout
40+
local -r out=$(((exit_code != 0) + 1))
4241

43-
(($# > 0)) && printf '%s\n\n' "$*" >"$out"
42+
(($# > 0)) && printf '%s\n\n' "$*" >&"$out"
4443

45-
cat <<EOF
44+
cat >&"$out" <<EOF
4645
Usage: ${PROG} [OPTION] [FILE]...
4746
Open file in file explorer.
4847
Example: ${PROG} file.txt

0 commit comments

Comments
 (0)