Skip to content

Commit 9e2a121

Browse files
Merge pull request #742 from nodejs/improve-coding-style
Improve coding style of shell scripts
2 parents d02b4f4 + bb92568 commit 9e2a121

File tree

5 files changed

+107
-106
lines changed

5 files changed

+107
-106
lines changed

functions.sh

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
#!/bin/bash
2-
1+
#!/usr/bin/env bash
2+
#
33
# Utlity functions
44

55
info() {
@@ -37,12 +37,12 @@ function get_arch() {
3737
arch="arm32v7"
3838
;;
3939
*)
40-
echo "$0 does not support architecture $arch ... aborting"
40+
echo "$0 does not support architecture ${arch} ... aborting"
4141
exit 1
4242
;;
4343
esac
4444

45-
echo "$arch"
45+
echo "${arch}"
4646
}
4747

4848
# Get corresponding variants based on the architecture.
@@ -62,12 +62,12 @@ function get_variants() {
6262

6363
arch=$(get_arch)
6464
variantsfilter=("$@")
65-
IFS=' ' read -ra availablevariants <<<"$(grep "^$arch" "$dir/architectures" | sed -E 's/'"$arch"'[[:space:]]*//' | sed -E 's/,/ /g')"
65+
IFS=' ' read -ra availablevariants <<<"$(grep "^${arch}" "${dir}/architectures" | sed -E 's/'"${arch}"'[[:space:]]*//' | sed -E 's/,/ /g')"
6666

6767
if [ ${#variantsfilter[@]} -gt 0 ]; then
6868
for variant1 in "${availablevariants[@]}"; do
6969
for variant2 in "${variantsfilter[@]}"; do
70-
if [[ "$variant1" == "$variant2" ]]; then
70+
if [ "$variant1" = "$variant2" ]; then
7171
variants+=("$variant1")
7272
fi
7373
done
@@ -100,16 +100,16 @@ function get_supported_arches() {
100100
shift
101101

102102
# Get default supported arches
103-
lines=$(grep "$variant" "$(dirname "$version")"/architectures 2>/dev/null | cut -d' ' -f1)
103+
lines=$(grep "$variant" "$(dirname "${version}")"/architectures 2>/dev/null | cut -d' ' -f1)
104104

105105
# Get version specific supported architectures if there is specialized information
106-
if [ -a "$version"/architectures ]; then
107-
lines=$(grep "$variant" "$version"/architectures 2>/dev/null | cut -d' ' -f1)
106+
if [ -a "${version}"/architectures ]; then
107+
lines=$(grep "$variant" "${version}"/architectures 2>/dev/null | cut -d' ' -f1)
108108
fi
109109

110110
while IFS='' read -r line; do
111-
arches+=("$line")
112-
done <<<"$lines"
111+
arches+=("${line}")
112+
done <<<"${lines}"
113113

114114
echo "${arches[@]}"
115115
}
@@ -127,7 +127,7 @@ function get_config() {
127127
shift
128128

129129
local value
130-
value=$(grep "^$name" "$dir/config" | sed -E 's/'"$name"'[[:space:]]*//')
130+
value=$(grep "^$name" "${dir}/config" | sed -E 's/'"$name"'[[:space:]]*//')
131131
echo "$value"
132132
}
133133

@@ -150,13 +150,13 @@ function get_versions() {
150150
fi
151151

152152
for dir in "${dirs[@]}"; do
153-
if [ -a "$dir/config" ]; then
153+
if [ -a "${dir}/config" ]; then
154154
local subdirs
155155
IFS=' ' read -ra subdirs <<<"$(get_versions "${dir#./}")"
156156
for subdir in "${subdirs[@]}"; do
157157
versions+=("$subdir")
158158
done
159-
elif [ -a "$dir/Dockerfile" ]; then
159+
elif [ -a "${dir}/Dockerfile" ]; then
160160
versions+=("${dir#./}")
161161
fi
162162
done
@@ -171,7 +171,7 @@ function get_fork_name() {
171171
version=$1
172172
shift
173173

174-
IFS='/' read -ra versionparts <<<"$version"
174+
IFS='/' read -ra versionparts <<<"${version}"
175175
if [ ${#versionparts[@]} -gt 1 ]; then
176176
echo "${versionparts[0]}"
177177
fi
@@ -182,7 +182,7 @@ function get_full_version() {
182182
version=$1
183183
shift
184184

185-
grep -m1 'ENV NODE_VERSION ' "$version/Dockerfile" | cut -d' ' -f3
185+
grep -m1 'ENV NODE_VERSION ' "${version}/Dockerfile" | cut -d' ' -f3
186186
}
187187

188188
function get_major_minor_version() {
@@ -191,9 +191,9 @@ function get_major_minor_version() {
191191
shift
192192

193193
local fullversion
194-
fullversion=$(get_full_version "$version")
194+
fullversion=$(get_full_version "${version}")
195195

196-
echo "$(echo "$fullversion" | cut -d'.' -f1).$(echo "$fullversion" | cut -d'.' -f2)"
196+
echo "$(echo "${fullversion}" | cut -d'.' -f1).$(echo "${fullversion}" | cut -d'.' -f2)"
197197
}
198198

199199
function get_tag() {
@@ -206,14 +206,14 @@ function get_tag() {
206206
shift
207207

208208
local tagversion
209-
if [ "$versiontype" = full ]; then
210-
tagversion=$(get_full_version "$version")
211-
elif [ "$versiontype" = majorminor ]; then
212-
tagversion=$(get_major_minor_version "$version")
209+
if [ "${versiontype}" = full ]; then
210+
tagversion=$(get_full_version "${version}")
211+
elif [ "${versiontype}" = majorminor ]; then
212+
tagversion=$(get_major_minor_version "${version}")
213213
fi
214214

215215
local tagparts
216-
IFS=' ' read -ra tagparts <<<"$(get_fork_name "$version") $tagversion"
216+
IFS=' ' read -ra tagparts <<<"$(get_fork_name "${version}") ${tagversion}"
217217
IFS='-'
218218
echo "${tagparts[*]}"
219219
unset IFS
@@ -230,12 +230,12 @@ function sort_versions() {
230230
unset IFS
231231

232232
while IFS='' read -r line; do
233-
sorted+=("$line")
234-
done <<<"$(echo "$lines" | grep "^[0-9]" | sort -r)"
233+
sorted+=("${line}")
234+
done <<<"$(echo "${lines}" | grep "^[0-9]" | sort -r)"
235235

236236
while IFS='' read -r line; do
237-
sorted+=("$line")
238-
done <<<"$(echo "$lines" | grep -v "^[0-9]" | sort -r)"
237+
sorted+=("${line}")
238+
done <<<"$(echo "${lines}" | grep -v "^[0-9]" | sort -r)"
239239

240240
echo "${sorted[@]}"
241241
}

generate-stackbrew-library.sh

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
#!/bin/bash
1+
#!/usr/bin/env bash
2+
23
set -e
34
. functions.sh
45

@@ -31,7 +32,7 @@ fileCommit() {
3132
git log -1 --format='format:%H' HEAD -- "$@"
3233
}
3334

34-
echo "# this file is generated via ${url}/blob/$(fileCommit "$self")/$self"
35+
echo "# this file is generated via ${url}/blob/$(fileCommit "${self}")/${self}"
3536
echo
3637
echo "Maintainers: The Node.js Docker Team <${url}> (@nodejs)"
3738
echo "GitRepo: ${url}.git"
@@ -49,24 +50,24 @@ join() {
4950
get_stub() {
5051
local version="$1"
5152
shift
52-
IFS='/' read -ra versionparts <<<"$version"
53+
IFS='/' read -ra versionparts <<<"${version}"
5354
local stub
5455
eval stub="$(join '_' "${versionparts[@]}" | awk -F. '{ print "$array_" $1 }')"
5556
echo "$stub"
5657
}
5758

5859
for version in "${versions[@]}"; do
5960
# Skip "docs" and other non-docker directories
60-
[ -f "$version/Dockerfile" ] || continue
61+
[ -f "${version}/Dockerfile" ] || continue
6162

62-
stub=$(get_stub "$version")
63-
commit="$(fileCommit "$version")"
64-
fullVersion="$(get_tag "$version" full)"
65-
majorMinorVersion="$(get_tag "$version" majorminor)"
63+
stub=$(get_stub "${version}")
64+
commit="$(fileCommit "${version}")"
65+
fullVersion="$(get_tag "${version}" full)"
66+
majorMinorVersion="$(get_tag "${version}" majorminor)"
6667

6768
IFS=' ' read -ra versionAliases <<<"$fullVersion $majorMinorVersion $stub"
6869
# Get supported architectures for a specific version. See details in function.sh
69-
IFS=' ' read -ra supportedArches <<<"$(get_supported_arches "$version" "default")"
70+
IFS=' ' read -ra supportedArches <<<"$(get_supported_arches "${version}" "default")"
7071

7172
echo "Tags: $(join ', ' "${versionAliases[@]}")"
7273
echo "Architectures: $(join ', ' "${supportedArches[@]}")"
@@ -76,19 +77,19 @@ for version in "${versions[@]}"; do
7677

7778
# Get supported variants according to the target architecture.
7879
# See details in function.sh
79-
IFS=' ' read -ra variants <<<"$(get_variants "$(dirname "$version")")"
80+
IFS=' ' read -ra variants <<<"$(get_variants "$(dirname "${version}")")"
8081
for variant in "${variants[@]}"; do
8182
# Skip non-docker directories
82-
[ -f "$version/$variant/Dockerfile" ] || continue
83+
[ -f "${version}/${variant}/Dockerfile" ] || continue
8384

84-
commit="$(fileCommit "$version/$variant")"
85+
commit="$(fileCommit "${version}/${variant}")"
8586

8687
slash='/'
8788
variantAliases=("${versionAliases[@]/%/-${variant//$slash/-}}")
8889
variantAliases=("${variantAliases[@]//latest-/}")
8990
# Get supported architectures for a specific version and variant.
9091
# See details in function.sh
91-
IFS=' ' read -ra supportedArches <<<"$(get_supported_arches "$version" "$variant")"
92+
IFS=' ' read -ra supportedArches <<<"$(get_supported_arches "${version}" "${variant}")"
9293

9394
echo "Tags: $(join ', ' "${variantAliases[@]}")"
9495
echo "Architectures: $(join ', ' "${supportedArches[@]}")"

0 commit comments

Comments
 (0)