Merge ~cjwatson/launchpad:improve-shell-quoting into launchpad:master

Proposed by Colin Watson
Status: Merged
Approved by: Colin Watson
Approved revision: 2809101db8e0f0ebebed443d5ce4aee1848f8f29
Merge reported by: Otto Co-Pilot
Merged at revision: not available
Proposed branch: ~cjwatson/launchpad:improve-shell-quoting
Merge into: launchpad:master
Diff against target: 287 lines (+54/-50)
10 files modified
cronscripts/nightly.sh (+20/-20)
cronscripts/publishing/cron.daily-ppa (+3/-3)
cronscripts/publishing/cron.publish-copy-archives (+1/-1)
cronscripts/publishing/cron.publish-ppa (+9/-5)
scripts/update-version-info.sh (+1/-1)
utilities/find-changed-files.sh (+2/-2)
utilities/launchpad-database-setup (+2/-2)
utilities/rocketfuel-get (+2/-2)
utilities/rocketfuel-setup (+13/-13)
utilities/start-dev-soyuz.sh (+1/-1)
Reviewer Review Type Date Requested Status
Jürgen Gmach Approve
Review via email: mp+417099@code.launchpad.net

Commit message

Improve quoting in shell scripts

Description of the change

In shell, $-expansions should normally be double-quoted to prevent word splitting being applied to the result of the expansion, except in cases where word splitting is specifically needed. `shellcheck` complained about a number of cases where we were failing to do this, so fix them.

To post a comment you must log in.
Revision history for this message
Jürgen Gmach (jugmac00) wrote :

I may miss the reason why you do not add the pre-commit hook for shellcheck with this commit?

If we do not enforce shellcheck, there is always the chance that bad code gets added again.

review: Approve
Revision history for this message
Colin Watson (cjwatson) wrote :

I'm working up to adding the shellcheck pre-commit hook, but there are some other things that need to be fixed first - this is part of a series.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/cronscripts/nightly.sh b/cronscripts/nightly.sh
2index 9c4a674..78cf8e9 100755
3--- a/cronscripts/nightly.sh
4+++ b/cronscripts/nightly.sh
5@@ -14,38 +14,38 @@ LOGFILE=$LOGDIR/nightly.log
6 LOCK=/var/lock/launchpad_nightly.lock
7 lockfile -r0 -l 259200 $LOCK
8 if [ $? -ne 0 ]; then
9- echo $(date): Unable to grab $LOCK lock - aborting | tee -a $LOGFILE
10+ echo "$(date): Unable to grab $LOCK lock - aborting" | tee -a "$LOGFILE"
11 ps fuxwww
12 exit 1
13 fi
14
15-cd `dirname $0`
16+cd "$(dirname "$0")"
17
18-echo $(date): Grabbed lock >> $LOGFILE
19+echo "$(date): Grabbed lock" >> "$LOGFILE"
20
21-echo $(date): Expiring memberships >> $LOGFILE
22-./flag-expired-memberships.py -q --log-file=DEBUG:$LOGDIR/flag-expired-memberships.log
23+echo "$(date): Expiring memberships" >> "$LOGFILE"
24+./flag-expired-memberships.py -q --log-file=DEBUG:"$LOGDIR/flag-expired-memberships.log"
25
26-echo $(date): Allocating revision karma >> $LOGFILE
27-./allocate-revision-karma.py -q --log-file=DEBUG:$LOGDIR/allocate-revision-karma.log
28+echo "$(date): Allocating revision karma" >> "$LOGFILE"
29+./allocate-revision-karma.py -q --log-file=DEBUG:"$LOGDIR/allocate-revision-karma.log"
30
31-echo $(date): Recalculating karma >> $LOGFILE
32-./foaf-update-karma-cache.py -q --log-file=INFO:$LOGDIR/foaf-update-karma-cache.log
33+echo "$(date): Recalculating karma" >> "$LOGFILE"
34+./foaf-update-karma-cache.py -q --log-file=INFO:"$LOGDIR/foaf-update-karma-cache.log"
35
36-echo $(date): Updating cached statistics >> $LOGFILE
37-./update-stats.py -q --log-file=DEBUG:$LOGDIR/update-stats.log
38+echo "$(date): Updating cached statistics" >> "$LOGFILE"
39+./update-stats.py -q --log-file=DEBUG:"$LOGDIR/update-stats.log"
40
41-echo $(date): Expiring questions >> $LOGFILE
42-./expire-questions.py -q --log-file=DEBUG:$LOGDIR/expire-questions.log
43+echo "$(date): Expiring questions" >> "$LOGFILE"
44+./expire-questions.py -q --log-file=DEBUG:"$LOGDIR/expire-questions.log"
45
46-echo $(date): Updating bugtask target name caches >> $LOGFILE
47-./update-bugtask-targetnamecaches.py -q --log-file=DEBUG:$LOGDIR/update-bugtask-targetnamecaches.log
48+echo "$(date): Updating bugtask target name caches" >> "$LOGFILE"
49+./update-bugtask-targetnamecaches.py -q --log-file=DEBUG:"$LOGDIR/update-bugtask-targetnamecaches.log"
50
51-echo $(date): Updating personal standings >> $LOGFILE
52-./update-standing.py -q --log-file=DEBUG:$LOGDIR/update-standing.log
53+echo "$(date): Updating personal standings" >> "$LOGFILE"
54+./update-standing.py -q --log-file=DEBUG:"$LOGDIR/update-standing.log"
55
56-echo $(date): Updating CVE database >> $LOGFILE
57-./update-cve.py -q --log-file=DEBUG:$LOGDIR/update-cve.log
58+echo "$(date): Updating CVE database" >> "$LOGFILE"
59+./update-cve.py -q --log-file=DEBUG:"$LOGDIR/update-cve.log"
60
61-echo $(date): Removing lock >> $LOGFILE
62+echo "$(date): Removing lock" >> "$LOGFILE"
63 rm -f $LOCK
64diff --git a/cronscripts/publishing/cron.daily-ppa b/cronscripts/publishing/cron.daily-ppa
65index 20b1d74..e95937e 100755
66--- a/cronscripts/publishing/cron.daily-ppa
67+++ b/cronscripts/publishing/cron.daily-ppa
68@@ -10,8 +10,8 @@ set -x
69 LOCKFILEOPTIONS="-r-1"
70
71 # Variables, lockfile and exit handler for PPA scripts.
72-source `dirname $0`/cron.base-ppa.sh
73+source "$(dirname "$0")/cron.base-ppa.sh"
74
75 # Clear out empty and thus redundant dirs.
76-find $PPAROOT -type d -empty | xargs -r rmdir
77-find $P3AROOT -type d -empty | xargs -r rmdir
78+find "$PPAROOT" -type d -empty | xargs -r rmdir
79+find "$P3AROOT" -type d -empty | xargs -r rmdir
80diff --git a/cronscripts/publishing/cron.publish-copy-archives b/cronscripts/publishing/cron.publish-copy-archives
81index 7e6cad0..5c35e56 100755
82--- a/cronscripts/publishing/cron.publish-copy-archives
83+++ b/cronscripts/publishing/cron.publish-copy-archives
84@@ -5,7 +5,7 @@
85
86 # LPCONFIG will come from the environment so this script can run unaltered
87 # on dogfood.
88-if [ -z $LPCONFIG ]; then
89+if [ -z "$LPCONFIG" ]; then
90 echo LPCONFIG must be set to run this script.
91 exit 1
92 fi
93diff --git a/cronscripts/publishing/cron.publish-ppa b/cronscripts/publishing/cron.publish-ppa
94index 81ed9b1..7c93542 100755
95--- a/cronscripts/publishing/cron.publish-ppa
96+++ b/cronscripts/publishing/cron.publish-ppa
97@@ -6,12 +6,16 @@
98 set -x
99
100 # Variables, lockfile and exit handler for PPA scripts.
101-source `dirname $0`/cron.base-ppa.sh
102+source "$(dirname "$0")/cron.base-ppa.sh"
103
104-LPCURRENT=`dirname $0`/../..
105+LPCURRENT="$(dirname "$0")/../.."
106
107 for DISTRO_ARG in "$@"; do
108- $LPCURRENT/scripts/process-accepted.py -v --ppa $DISTRO_ARG
109- $LPCURRENT/scripts/publish-distro.py -v --ppa $DISTRO_ARG
110- $LPCURRENT/scripts/publish-distro.py -v --private-ppa $DISTRO_ARG
111+ # We intentionally allow word-splitting of $DISTRO_ARG here.
112+ # shellcheck disable=SC2086
113+ "$LPCURRENT/scripts/process-accepted.py" -v --ppa $DISTRO_ARG
114+ # shellcheck disable=SC2086
115+ "$LPCURRENT/scripts/publish-distro.py" -v --ppa $DISTRO_ARG
116+ # shellcheck disable=SC2086
117+ "$LPCURRENT/scripts/publish-distro.py" -v --private-ppa $DISTRO_ARG
118 done
119diff --git a/scripts/update-version-info.sh b/scripts/update-version-info.sh
120index 46c5d7c..76f0c20 100755
121--- a/scripts/update-version-info.sh
122+++ b/scripts/update-version-info.sh
123@@ -13,7 +13,7 @@ if [ ! -e .git ]; then
124 exit 1
125 fi
126
127-if ! which git > /dev/null || ! test -x $(which git); then
128+if ! which git > /dev/null || ! test -x "$(which git)"; then
129 echo "No working 'git' executable found" >&2
130 exit 1
131 fi
132diff --git a/utilities/find-changed-files.sh b/utilities/find-changed-files.sh
133index bdaedd2..530138e 100755
134--- a/utilities/find-changed-files.sh
135+++ b/utilities/find-changed-files.sh
136@@ -15,7 +15,7 @@ if [ ! -e .git ]; then
137 fi
138
139 git_diff_files() {
140- git diff --name-only -z $@ | perl -l -0 -ne '
141+ git diff --name-only -z "$@" | perl -l -0 -ne '
142 # Only show paths that exist and are not symlinks.
143 print if -e and not -l'
144 }
145@@ -28,4 +28,4 @@ if [ -z "$files" ]; then
146 files=$(git_diff_files "${1:-master}")
147 fi
148
149-echo $files
150+echo "$files"
151diff --git a/utilities/launchpad-database-setup b/utilities/launchpad-database-setup
152index 83b79b4..729f941 100755
153--- a/utilities/launchpad-database-setup
154+++ b/utilities/launchpad-database-setup
155@@ -97,8 +97,8 @@ fi
156 echo Waiting 10 seconds for postgresql to come up...
157 sleep 10
158
159-echo Creating postgresql user $USER
160-sudo -u postgres /usr/lib/postgresql/$pgversion/bin/createuser -a -d $USER
161+echo "Creating postgresql user $USER"
162+sudo -u postgres /usr/lib/postgresql/$pgversion/bin/createuser -a -d "$USER"
163
164 echo
165 echo Looks like everything went ok.
166diff --git a/utilities/rocketfuel-get b/utilities/rocketfuel-get
167index 7335f4a..c41416e 100755
168--- a/utilities/rocketfuel-get
169+++ b/utilities/rocketfuel-get
170@@ -30,7 +30,7 @@ fi
171
172 LP_DOWNLOAD_CACHE_PATH="$LP_PROJECT_ROOT/$LP_SOURCEDEPS_DIR/download-cache"
173 YUI_PATH="$LP_PROJECT_ROOT/$LP_SOURCEDEPS_DIR/yui"
174-LP_DOWNLOAD_CACHE_PATH="$(eval echo ${LP_DOWNLOAD_CACHE_PATH})"
175+LP_DOWNLOAD_CACHE_PATH="$(eval echo "$LP_DOWNLOAD_CACHE_PATH")"
176
177 # Pull launchpad devel from launchpad.
178 INITIAL_REV=$(git -C "$LP_TRUNK_PATH" rev-parse HEAD)
179@@ -94,7 +94,7 @@ find_branches_to_relink | filter_branches_to_relink | relink_branches
180
181
182 # Build launchpad if there were changes.
183-if [ $FINAL_REV != $INITIAL_REV ];
184+if [ "$FINAL_REV" != "$INITIAL_REV" ]
185 then
186 make -C "$LP_TRUNK_PATH"
187 fi
188diff --git a/utilities/rocketfuel-setup b/utilities/rocketfuel-setup
189index 0e003d5..78be81e 100755
190--- a/utilities/rocketfuel-setup
191+++ b/utilities/rocketfuel-setup
192@@ -33,12 +33,12 @@ while :; do
193 esac
194 done
195
196-if [ -z ${lpusername} ]; then
197+if [ -z "$lpusername" ]; then
198 # Establish LP username
199 whoami=`whoami`
200 printf "What is your Launchpad username? [$whoami] "
201 read lpusername
202- if [ -z ${lpusername} ]; then
203+ if [ -z "$lpusername" ]; then
204 lpusername=${whoami}
205 fi
206 fi
207@@ -134,10 +134,10 @@ for key in $REQUIRED_PPA_KEYS; do
208 done
209
210 do_install() {
211- dpkg -s $pkg | grep -q "^Status: install ok installed"
212+ dpkg -s "$pkg" | grep -q "^Status: install ok installed"
213 if [ $? -ne 0 ]; then
214 echo "Installing $pkg package..."
215- sudo apt-get install $INSTALL_OPTS $pkg
216+ sudo apt-get install $INSTALL_OPTS "$pkg"
217 if [ $? -ne 0 ]; then
218 echo "Unable to install $pkg."
219 exit 1
220@@ -249,11 +249,11 @@ fi
221
222
223 # Create the local branch structure we will use for managing Launchpad code
224-mkdir -p $LP_PROJECT_ROOT
225-cd $LP_PROJECT_ROOT
226+mkdir -p "$LP_PROJECT_ROOT"
227+cd "$LP_PROJECT_ROOT"
228
229 echo "Logging bzr into Launchpad (it's okay if this errors)..."
230-bzr launchpad-login $lpusername
231+bzr launchpad-login "$lpusername"
232 if [ $? -ne 0 ]; then
233 echo ""
234 echo "You can ignore any error above about not registering an SSH key"
235@@ -275,17 +275,17 @@ if [ "$(git ls-remote --get-url lp:launchpad)" = lp:launchpad ]; then
236 fi
237 fi
238
239-cd $LP_PROJECT_ROOT
240-if [ ! -d $LP_TRUNK_NAME ]; then
241+cd "$LP_PROJECT_ROOT"
242+if [ ! -d "$LP_TRUNK_NAME" ]; then
243 echo "Making local clone of Launchpad; this may take a while..."
244- git clone lp:launchpad $LP_TRUNK_NAME
245+ git clone lp:launchpad "$LP_TRUNK_NAME"
246 if [ $? -ne 0 ]; then
247 echo "ERROR: Unable to create local clone of Launchpad"
248 exit 1
249 fi
250 fi
251
252-cd $LP_TRUNK_NAME
253+cd "$LP_TRUNK_NAME"
254 git status
255 if [ $? -ne 0 ]; then
256 echo "ERROR: Your clone in $LP_TRUNK_PATH is corrupted.
257@@ -313,7 +313,7 @@ fi
258 # Set up scripts in /usr/local/bin
259 cd /usr/local/bin
260 if [ ! -e rocketfuel-get ]; then
261- sudo ln -s $LP_TRUNK_PATH/utilities/rocketfuel-get
262+ sudo ln -s "$LP_TRUNK_PATH/utilities/rocketfuel-get"
263 fi
264 ls -l rocketfuel-get | cut -d">" -f2 | grep -q "$LP_TRUNK_NAME\/utilities\/rocketfuel"
265 if [ $? -ne 0 ]; then
266@@ -321,7 +321,7 @@ if [ $? -ne 0 ]; then
267 recreate it."
268 fi
269 if [ ! -e rocketfuel-setup ]; then
270- sudo ln -s $LP_TRUNK_PATH/utilities/rocketfuel-setup
271+ sudo ln -s "$LP_TRUNK_PATH/utilities/rocketfuel-setup"
272 fi
273 ls -l rocketfuel-setup | cut -d">" -f2 | grep -q "$LP_TRUNK_NAME\/utilities\/rocketfuel"
274 if [ $? -ne 0 ]; then
275diff --git a/utilities/start-dev-soyuz.sh b/utilities/start-dev-soyuz.sh
276index efe4b0f..2517b5a 100755
277--- a/utilities/start-dev-soyuz.sh
278+++ b/utilities/start-dev-soyuz.sh
279@@ -11,7 +11,7 @@ start_twistd() {
280 bin/twistd \
281 --logfile "/var/tmp/development-$name.log" \
282 --pidfile "/var/tmp/development-$name.pid" \
283- -y "$tac" $@
284+ -y "$tac" "$@"
285 }
286
287 start_twistd_plugin() {

Subscribers

People subscribed via source and target branches

to status/vote changes: