Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e5f5fac

Browse files
refactor/robust(tcp-connection-state-counter/console-text-color-themes.sh): use printf 💪 instead of echo; use if-else instead of &&-||
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 a17b7c2 commit e5f5fac

File tree

2 files changed

+30
-20
lines changed

2 files changed

+30
-20
lines changed

‎bin/tcp-connection-state-counter‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ EOF
3535
}
3636

3737
progVersion() {
38-
echo "$PROG $PROG_VERSION"
38+
printf'%s\n' "$PROG $PROG_VERSION"
3939
exit
4040
}
4141

@@ -58,6 +58,7 @@ done
5858
# On MacOS, netstat need to using -p tcp to get only tcp output.
5959
uname | grep Darwin -q && option_for_mac="-ptcp"
6060

61+
# shellcheck disable=SC2086
6162
netstat -tna ${option_for_mac:-} | awk 'NR > 2 {
6263
++s[$NF]
6364
}

‎lib/console-text-color-themes.sh‎

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -37,55 +37,64 @@ colorEcho() {
3737
local combination="1ドル"
3838
shift 1
3939

40-
[ -t 1 ] && echo "${_ctct_ec}[${combination}m$*$_ctct_eend" || echo "$*"
40+
if [ -t 1 ]; then
41+
echo "${_ctct_ec}[${combination}m$*$_ctct_eend"
42+
else
43+
echo "$*"
44+
fi
4145
}
4246

4347
colorEchoWithoutNewLine() {
4448
local combination="1ドル"
4549
shift 1
4650

47-
[ -t 1 ] && echo -n "${_ctct_ec}[${combination}m$*$_ctct_eend" || echo -n "$*"
51+
if [ -t 1 ]; then
52+
echo -n "${_ctct_ec}[${combination}m$*$_ctct_eend"
53+
else
54+
echo -n "$*"
55+
fi
4856
}
4957

5058
# if not directly run this script(use as lib), just export 2 helper functions,
5159
# and do NOT print anything.
52-
[ "$_ctct_is_direct_run" == "true" ] && {
60+
[ "$_ctct_is_direct_run" == true ] && {
5361
for style in 0 1 2 3 4 5 6 7; do
5462
for fg in 30 31 32 33 34 35 36 37; do
5563
for bg in 40 41 42 43 44 45 46 47; do
5664
combination="${style};${fg};${bg}"
5765
colorEchoWithoutNewLine "$combination" "$combination"
58-
echo -n ""
66+
printf''
5967
done
6068
echo
6169
done
6270
echo
6371
done
6472

65-
echo "Code sample to print color text:"
73+
echo 'Code sample to print color text:'
6674

6775
echo -n ' echo -e "033円['
68-
colorEchoWithoutNewLine "3;35;40""1;36;41"
69-
echo -n "m"
70-
colorEchoWithoutNewLine "0;32;40""Sample Text"
71-
echo "033円[0m\""
76+
colorEchoWithoutNewLine '3;35;40''1;36;41'
77+
echo -n m
78+
colorEchoWithoutNewLine '0;32;40''Sample Text'
79+
echo '033円[0m"'
7280

7381
echo -n " echo \$'033円["
74-
colorEchoWithoutNewLine "3;35;40""1;36;41"
82+
colorEchoWithoutNewLine '3;35;40''1;36;41'
7583
echo -n "m'\""
76-
colorEchoWithoutNewLine "0;32;40""Sample Text"
84+
colorEchoWithoutNewLine '0;32;40''Sample Text'
7785
echo "\"$'033円[0m'"
7886
echo " # NOTE: $'foo' is the escape sequence syntax of bash, safer escape"
7987

80-
echo "Output of above code:"
81-
echo " ${_ctct_ec}[1;36;41mSample Text${_ctct_eend}"
88+
echo 'Output of above code:'
89+
echo -n ' '
90+
colorEcho '1;36;41' 'Sample Text'
8291
echo
83-
echo "If you are going crazy to write text in escapes string like me,"
84-
echo "you can use colorEcho and colorEchoWithoutNewLine function in this script."
92+
echo 'If you are going crazy to write text in escapes string like me,'
93+
echo 'you can use colorEcho and colorEchoWithoutNewLine function in this script.'
8594
echo
86-
echo "Code sample to print color text:"
95+
echo 'Code sample to print color text:'
8796
echo ' colorEcho "1;36;41" "Sample Text"'
88-
echo "Output of above code:"
89-
echo -n " "
90-
colorEcho "1;36;41""Sample Text"
97+
echo 'Output of above code:'
98+
echo -n ' '
99+
colorEcho '1;36;41''Sample Text'
91100
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /