Skip to content

Commit eb47955

Browse files
committed
fix: missing error exit code when file not existed or open file failure ❗️
1 parent adefc1a commit eb47955

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

bin/xpl

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,23 @@ readonly PROG_VERSION='2.6.0-dev'
3434
# util functions
3535
################################################################################
3636

37+
redPrint() {
38+
# if stdout is a terminal, turn on color output.
39+
# '-t' check: is a terminal?
40+
# check isatty in bash https://stackoverflow.com/questions/10022323
41+
if [ -t 1 ]; then
42+
printf '\e[1;31m%s\e[0m\n' "$*"
43+
else
44+
printf '%s\n' "$*"
45+
fi
46+
}
47+
3748
usage() {
3849
local -r exit_code=${1:-0}
3950
(($# > 0)) && shift
4051
local -r out=$(((exit_code != 0) + 1))
4152

42-
(($# > 0)) && printf '%s\n\n' "$*" >&"$out"
53+
(($# > 0)) && redPrint "$*"$'\n' >&"$out"
4354

4455
cat >&"$out" <<EOF
4556
Usage: ${PROG} [OPTION] [FILE]...
@@ -148,12 +159,13 @@ openOneFile() {
148159
has_error=false
149160

150161
for file in "${files[@]}"; do
151-
[ ! -e "$file" ] && {
152-
printf '%s\n' "$file not existed!"
162+
[ -e "$file" ] || {
163+
has_error=true
164+
redPrint "$file not existed!" >&2
153165
continue
154166
}
155167

156-
openOneFile "$file"
168+
openOneFile "$file" || has_error=true
157169
done
158170

159171
# set exit status

0 commit comments

Comments
 (0)