Skip to content

Commit da2d7ca

Browse files
committed
Shellcheck stage_binaries.sh
1 parent eab3f83 commit da2d7ca

File tree

1 file changed

+32
-18
lines changed

1 file changed

+32
-18
lines changed

stage_binaries.sh

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ function stack() {
3737
echo -ne " from "
3838
fi
3939
indent="true"
40-
local file="$(basename "${BASH_SOURCE["${frame}"]}")"
40+
local file
41+
file="$(basename "${BASH_SOURCE["${frame}"]}")"
4142
local line="${BASH_LINENO["$((frame-1))"]}" # ???
4243
local func="${FUNCNAME["${frame}"]:-}"
4344
echo -e "${func}() ${file}:${line}"
@@ -58,7 +59,8 @@ function errexit() {
5859
stack 1 >&2 # skip this frame
5960

6061
# Exit, really, right now.
61-
local pgid="$(cat /proc/self/stat | awk '{print $5}')"
62+
local pgid
63+
pgid="$(awk '{print $5}' /proc/self/stat)"
6264
kill -- -"${pgid}"
6365
}
6466

@@ -133,22 +135,24 @@ function file_to_package() {
133135
fwd="${ROOT_FWD_LINKS[i]}"
134136
rev="${ROOT_REV_LINKS[i]}"
135137
if [[ "${file}" =~ ^"${fwd}/" ]]; then
136-
alt="$(echo "${file}" | sed "s|^${fwd}|${rev}|")"
138+
alt="${file/#"${fwd}"/"${rev}"}"
137139
break
138140
elif [[ "${file}" =~ ^"${rev}/" ]]; then
139-
alt="$(echo "${file}" | sed "s|^${rev}|${fwd}|")"
141+
alt="${file/#"${rev}"/"${fwd}"}"
140142
break
141143
fi
142-
i=$(($i+1))
144+
i=$((i+1))
143145
done
144146

145147
local out=""
146148
local result=""
147149
out="$(dpkg-query --search "${file}" 2>&1)"
150+
# shellcheck disable=SC2181
148151
if [[ $? == 0 ]]; then
149152
result="${out}"
150153
elif [[ -n "${alt}" ]]; then
151154
out="$(dpkg-query --search "${alt}" 2>&1)"
155+
# shellcheck disable=SC2181
152156
if [[ $? == 0 ]]; then
153157
result="${out}"
154158
fi
@@ -171,7 +175,8 @@ function ensure_dir_in_staging() {
171175
local dir="$2"
172176

173177
if [[ ! -e "${staging}/${dir}" ]]; then
174-
local rel="$(echo "${dir}" | sed 's|^/||')"
178+
# Stript the leading /
179+
local rel="${dir/#\//}"
175180
tar -C / -c --no-recursion --dereference "${rel}" | tar -C "${staging}" -x
176181
fi
177182
}
@@ -182,13 +187,15 @@ function stage_one_file() {
182187
local file="$2"
183188

184189
# copy the real form of the named path
185-
local real="$(realpath "${file}")"
190+
local real
191+
real="$(realpath "${file}")"
186192
cp -a --parents "${real}" "${staging}"
187193

188194
# recreate symlinks, even on intermediate path elements
189195
if [[ "${file}" != "${real}" ]]; then
190196
if [[ ! -e "${staging}/${file}" ]]; then
191-
local dir="$(dirname "${file}")"
197+
local dir
198+
dir="$(dirname "${file}")"
192199
ensure_dir_in_staging "${staging}" "${dir}"
193200
ln -s "${real}" "${staging}/${file}"
194201
fi
@@ -252,7 +259,8 @@ function stage_one_package() {
252259
local i=0
253260
for s in "${sums[@]}"; do
254261
if [[ "${sum}" == "${s}" ]]; then
255-
local dir="$(dirname "${file}")"
262+
local dir
263+
dir="$(dirname "${file}")"
256264
ensure_dir_in_staging "${staging}" "$(dirname "${file}")"
257265
ln -s "${names[$i]}" "${staging}/${file}"
258266
found="true"
@@ -296,15 +304,17 @@ function stage_packages() {
296304
local pkg
297305
for pkg; do
298306
echo "staging package ${pkg}"
299-
local du_before="$(du -sk "${staging}" | cut -f1)"
307+
local du_before
308+
du_before="$(du -sk "${staging}" | cut -f1)"
300309
indent apt-get -y -qq -o Dpkg::Use-Pty=0 --no-install-recommends install "${pkg}"
301310
stage_one_package "$staging" "${pkg}"
302311
while read -r dep; do
303312
DBG "staging dependent package ${dep}"
304313
indent stage_one_package "${staging}" "${dep}"
305314
done < <( get_dependent_packages "${pkg}" )
306-
local du_after="$(du -sk "${staging}" | cut -f1)"
307-
indent echo "package ${pkg} size: +$(( $du_after - $du_before )) kB (of ${du_after} kB)"
315+
local du_after
316+
du_after="$(du -sk "${staging}" | cut -f1)"
317+
indent echo "package ${pkg} size: +$(( du_after - du_before )) kB (of ${du_after} kB)"
308318
done
309319
}
310320

@@ -355,10 +365,12 @@ function stage_binaries() {
355365
local bin
356366
for bin; do
357367
echo "staging binary ${bin}"
358-
local du_before="$(du -sk "${staging}" | cut -f1)"
368+
local du_before
369+
du_before="$(du -sk "${staging}" | cut -f1)"
359370
stage_one_binary "${staging}" "${bin}"
360-
local du_after="$(du -sk "${staging}" | cut -f1)"
361-
indent echo "binary ${bin} size: +$(( $du_after - $du_before )) kB (of ${du_after} kB)"
371+
local du_after
372+
du_after="$(du -sk "${staging}" | cut -f1)"
373+
indent echo "binary ${bin} size: +$(( du_after - du_before )) kB (of ${du_after} kB)"
362374
done
363375
}
364376

@@ -369,10 +381,12 @@ function stage_files() {
369381
local bin
370382
for file; do
371383
echo "staging file ${file}"
372-
local du_before="$(du -sk "${staging}" | cut -f1)"
384+
local du_before
385+
du_before="$(du -sk "${staging}" | cut -f1)"
373386
stage_one_file "${staging}" "${file}"
374-
local du_after="$(du -sk "${staging}" | cut -f1)"
375-
indent echo "file ${file} size: +$(( $du_after - $du_before )) kB (of ${du_after} kB)"
387+
local du_after
388+
du_after="$(du -sk "${staging}" | cut -f1)"
389+
indent echo "file ${file} size: +$(( du_after - du_before )) kB (of ${du_after} kB)"
376390
done
377391
}
378392

0 commit comments

Comments
 (0)