Skip to content

Commit 573b983

Browse files
committed
Fix all shellcheck warnings
1 parent 3855ec9 commit 573b983

File tree

2 files changed

+41
-38
lines changed

2 files changed

+41
-38
lines changed

msync

Lines changed: 38 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ usage() {
1919
--rsync-option Can be repeated
2020
2121
Examples:
22-
$0 --verbse --dry-run rsync://rsync.samba.org/rsyncftp/ $PWD/rsync/
22+
$0 --verbse --dry-run rsync://rsync.samba.org/rsyncftp/ ${PWD}/rsync/
2323
$0 --rsync-option '--bwlimit=5m' rsync://foo.bar/archlinux /srv/mirror/archlinux
2424
$0 rsync://foo.bar/archlinux /srv/mirror/archlinux --last-update-url=http://foo.bar/archlinux/lastupdate
2525
USAGE
@@ -38,18 +38,18 @@ rsync_cmd() {
3838
cmd+=(--delete-delay --delay-updates "--temp-dir=${temp_dir}")
3939
cmd+=("--contimeout=60" "--timeout=600")
4040

41-
test -z "$verbose" || cmd+=(--verbose --progress --stats)
41+
[[ -z "${verbose}" ]] || cmd+=(--verbose --progress --stats)
4242

4343
cmd=("${cmd[@]}" "${rsync_options[@]}")
44-
cmd+=("$source")
44+
cmd+=("${source}")
4545

46-
test -z "$destination" || cmd+=("$destination")
46+
[[ -z "${destination}" ]] || cmd+=("${destination}")
4747

4848
verbose "About to run:" "${cmd[@]}"
4949
verbose ""
5050

5151
set +e
52-
if [ "$verbose" ]; then
52+
if [[ -n "${verbose}" ]]; then
5353
"${cmd[@]}"
5454
exit_status=$?
5555
else
@@ -59,21 +59,21 @@ rsync_cmd() {
5959
set -e
6060

6161
verbose ""
62-
verbose "rsync exited with $exit_status"
62+
verbose "rsync exited with ${exit_status}"
6363

64-
if [[ -z "$verbose" && $exit_status -ne 0 && $exit_status -ne 23 && $exit_status -ne 24 ]]; then
65-
echo "$output"
64+
if [[ -z "${verbose}" && ${exit_status} -ne 0 && ${exit_status} -ne 23 && ${exit_status} -ne 24 ]]; then
65+
echo "${output}"
6666
fi
6767

68-
# Treat partial transfers as non-error
69-
[ $exit_status -ne 23 ] || exit_status=0
70-
[ $exit_status -ne 24 ] || exit_status=0
68+
# Treat partial transfers as non-errors
69+
[[ "${exit_status}" -ne 23 ]] || exit_status=0
70+
[[ "${exit_status}" -ne 24 ]] || exit_status=0
7171

72-
return $exit_status
72+
return "${exit_status}"
7373
}
7474

7575
verbose() {
76-
test -z "$verbose" || warn "$@"
76+
[[ -z "${verbose}" ]] || warn "$@"
7777
}
7878

7979
warn() {
@@ -98,7 +98,7 @@ declare -a rsync_options
9898

9999
# PARSE OPTIONS
100100

101-
[ $# -ge 1 ] || usage
101+
[[ $# -ge 1 ]] || usage
102102

103103
while true; do
104104
case ${1:-} in
@@ -120,19 +120,19 @@ while true; do
120120
esac
121121
done
122122

123-
id=$(echo "$destination_path" | tr '/' '-')
123+
id=$(echo "${destination_path}" | tr '/' '-')
124124
id=${id#-} # trim prefix
125125
id=${id%-} # trim suffix
126-
[ "$id" ] || id="temp"
126+
[[ -n "${id}" ]] || id="temp"
127127

128128
# Directory where files are downloaded to before being moved in place.
129129
# This should be on the same filesystem as $destination_path, but not a
130130
# subdirectory of $destination_path.
131131
temp_dir="${temporary_directory}/msync-${id}"
132132
lock_file="${temporary_directory}/msync-${id}.lck"
133-
mkdir -p "$(dirname $lock_file)"
133+
mkdir -p "$(dirname "${lock_file}")"
134134

135-
if [[ "$last_update_sync" && -z "$last_update_url" ]]; then
135+
if [[ -n "${last_update_sync}" && -z "${last_update_url}" ]]; then
136136
warn "--last-update-sync requires --last-update-url to be set."
137137
exit 1
138138
fi
@@ -144,33 +144,34 @@ verbose "Verbose mode is on"
144144
exec 9>"${lock_file}"
145145
flock -n 9 || { verbose "${lock_file} present, exiting."; exit 0; }
146146

147+
# shellcheck disable=SC2317
147148
exit_handler() {
148-
[ $SECONDS -le "$warning_timeout" ] || warn "msync took $SECONDS s"
149+
[[ "${SECONDS}" -le "${warning_timeout}" ]] || warn "msync took ${SECONDS} s"
149150

150-
[ ! -d $temp_dir ] || rmdir $temp_dir
151-
rm $lock_file
151+
[[ ! -d "${temp_dir}" ]] || rmdir "${temp_dir}"
152+
rm "${lock_file}"
152153
verbose "msync cleaned up successfully, bye."
153154
}
154155

155156
trap exit_handler EXIT
156157

157-
[ -z "$destination_path" ] || [ -d "${destination_path}" ] || mkdir -p "${destination_path}"
158-
[ -d "${temp_dir}" ] || mkdir -p "${temp_dir}"
158+
[[ -z "${destination_path}" ]] || [[ -d "${destination_path}" ]] || mkdir -p "${destination_path}"
159+
[[ -d "${temp_dir}" ]] || mkdir -p "${temp_dir}"
159160

160161
tty -s || {
161162
delay=$((RANDOM % random_delay))
162163
warning_timeout=$((warning_timeout + delay))
163-
sleep $delay
164+
sleep "${delay}"
164165
}
165166

166-
if [ "${skip_connection_check}" ]; then
167+
if [[ -n "${skip_connection_check}" ]]; then
167168
rsync_uri=${rsync_uris[0]}
168169
verbose "Skipping connection check and using ${rsync_uri}"
169170
else
170171
rsync_uri=""
171172
for uri in "${rsync_uris[@]}"; do
172-
if rsync --no-motd --contimeout=10 --timeout=30 "$uri" >/dev/null; then
173-
rsync_uri=$uri
173+
if rsync --no-motd --contimeout=10 --timeout=30 "${uri}" >/dev/null; then
174+
rsync_uri=${uri}
174175
verbose "${uri} is up, using it as upstream"
175176
break
176177
else
@@ -179,18 +180,18 @@ else
179180
done
180181
fi
181182

182-
if [ -z "$rsync_uri" ]; then
183+
if [[ -z "${rsync_uri}" ]]; then
183184
warn "Could not find any valid upstreams, aborting."
184185
exit 1
185186
fi
186187

187-
if [ "$last_update_url" ]; then
188-
last_update_file="$destination_path/${last_update_url##*/}"
188+
if [[ -n "${last_update_url}" ]]; then
189+
last_update_file="${destination_path}/${last_update_url##*/}"
189190

190-
if [ -f "$last_update_file" ]; then
191+
if [[ -f "${last_update_file}" ]]; then
191192
verbose "Checking whether ${last_update_url} is different than ${last_update_file}"
192193

193-
if diff -b <(curl -Ls "$last_update_url") "$last_update_file" >/dev/null; then
194+
if diff -b <(curl -Ls "${last_update_url}" || true) "${last_update_file}" >/dev/null; then
194195
verbose "Files are identical, doing partial sync"
195196
verbose "Syncing ${rsync_uri}${last_update_sync}"
196197

@@ -200,23 +201,23 @@ if [ "$last_update_url" ]; then
200201
verbose "Files are different, doing full sync"
201202
fi
202203
else
203-
verbose "$last_update_file missing, doing full sync"
204+
verbose "${last_update_file} missing, doing full sync"
204205
fi
205206
else
206207
verbose "--last-update-url unset, doing full sync"
207208
fi
208209

209210
set +e
210-
if [ "$verbose" ]; then
211+
if [[ -n "${verbose}" ]]; then
211212
rsync_cmd "${rsync_uri}" "${destination_path}"
212213
else
213214
output=$(rsync_cmd "${rsync_uri}" "${destination_path}")
214215
fi
215216
exit_status=$?
216217
set -e
217218

218-
if [[ -z "$verbose" && $exit_status -ne 0 ]]; then
219-
warn "$output"
219+
if [[ -z "${verbose}" && ${exit_status} -ne 0 ]]; then
220+
warn "${output}"
220221
fi
221222

222-
exit $exit_status
223+
exit "${exit_status}"

test.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ set -euxo pipefail
44

55
cd $(dirname $0)
66

7+
shellcheck -o all ./msync
8+
79
gem install bashcov -N
810

911
rm -rf coverage
1012

1113
bashcov -- ./msync --help || true
12-
bashcov -- ./msync rsync://rsync.cyberbits.eu/ruby/ /tmp/msync/ --dry-run --temporary-directory /tmp
14+
bashcov --mute -- ./msync rsync://rsync.cyberbits.eu/ruby/ /tmp/msync/ --dry-run --temporary-directory /tmp
1315
bashcov -- ./msync rsync://rsync.cyberbits.eu/archlinux/ /tmp/msync/ --dry-run --quiet --last-update-url https://mirror.cyberbits.eu/archlinux/lastupdate --last-update-sync lastsync --random-delay 1 --warning-timeout 30 --temporary-directory /tmp

0 commit comments

Comments
 (0)