Skip to content

Commit dce9ca8

Browse files
committed
! refactor/improve bash script
- use == instead of = in test - fix typo - use $() instead of `` - use [[ ]] instead of [] when compound condition - use if-else instead of &&-||
1 parent 7942b5b commit dce9ca8

File tree

8 files changed

+66
-63
lines changed

8 files changed

+66
-63
lines changed

bin/uq

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,15 +209,15 @@ elif ((argc == 1)); then
209209
else
210210
input_files=("${argv[@]:0:argc-1}")
211211
output_file=${argv[argc - 1]}
212-
if [ "$output_file" = - ]; then
212+
if [ "$output_file" == - ]; then
213213
output_file=/dev/stdout
214214
fi
215215
fi
216216

217217
# Check input file
218218
for f in ${input_files[@]:+"${input_files[@]}"}; do
219219
# - is stdin, ok
220-
[ "$f" = - ] && continue
220+
[ "$f" == - ] && continue
221221

222222
[ -e "$f" ] || die "input file $f does not exist!"
223223
[ ! -d "$f" ] || die "input file $f exists, but is a directory!"

legacy-bin/cp-svn-url

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
# tested on bash 3.2.57/4.2.46
2121

2222
# NOTE: DO NOT declare var PROG as readonly, because its value is supplied by subshell.
23-
PROG=`basename $0`
23+
PROG="$(basename "$0")"
2424

2525
usage() {
2626
cat <<EOF
@@ -35,11 +35,12 @@ Example:
3535
Options:
3636
-h, --help display this help and exit
3737
EOF
38+
# shellcheck disable=SC2086
3839
exit $1
3940
}
4041

4142
for a; do
42-
[ -h = "$a" -o --help = "$1" ] && usage
43+
[[ "-h" == "$a" || "--help" == "$a" ]] && usage
4344
done
4445

4546
[ $# -gt 1 ] && { echo At most 1 local directory is need! ; usage 1; }
@@ -54,7 +55,7 @@ if [ -z "${url}" ]; then
5455
fi
5556

5657
copy() {
57-
case "`uname`" in
58+
case "$(uname)" in
5859
Darwin*)
5960
pbcopy ;;
6061
CYGWIN*|MINGW*)

legacy-bin/svn-merge-stop-on-copy

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/bin/bash
22
# @Function
3-
# svn merge commit between verison when source branch copy(--stop-on-copy)
3+
# svn merge commit between version when source branch copy(--stop-on-copy)
44
# and head version of source branch.
55
#
66
# @Usage
@@ -11,28 +11,29 @@
1111
# @author jiangjizhong(@jzwlqx)
1212
# @author Jerry Lee (oldratlee at gmail dot com)
1313

14-
PROG=`basename $0`
14+
PROG="$(basename "$0")"
1515

1616
usage() {
1717
cat <<EOF
1818
Usage: ${PROG} <source branch> [target branch]
19-
svn merge commit between verison when source branch copy(--stop-on-copy)
19+
svn merge commit between version when source branch copy(--stop-on-copy)
2020
and head version of source branch.
2121
Source branch must be a remote branch.
2222
2323
Example:
2424
${PROG} http://www.foo.com/project1/branches/feature1
25-
# merge http://www.foo.com/project1/branches/feature1 to current svn direcotry
25+
# merge http://www.foo.com/project1/branches/feature1 to current svn directory
2626
27-
${PROG} http://www.foo.com/project1/branches/feature1 /path/to/svn/direcotry
28-
# merge branch http://www.foo.com/project1/branches/feature1 to svn direcotry /path/to/svn/direcotry
29-
# will prompt comfirm for committing to target branch.
27+
${PROG} http://www.foo.com/project1/branches/feature1 /path/to/svn/directory
28+
# merge branch http://www.foo.com/project1/branches/feature1 to svn directory /path/to/svn/directory
29+
# will prompt confirm for committing to target branch.
3030
3131
${PROG} http://www.foo.com/project1/branches/feature1 http://www.foo.com/project1/branches/feature2
3232
# merge http://www.foo.com/project1/branches/feature1 to branch http://www.foo.com/project1/branches/feature2
3333
# because http://www.foo.com/project1/branches/feature2 is remote url,
34-
# will check out target branch to tmp direcotry, and prompt comfirm for committing to target branch.
34+
# will check out target branch to tmp directory, and prompt confirm for committing to target branch.
3535
EOF
36+
# shellcheck disable=SC2086
3637
exit $1
3738
}
3839

@@ -68,21 +69,21 @@ cleanup() {
6869
}
6970
trap "cleanup" EXIT
7071

71-
svnstatusline=$(svn status --ignore-externals "$workDir" | grep -v ^X | wc -l)
72-
[ "$svnstatusline" -ne 0 ] && {
73-
echo "svn work direcotry is modified!"
72+
svn_status_line=$(svn status --ignore-externals "$workDir" | grep -c -v ^X)
73+
[ "$svn_status_line" -ne 0 ] && {
74+
echo "svn work directory is modified!"
7475
exit 1
7576
}
7677

7778
cd "$workDir" &&
78-
from_version=$(svn log --stop-on-copy --quiet "$source_branch" | awk '$1~/^r[0-9]+/{print $1}' | tail -n1) && {
79+
if from_version=$(svn log --stop-on-copy --quiet "$source_branch" | awk '$1~/^r[0-9]+/{print $1}' | tail -n1); then
7980
echo "oldest version($from_version) of source branch $source_branch ."
8081
echo "starting merge to $workDir ."
81-
svn merge -${from_version}:HEAD $source_branch
82-
} || {
82+
svn merge "-${from_version}:HEAD" "$source_branch"
83+
else
8384
echo "Fail to merge to work dir $workDir ."
8485
exit 2
85-
}
86+
fi
8687

87-
read -p "Check In? (Y/N)" ci
88-
[ "$ci" = "Y" ] && svn ci -m "svn merge -${from_version}:HEAD $source_branch"
88+
read -r -p "Check In? (Y/N)" ci
89+
[ "$ci" == "Y" ] && svn ci -m "svn merge -${from_version}:HEAD $source_branch"

legacy-bin/swtrunk

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@
99
# @author Jerry Lee (oldratlee at gmail dot com)
1010

1111
# NOTE: $'foo' is the escape sequence syntax of bash
12-
readonly ec=$'\033' # escape char
12+
readonly ec=$'\033' # escape char
1313
readonly eend=$'\033[0m' # escape end
1414

1515
colorEcho() {
1616
local color=$1
1717
shift
1818
# if stdout is console, turn on color output.
19-
[ -t 1 ] && echo "$ec[1;${color}m$@$eend" || echo "$@"
19+
[ -t 1 ] && echo "${ec}[1;${color}m$*$eend" || echo "$@"
2020
}
2121

2222
redEcho() {
@@ -29,18 +29,19 @@ greenEcho() {
2929

3030
[ $# -eq 0 ] && dirs=(.) || dirs=("$@")
3131

32-
for d in "${dirs[@]}" ; do
33-
[ ! -d ${d}/.svn ] && {
32+
for d in "${dirs[@]}"; do
33+
[ ! -d "${d}/.svn" ] && {
3434
redEcho "directory $d is not a svn work directory, ignore directory $d !"
3535
continue
3636
}
3737
(
3838
cd "$d" &&
39-
branches=`svn info | grep '^URL' | awk '{print $2}'` &&
40-
trunk=`echo $branches | awk -F'/branches/' '{print $1}'`/trunk &&
41-
42-
svn sw "$trunk" &&
43-
greenEcho "svn work directory $d switch from ${branches} to ${trunk} ." ||
44-
redEcho "fail to switch $d to trunk!"
39+
branches=$(svn info | grep '^URL' | awk '{print $2}') &&
40+
trunk=$(echo "$branches" | awk -F'/branches/' '{print $1}')/trunk &&
41+
if svn sw "$trunk"; then
42+
greenEcho "svn work directory $d switch from ${branches} to ${trunk} ."
43+
else
44+
redEcho "fail to switch $d to trunk!"
45+
fi
4546
)
4647
done

lib/parseOpts.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ _opts_findOptMode() {
7676

7777
local optName
7878
for optName in "${idxNameArray[@]:1:${#idxNameArray[@]}}"; do # index from 1, skip mode
79-
[ "$opt" = "${optName}" ] && {
79+
[ "$opt" == "${optName}" ] && {
8080
echo "$mode"
8181
return
8282
}
@@ -111,7 +111,7 @@ _opts_setOptValue() {
111111

112112
local optName
113113
for optName in "${idxNameArray[@]:1:${#idxNameArray[@]}}"; do # index from 1, skip mode
114-
[ "$opt" = "$optName" ] && {
114+
[ "$opt" == "$optName" ] && {
115115
local optName2
116116
for optName2 in "${idxNameArray[@]:1:${#idxNameArray[@]}}"; do
117117
local optValueVarName="_OPT_VALUE_`_opts_convertToVarName "${optName2}"`"
@@ -138,7 +138,7 @@ _opts_setOptArray() {
138138

139139
local optName
140140
for optName in "${idxNameArray[@]:1:${#idxNameArray[@]}}"; do # index from 1, skip mode
141-
[ "$opt" = "$optName" ] && {
141+
[ "$opt" == "$optName" ] && {
142142
# set _OPT_VALUE
143143
local optName2
144144
for optName2 in "${idxNameArray[@]:1:${#idxNameArray[@]}}"; do
@@ -286,7 +286,7 @@ parseOpts() {
286286

287287
local value
288288
for value in "$@" ; do
289-
[ ";" = "$value" ] && {
289+
[ ";" == "$value" ] && {
290290
foundComma=true
291291
break
292292
} || valueArray=("${valueArray[@]}" "$value")

test-cases/integration-test.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ blueEcho() {
4343

4444
logAndRun() {
4545
local simple_mode=false
46-
[ "$1" = "-s" ] && {
46+
[ "$1" == "-s" ] && {
4747
simple_mode=true
4848
shift
4949
}

test-cases/my_unit_test_lib.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ assertEquals() {
7474
failMsg=$1
7575
shift
7676
}
77-
[ "$1" = "$2" ] || fail "assertEqual fail [$1] != [$2]${failMsg:+: $failMsg}"
77+
[ "$1" == "$2" ] || fail "assertEqual fail [$1] != [$2]${failMsg:+: $failMsg}"
7878
}
7979

8080
readonly __ut_exclude_vars_builtin='^BASH_|^_=|^COLUMNS=|LINES='

test-cases/parseOpts_test.sh

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ _opts_showOptValueInfoList
2222
[ $test_exitCode -eq 0 ] || fail "Wrong exit code!"
2323
[ ${#_OPT_INFO_LIST_INDEX[@]} -eq 4 ] || fail "Wrong _OPT_INFO_LIST_INDEX!"
2424

25-
[ $_OPT_VALUE_a = "true" ] && [ $_OPT_VALUE_a_long = "true" ] || fail "Wrong option value of a!"
26-
[ $_OPT_VALUE_b = "bb" ] && [ $_OPT_VALUE_b_long = "bb" ] || fail "Wrong option value of b!"
25+
[[ $_OPT_VALUE_a == "true" && $_OPT_VALUE_a_long == "true" ]] || fail "Wrong option value of a!"
26+
[[ $_OPT_VALUE_b == "bb" && $_OPT_VALUE_b_long == "bb" ]] || fail "Wrong option value of b!"
2727

2828
test_cArray=(c.sh -p pv -q qv cc)
2929
assertArrayEquals "Wrong option value of c!" test_cArray _OPT_VALUE_c
@@ -53,14 +53,14 @@ _opts_showOptValueInfoList
5353
[ $test_exitCode -eq 0 ] || fail "Wrong exit code!"
5454
[ ${#_OPT_INFO_LIST_INDEX[@]} -eq 4 ] || fail "Wrong _OPT_INFO_LIST_INDEX!"
5555

56-
[ $_OPT_VALUE_a = "true" ] && [ $_OPT_VALUE_a_long = "true" ] || fail "Wrong option value of a!"
57-
[ $_OPT_VALUE_b = "bb" ] && [ $_OPT_VALUE_b_long = "bb" ] || fail "Wrong option value of b!"
56+
[[ $_OPT_VALUE_a == "true" && $_OPT_VALUE_a_long == "true" ]] || fail "Wrong option value of a!"
57+
[[ $_OPT_VALUE_b == "bb" && $_OPT_VALUE_b_long == "bb" ]] || fail "Wrong option value of b!"
5858

5959
test_cArray=(c.sh -p pv -q qv cc)
6060
assertArrayEquals "Wrong option value of c!" test_cArray _OPT_VALUE_c
6161
assertArrayEquals "Wrong option value of c!" test_cArray _OPT_VALUE_c_long
6262

63-
[ "$_OPT_VALUE_d" = "" ] && [ "$_OPT_VALUE_d_long" = "" ] || fail "Wrong option value of d!"
63+
[[ "$_OPT_VALUE_d" == "" && "$_OPT_VALUE_d_long" == "" ]] || fail "Wrong option value of d!"
6464

6565
test_argArray=(aa bb --d-long d.sh -x xv d1 d2 d3 \; cc dd ee)
6666
assertArrayEquals "Wrong args!" test_argArray _OPT_ARGS
@@ -81,11 +81,11 @@ _opts_showOptValueInfoList
8181

8282
[ $test_exitCode -eq 232 ] || fail "Wrong exit code!"
8383
[ ${#_OPT_INFO_LIST_INDEX[@]} -eq 0 ] || fail "Wrong _OPT_INFO_LIST_INDEX!"
84-
[ "$_OPT_VALUE_a" = "" ] && [ "$_OPT_VALUE_a_long" = "" ] || fail "Wrong option value of a!"
85-
[ "$_OPT_VALUE_b" = "" ] && [ "$_OPT_VALUE_b_long" = "" ] || fail "Wrong option value of b!"
86-
[ "$_OPT_VALUE_c" = "" ] && [ "$_OPT_VALUE_c_long" = "" ] || fail "Wrong option value of c!"
87-
[ "$_OPT_VALUE_d" = "" ] && [ "$_OPT_VALUE_d_long" = "" ] || fail "Wrong option value of d!"
88-
[ "$_OPT_ARGS" = "" ] || fail "Wrong args!"
84+
[[ "$_OPT_VALUE_a" == "" && "$_OPT_VALUE_a_long" == "" ]] || fail "Wrong option value of a!"
85+
[[ "$_OPT_VALUE_b" == "" && "$_OPT_VALUE_b_long" == "" ]] || fail "Wrong option value of b!"
86+
[[ "$_OPT_VALUE_c" == "" && "$_OPT_VALUE_c_long" == "" ]] || fail "Wrong option value of c!"
87+
[[ "$_OPT_VALUE_d" == "" && "$_OPT_VALUE_d_long" == "" ]] || fail "Wrong option value of d!"
88+
[ "$_OPT_ARGS" == "" ] || fail "Wrong args!"
8989

9090
# ========================================
9191
blueEcho "Test case: empty options"
@@ -98,11 +98,11 @@ _opts_showOptValueInfoList
9898

9999
[ $test_exitCode -eq 0 ] || fail "Wrong exit code!"
100100
[ ${#_OPT_INFO_LIST_INDEX[@]} -eq 4 ] || fail "Wrong _OPT_INFO_LIST_INDEX!"
101-
[ "$_OPT_VALUE_a" = "" ] && [ "$_OPT_VALUE_a_long" = "" ] || fail "Wrong option value of a!"
102-
[ "$_OPT_VALUE_b" = "" ] && [ "$_OPT_VALUE_b_long" = "" ] || fail "Wrong option value of b!"
103-
[ "$_OPT_VALUE_c" = "" ] && [ "$_OPT_VALUE_c_long" = "" ] || fail "Wrong option value of c!"
104-
[ "$_OPT_VALUE_d" = "" ] && [ "$_OPT_VALUE_d_long" = "" ] || fail "Wrong option value of d!"
105-
[ "$_OPT_ARGS" = "" ] || fail "Wrong args!"
101+
[[ "$_OPT_VALUE_a" == "" && "$_OPT_VALUE_a_long" == "" ]] || fail "Wrong option value of a!"
102+
[[ "$_OPT_VALUE_b" == "" && "$_OPT_VALUE_b_long" == "" ]] || fail "Wrong option value of b!"
103+
[[ "$_OPT_VALUE_c" == "" && "$_OPT_VALUE_c_long" == "" ]] || fail "Wrong option value of c!"
104+
[[ "$_OPT_VALUE_d" == "" && "$_OPT_VALUE_d_long" == "" ]] || fail "Wrong option value of d!"
105+
[ "$_OPT_ARGS" == "" ] || fail "Wrong args!"
106106

107107
# ========================================
108108
blueEcho "Test case: illegal option name"
@@ -115,11 +115,11 @@ _opts_showOptValueInfoList
115115

116116
[ $test_exitCode -eq 221 ] || fail "Wrong exit code!"
117117
[ ${#_OPT_INFO_LIST_INDEX[@]} -eq 0 ] || fail "Wrong _OPT_INFO_LIST_INDEX!"
118-
[ "$_OPT_VALUE_a" = "" ] && [ "$_OPT_VALUE_a_long" = "" ] || fail "Wrong option value of a!"
119-
[ "$_OPT_VALUE_b" = "" ] && [ "$_OPT_VALUE_b_long" = "" ] || fail "Wrong option value of b!"
120-
[ "$_OPT_VALUE_c" = "" ] && [ "$_OPT_VALUE_c_long" = "" ] || fail "Wrong option value of c!"
121-
[ "$_OPT_VALUE_d" = "" ] && [ "$_OPT_VALUE_d_long" = "" ] || fail "Wrong option value of d!"
122-
[ "$_OPT_ARGS" = "" ] || fail "Wrong args!"
118+
[[ "$_OPT_VALUE_a" == "" && "$_OPT_VALUE_a_long" == "" ]] || fail "Wrong option value of a!"
119+
[[ "$_OPT_VALUE_b" == "" && "$_OPT_VALUE_b_long" == "" ]] || fail "Wrong option value of b!"
120+
[[ "$_OPT_VALUE_c" == "" && "$_OPT_VALUE_c_long" == "" ]] || fail "Wrong option value of c!"
121+
[[ "$_OPT_VALUE_d" == "" && "$_OPT_VALUE_d_long" == "" ]] || fail "Wrong option value of d!"
122+
[ "$_OPT_ARGS" == "" ] || fail "Wrong args!"
123123

124124
parseOpts "a,a-long|b,b-long:|c,c-long+|d,d-long+|z,z-#long" aa -a -b bb -x -c c.sh -p pv -q qv cc \; bb -d d.sh -x xv d1 d2 d3 \; cc -- dd ee
125125
test_exitCode=$?
@@ -128,11 +128,11 @@ _opts_showOptValueInfoList
128128

129129
[ $test_exitCode -eq 222 ] || fail "Wrong exit code!"
130130
[ ${#_OPT_INFO_LIST_INDEX[@]} -eq 0 ] || fail "Wrong _OPT_INFO_LIST_INDEX!"
131-
[ "$_OPT_VALUE_a" = "" ] && [ "$_OPT_VALUE_a_long" = "" ] || fail "Wrong option value of a!"
132-
[ "$_OPT_VALUE_b" = "" ] && [ "$_OPT_VALUE_b_long" = "" ] || fail "Wrong option value of b!"
133-
[ "$_OPT_VALUE_c" = "" ] && [ "$_OPT_VALUE_c_long" = "" ] || fail "Wrong option value of c!"
134-
[ "$_OPT_VALUE_d" = "" ] && [ "$_OPT_VALUE_d_long" = "" ] || fail "Wrong option value of d!"
135-
[ "$_OPT_ARGS" = "" ] || fail "Wrong args!"
131+
[[ "$_OPT_VALUE_a" == "" && "$_OPT_VALUE_a_long" == "" ]] || fail "Wrong option value of a!"
132+
[[ "$_OPT_VALUE_b" == "" && "$_OPT_VALUE_b_long" == "" ]] || fail "Wrong option value of b!"
133+
[[ "$_OPT_VALUE_c" == "" && "$_OPT_VALUE_c_long" == "" ]] || fail "Wrong option value of c!"
134+
[[ "$_OPT_VALUE_d" == "" && "$_OPT_VALUE_d_long" == "" ]] || fail "Wrong option value of d!"
135+
[ "$_OPT_ARGS" == "" ] || fail "Wrong args!"
136136

137137
assertAllVarsSame
138138

0 commit comments

Comments
 (0)