forked from oldratlee/useful-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathecho-args
executable file
·44 lines (38 loc) · 1.07 KB
/
echo-args
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/bin/bash
# @Function
# print arguments in human and debugging friendly style.
#
# @online-doc https://github.com/oldratlee/useful-scripts/blob/dev-2.x/docs/shell.md#-echo-args
# @author Jerry Lee (oldratlee at gmail dot com)
set -eEuo pipefail
digitCount() {
# argument 1(num) is always a non-negative integer in this script usage,
# so NO argument validation logic.
local num="$1" count=0
while ((num != 0)); do
((++count))
((num = num / 10))
done
echo "$count"
}
digit_count=$(digitCount $#)
readonly arg_count=$# digit_count
readonly red='\e[1;31m'
readonly blue='\e[1;36m'
readonly normal='\e[0m'
printArg() {
local idx="$1" value="$2"
# if stdout is a terminal, turn on color output.
# '-t' check: is a terminal?
# check isatty in bash https://stackoverflow.com/questions/10022323
if [ -t 1 ]; then
printf "%${digit_count}s/%s: ${red}[${blue}%s${red}]${normal}\n" "$idx" "$arg_count" "$value"
else
printf "%${digit_count}s/%s: [%s]\n" "$idx" "$arg_count" "$value"
fi
}
printArg 0 "$0"
idx=1
for a; do
printArg $((idx++)) "$a"
done