Skip to content
This repository was archived by the owner on Nov 29, 2018. It is now read-only.

Commit b9e20a4

Browse files
apply bash best practices
1 parent b14cf97 commit b9e20a4

File tree

1 file changed

+19
-17
lines changed

1 file changed

+19
-17
lines changed

src/circleci-matrix.sh

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ parse_args() {
7777
}
7878

7979
ensure_file() {
80-
if [ ! -f $CONFIG_FILE ]; then
80+
if [ ! -f "$CONFIG_FILE" ]; then
8181
error "No $CONFIG_FILE file found!"
8282
fi
8383
}
@@ -134,10 +134,10 @@ read_file() {
134134
local line_trimmed=$(echo "$line" | sed -e 's/^ *//')
135135
local len_trimmed=$(expr "$line_trimmed" : '.*')
136136
local len_full=$(expr "$line" : '.*')
137-
local group_indent=$(($len_full - $len_trimmed))
137+
local group_indent=$((len_full - len_trimmed))
138138
fi
139139

140-
local line_trimmed=${line:$(($group_indent))}
140+
local line_trimmed=${line:$group_indent}
141141

142142
# Detect element-end
143143
local first_chars=${line_trimmed:0:2}
@@ -172,7 +172,7 @@ read_file() {
172172
# Handle lines
173173
current_line="${current_line}${next_spacer}${line_content}"
174174
next_spacer="${default_spacer}"
175-
done < <(cat $CONFIG_FILE)
175+
done < <(cat "$CONFIG_FILE")
176176

177177
current_line="${current_line:2}"
178178
echo "${current_line}"
@@ -184,14 +184,14 @@ process_commands() {
184184
local tempfile=$(mktemp -t circleci_matrix.XXX)
185185

186186
while read -r line; do
187-
cp -f $envfile $tempfile
188-
echo -e "$line" >> $tempfile
187+
cp -f "$envfile" "$tempfile"
188+
echo -e "$line" >> "$tempfile"
189189

190190
set +e
191-
(bash $tempfile)
191+
(bash "$tempfile")
192192
local exitcode=$?
193193
set -e
194-
rm -rf $tempfile
194+
rm -rf "$tempfile"
195195

196196
if [ $exitcode -ne 0 ]; then
197197
((FAILED_COMMANDS=FAILED_COMMANDS+1))
@@ -209,17 +209,19 @@ process_envs() {
209209
local sources_prefix="$(sources)"
210210

211211
while read -r line; do
212-
if [ $(($i % $CIRCLE_NODE_TOTAL)) -eq $CIRCLE_NODE_INDEX ]; then
212+
if [ $((i % CIRCLE_NODE_TOTAL)) -eq "$CIRCLE_NODE_INDEX" ]; then
213213
print_horizontal_rule
214214
info "Env: $line"
215215
print_horizontal_rule
216216

217-
rm -rf $tempfile
218-
echo "#!/usr/bin/env bash" >> $tempfile
219-
echo "$sources_prefix" >> $tempfile
220-
echo -e "$line" >> $tempfile
221-
process_commands $tempfile
222-
rm -rf $tempfile
217+
rm -rf "$tempfile"
218+
{
219+
echo "#!/usr/bin/env bash";
220+
echo "$sources_prefix";
221+
echo -e "$line"
222+
} >> "$tempfile"
223+
process_commands "$tempfile"
224+
rm -rf "$tempfile"
223225

224226
info ""
225227
fi
@@ -228,7 +230,7 @@ process_envs() {
228230
}
229231

230232
main() {
231-
parse_args $@
233+
parse_args "$@"
232234

233235
info "circleci-matrix version: $VERSION"
234236
info "circleci node total: $CIRCLE_NODE_TOTAL"
@@ -249,4 +251,4 @@ trim_right() {
249251
echo -n "$var"
250252
}
251253

252-
main $@
254+
main "$@"

0 commit comments

Comments
 (0)