Skip to content

Commit 8564976

Browse files
kojiromikeclaude
andauthored
Fix shellcheck issues across shell scripts (#467)
Resolved the following shellcheck warnings and errors: - SC2006: Use $(...) instead of legacy `...` backticks - SC2016: Expressions don't expand in single quotes (disabled where intentional) - SC2029: Note that client-side expansion happens (disabled where intentional) - SC2086: Double quote to prevent globbing and word splitting - SC2162: read without -r will mangle backslashes - SC2001: See if you can use ${variable//search/replace} instead of sed - SC2004: $/${} is unnecessary on arithmetic variables - SC2143: Use grep -q instead of comparing output - SC2009: Consider using pgrep instead of ps|grep - SC2181: Check exit code directly with e.g. 'if mycmd;', not '$?' - SC2034: Variable appears unused (verify it or export if used externally) - SC2119: Use function_name "$@" if function's $1 should mean script's $1 - SC2120: Function references arguments, but none are ever passed - SC2154: Variable is referenced but not assigned - SC2015: Note that A && B || C is not if-then-else - SC2166: Prefer [ p ] && [ q ] as [ p -a q ] is not well defined - SC2003: expr is antiquated. Consider rewriting this with $((..)) - SC2103: Use a ( subshell ) to avoid having to cd back Fixed issues in: - utilities/openemr-env-migrator/openemr-env-migrator - utilities/openemr-env-installer/openemr-env-installer - utilities/openemr-monitor/monitor-installer - docker/openemr/7.0.3/utilities/devtoolsLibrary.source - docker/openemr/7.0.4/utilities/devtoolsLibrary.source 🤖 Generated with [Claude Code](https://claude.ai/code) Co-authored-by: Claude <[email protected]>
1 parent ffbd96e commit 8564976

File tree

5 files changed

+163
-90
lines changed

5 files changed

+163
-90
lines changed

docker/openemr/7.0.3/utilities/devtoolsLibrary.source

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,18 @@
11
#!/bin/sh
22

33
prepareVariables() {
4+
# Set default values for environment variables if not provided
5+
: "${MYSQL_DATABASE:=''}" \
6+
"${MYSQL_HOST:=localhost}" \
7+
"${MYSQL_PASS:=''}" \
8+
"${MYSQL_PORT:=''}" \
9+
"${MYSQL_ROOT_PASS:=''}" \
10+
"${MYSQL_ROOT_USER:=''}" \
11+
"${MYSQL_USER:=''}" \
12+
"${OE_PASS:=''}" \
13+
"${OE_USER:=''}" \
14+
"${SQL_DATA_DRIVE:=''}"
15+
416
CONFIGURATION="server=${MYSQL_HOST} rootpass=${MYSQL_ROOT_PASS} loginhost=%"
517
if [ "${MYSQL_ROOT_USER}" != "" ]; then
618
CONFIGURATION="${CONFIGURATION} root=${MYSQL_ROOT_USER}"
@@ -96,8 +108,8 @@ upgradeOpenEMR() {
96108

97109
sqlDataDrive() {
98110
echo "Installing sql data from drive"
99-
if [ "${SQL_DATA_DRIVE}" != "" ]; then
100-
cd "${SQL_DATA_DRIVE}"
111+
if [ -d "${SQL_DATA_DRIVE}" ]; then
112+
cd "${SQL_DATA_DRIVE}" || exit
101113
#Loop over all sql files inside of the current directory
102114
for f in *.sql ; do
103115
echo "Loading sql data from ${f}"
@@ -112,14 +124,14 @@ backupOpenemr() {
112124
mysqldump --ignore-table="${CUSTOM_DATABASE}".onsite_activity_view --hex-blob -u "${CUSTOM_ROOT_USER}" --password="${MYSQL_ROOT_PASS}" -h "${MYSQL_HOST}" -P "${CUSTOM_PORT}" "${CUSTOM_DATABASE}" > "/snapshots/${1}/backup.sql"
113125
rsync --delete --recursive --links /var/www/localhost/htdocs/openemr/sites "/snapshots/${1}/"
114126
rsync --delete --recursive --links /couchdb/data "/snapshots/${1}/"
115-
cd /snapshots
127+
cd /snapshots || exit
116128
tar -czf "${1}.tgz" "${1}"
117129
rm -fr "/snapshots/${1}"
118130
}
119131

120132
# parameter 1 is identifier
121133
restoreOpenemr() {
122-
cd /snapshots
134+
cd /snapshots || exit
123135
tar -xzf "${1}.tgz"
124136
# need to empty the database before the restore database import
125137
mysqldump -u "${CUSTOM_ROOT_USER}" --password="${MYSQL_ROOT_PASS}" -h "${MYSQL_HOST}" -P "${CUSTOM_PORT}" --add-drop-table --no-data "${CUSTOM_DATABASE}" | grep ^DROP | awk ' BEGIN { print "SET FOREIGN_KEY_CHECKS=0;" } { print $0 } END { print "SET FOREIGN_KEY_CHECKS=1;" } ' | mysql -u "${CUSTOM_ROOT_USER}" --password="${MYSQL_ROOT_PASS}" -h "${MYSQL_HOST}" -P "${CUSTOM_PORT}" "${CUSTOM_DATABASE}"
@@ -138,19 +150,27 @@ restoreOpenemr() {
138150
# parameter 1 is character set
139151
# parameter 2 is collation
140152
changeEncodingCollation() {
141-
mysql -u "${CUSTOM_ROOT_USER}" --password="${MYSQL_ROOT_PASS}" -h "${MYSQL_HOST}" -P "${CUSTOM_PORT}" -e 'SELECT concat("ALTER DATABASE `",TABLE_SCHEMA,"` CHARACTER SET = '"${1}"' COLLATE = '"${2}"';") as _sql FROM `information_schema`.`TABLES` where `TABLE_SCHEMA` like "'"${CUSTOM_DATABASE}"'" and `TABLE_TYPE`="BASE TABLE" group by `TABLE_SCHEMA`;' | egrep '^ALTER' | mysql -u "${CUSTOM_ROOT_USER}" --password="${MYSQL_ROOT_PASS}" -h "${MYSQL_HOST}" -P "${CUSTOM_PORT}"
142-
mysql -u "${CUSTOM_ROOT_USER}" --password="${MYSQL_ROOT_PASS}" -h "${MYSQL_HOST}" -P "${CUSTOM_PORT}" -e 'SELECT concat("ALTER TABLE `",TABLE_SCHEMA,"`.`",TABLE_NAME,"` CONVERT TO CHARACTER SET '"${1}"' COLLATE '"${2}"';") as _sql FROM `information_schema`.`TABLES` where `TABLE_SCHEMA` like "'"${CUSTOM_DATABASE}"'" and `TABLE_TYPE`="BASE TABLE" group by `TABLE_SCHEMA`, `TABLE_NAME`;' | egrep '^ALTER' | mysql -u "${CUSTOM_ROOT_USER}" --password="${MYSQL_ROOT_PASS}" -h "${MYSQL_HOST}" -P "${CUSTOM_PORT}"
153+
# Single quotes intentionally used for SQL syntax - variables expand on server side
154+
# shellcheck disable=SC2016
155+
mysql -u "${CUSTOM_ROOT_USER}" --password="${MYSQL_ROOT_PASS}" -h "${MYSQL_HOST}" -P "${CUSTOM_PORT}" -e 'SELECT concat("ALTER DATABASE `",TABLE_SCHEMA,"` CHARACTER SET = '"${1}"' COLLATE = '"${2}"';") as _sql FROM `information_schema`.`TABLES` where `TABLE_SCHEMA` like "'"${CUSTOM_DATABASE}"'" and `TABLE_TYPE`="BASE TABLE" group by `TABLE_SCHEMA`;' | grep -E '^ALTER' | mysql -u "${CUSTOM_ROOT_USER}" --password="${MYSQL_ROOT_PASS}" -h "${MYSQL_HOST}" -P "${CUSTOM_PORT}"
156+
# Single quotes intentionally used for SQL syntax - variables expand on server side
157+
# shellcheck disable=SC2016
158+
mysql -u "${CUSTOM_ROOT_USER}" --password="${MYSQL_ROOT_PASS}" -h "${MYSQL_HOST}" -P "${CUSTOM_PORT}" -e 'SELECT concat("ALTER TABLE `",TABLE_SCHEMA,"`.`",TABLE_NAME,"` CONVERT TO CHARACTER SET '"${1}"' COLLATE '"${2}"';") as _sql FROM `information_schema`.`TABLES` where `TABLE_SCHEMA` like "'"${CUSTOM_DATABASE}"'" and `TABLE_TYPE`="BASE TABLE" group by `TABLE_SCHEMA`, `TABLE_NAME`;' | grep -E '^ALTER' | mysql -u "${CUSTOM_ROOT_USER}" --password="${MYSQL_ROOT_PASS}" -h "${MYSQL_HOST}" -P "${CUSTOM_PORT}"
143159
}
144160

145161
forceHttps() {
146162
sed -i 's@#RewriteEngine On@ RewriteEngine On@' /etc/apache2/conf.d/openemr.conf
147163
sed -i 's@#RewriteCond %{HTTPS} off@ RewriteCond %{HTTPS} off@' /etc/apache2/conf.d/openemr.conf
164+
# Single quotes intentionally used for sed replacement - $1 is a sed backreference
165+
# shellcheck disable=SC2016
148166
sed -i 's@#RewriteRule (\.\*) https://%{HTTP_HOST}/\$1 \[R,L\]@ RewriteRule (.*) https://%{HTTP_HOST}/$1 [R,L]@' /etc/apache2/conf.d/openemr.conf
149167
}
150168

151169
unForceHttps() {
152170
sed -i 's@[^#]RewriteEngine On@#RewriteEngine On@' /etc/apache2/conf.d/openemr.conf
153171
sed -i 's@[^#]RewriteCond %{HTTPS} off@#RewriteCond %{HTTPS} off@' /etc/apache2/conf.d/openemr.conf
172+
# Single quotes intentionally used for sed replacement - $1 is a sed backreference
173+
# shellcheck disable=SC2016
154174
sed -i 's@[^#]RewriteRule (\.\*) https://%{HTTP_HOST}/\$1 \[R,L\]@#RewriteRule (.*) https://%{HTTP_HOST}/$1 [R,L]@' /etc/apache2/conf.d/openemr.conf
155175
}
156176

@@ -160,7 +180,7 @@ setupClientCert() {
160180
mkdir -p "/certs/${1}"
161181
fi
162182
cp "/certs/${1}.zip" "/certs/${1}/"
163-
cd "/certs/${1}"
183+
cd "/certs/${1}" || exit
164184
unzip "${1}.zip"
165185
# server certificate
166186
cp "/certs/${1}/Server.crt" /etc/ssl/certs/customclientbased.cert.pem
@@ -219,11 +239,11 @@ importRandomPatients() {
219239
apk update
220240
apk add openjdk11-jre
221241
mkdir /root/synthea
222-
cd /root/synthea
242+
cd /root/synthea || exit
223243
wget https://github.com/synthetichealth/synthea/releases/download/master-branch-latest/synthea-with-dependencies.jar
224244
fi
225245
rm -fr /root/synthea/output
226-
cd /root/synthea
246+
cd /root/synthea || exit
227247
java -jar synthea-with-dependencies.jar --exporter.fhir.export false --exporter.ccda.export true --generate.only_alive_patients true -p "$1"
228248
sed -i "s@exit;@//exit;@" /var/www/localhost/htdocs/openemr/contrib/util/ccda_import/import_ccda.php
229249
php /var/www/localhost/htdocs/openemr/contrib/util/ccda_import/import_ccda.php --sourcePath=/root/synthea/output/ccda --site=default --openemrPath=/var/www/localhost/htdocs/openemr --isDev="$2"
@@ -255,7 +275,7 @@ generateMultisiteBank() {
255275
find /var/www/localhost/htdocs/openemr/sites/${run}/documents -type d -print0 | xargs -0 chmod 700
256276
find /var/www/localhost/htdocs/openemr/sites/${run}/documents -type f -print0 | xargs -0 chmod 700
257277
echo "completed adding ${run} multisite"
258-
a=$(expr ${a} + 1)
278+
a=$((a + 1))
259279
done
260280
sed -i "s@//exit;@exit;@" /var/www/localhost/htdocs/openemr/contrib/util/installScripts/InstallerAuto.php
261281
echo "Completed setting up a multisite bank with following number of multisites (labeled run1, run2, run3, ...): run1...run${1}"

docker/openemr/7.0.4/utilities/devtoolsLibrary.source

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,18 @@
11
#!/bin/sh
22

33
prepareVariables() {
4+
# Set default values for environment variables if not provided
5+
: "${MYSQL_DATABASE:=''}" \
6+
"${MYSQL_HOST:=localhost}" \
7+
"${MYSQL_PASS:=''}" \
8+
"${MYSQL_PORT:=''}" \
9+
"${MYSQL_ROOT_PASS:=''}" \
10+
"${MYSQL_ROOT_USER:=''}" \
11+
"${MYSQL_USER:=''}" \
12+
"${OE_PASS:=''}" \
13+
"${OE_USER:=''}" \
14+
"${SQL_DATA_DRIVE:=''}"
15+
416
CONFIGURATION="server=${MYSQL_HOST} rootpass=${MYSQL_ROOT_PASS} loginhost=%"
517
if [ "${MYSQL_ROOT_USER}" != "" ]; then
618
CONFIGURATION="${CONFIGURATION} root=${MYSQL_ROOT_USER}"
@@ -96,10 +108,11 @@ upgradeOpenEMR() {
96108

97109
sqlDataDrive() {
98110
echo "Installing sql data from drive"
99-
if [ "${SQL_DATA_DRIVE}" != "" ]; then
100-
cd "${SQL_DATA_DRIVE}"
101-
#Loop over all sql files inside of the current directory
102-
for f in *.sql ; do
111+
if [ -d "${SQL_DATA_DRIVE}" ]; then
112+
cd "${SQL_DATA_DRIVE}" || exit
113+
# Loop over all sql files inside of the current directory
114+
for f in *.sql; do
115+
[ -f "${f}" ] || continue
103116
echo "Loading sql data from ${f}"
104117
mariadb --skip-ssl -u "${CUSTOM_ROOT_USER}" --password="${MYSQL_ROOT_PASS}" -h "${MYSQL_HOST}" -P "${CUSTOM_PORT}" "${CUSTOM_DATABASE}" < "${f}"
105118
done
@@ -112,14 +125,14 @@ backupOpenemr() {
112125
mariadb-dump --skip-ssl --ignore-table="${CUSTOM_DATABASE}".onsite_activity_view --hex-blob -u "${CUSTOM_ROOT_USER}" --password="${MYSQL_ROOT_PASS}" -h "${MYSQL_HOST}" -P "${CUSTOM_PORT}" "${CUSTOM_DATABASE}" > "/snapshots/${1}/backup.sql"
113126
rsync --delete --recursive --links /var/www/localhost/htdocs/openemr/sites "/snapshots/${1}/"
114127
rsync --delete --recursive --links /couchdb/data "/snapshots/${1}/"
115-
cd /snapshots
128+
cd /snapshots || exit
116129
tar -czf "${1}.tgz" "${1}"
117130
rm -fr "/snapshots/${1}"
118131
}
119132

120133
# parameter 1 is identifier
121134
restoreOpenemr() {
122-
cd /snapshots
135+
cd /snapshots || exit
123136
tar -xzf "${1}.tgz"
124137
# need to empty the database before the restore database import
125138
mariadb-dump --skip-ssl -u "${CUSTOM_ROOT_USER}" --password="${MYSQL_ROOT_PASS}" -h "${MYSQL_HOST}" -P "${CUSTOM_PORT}" --add-drop-table --no-data "${CUSTOM_DATABASE}" | grep ^DROP | awk ' BEGIN { print "SET FOREIGN_KEY_CHECKS=0;" } { print $0 } END { print "SET FOREIGN_KEY_CHECKS=1;" } ' | mariadb --skip-ssl -u "${CUSTOM_ROOT_USER}" --password="${MYSQL_ROOT_PASS}" -h "${MYSQL_HOST}" -P "${CUSTOM_PORT}" "${CUSTOM_DATABASE}"
@@ -138,19 +151,27 @@ restoreOpenemr() {
138151
# parameter 1 is character set
139152
# parameter 2 is collation
140153
changeEncodingCollation() {
141-
mariadb --skip-ssl -u "${CUSTOM_ROOT_USER}" --password="${MYSQL_ROOT_PASS}" -h "${MYSQL_HOST}" -P "${CUSTOM_PORT}" -e 'SELECT concat("ALTER DATABASE `",TABLE_SCHEMA,"` CHARACTER SET = '"${1}"' COLLATE = '"${2}"';") as _sql FROM `information_schema`.`TABLES` where `TABLE_SCHEMA` like "'"${CUSTOM_DATABASE}"'" and `TABLE_TYPE`="BASE TABLE" group by `TABLE_SCHEMA`;' | egrep '^ALTER' | mariadb --skip-ssl -u "${CUSTOM_ROOT_USER}" --password="${MYSQL_ROOT_PASS}" -h "${MYSQL_HOST}" -P "${CUSTOM_PORT}"
142-
mariadb --skip-ssl -u "${CUSTOM_ROOT_USER}" --password="${MYSQL_ROOT_PASS}" -h "${MYSQL_HOST}" -P "${CUSTOM_PORT}" -e 'SELECT concat("ALTER TABLE `",TABLE_SCHEMA,"`.`",TABLE_NAME,"` CONVERT TO CHARACTER SET '"${1}"' COLLATE '"${2}"';") as _sql FROM `information_schema`.`TABLES` where `TABLE_SCHEMA` like "'"${CUSTOM_DATABASE}"'" and `TABLE_TYPE`="BASE TABLE" group by `TABLE_SCHEMA`, `TABLE_NAME`;' | egrep '^ALTER' | mariadb --skip-ssl -u "${CUSTOM_ROOT_USER}" --password="${MYSQL_ROOT_PASS}" -h "${MYSQL_HOST}" -P "${CUSTOM_PORT}"
154+
# Single quotes intentionally used for SQL syntax - variables expand on server side
155+
# shellcheck disable=SC2016
156+
mariadb --skip-ssl -u "${CUSTOM_ROOT_USER}" --password="${MYSQL_ROOT_PASS}" -h "${MYSQL_HOST}" -P "${CUSTOM_PORT}" -e 'SELECT concat("ALTER DATABASE `",TABLE_SCHEMA,"` CHARACTER SET = '"${1}"' COLLATE = '"${2}"';") as _sql FROM `information_schema`.`TABLES` where `TABLE_SCHEMA` like "'"${CUSTOM_DATABASE}"'" and `TABLE_TYPE`="BASE TABLE" group by `TABLE_SCHEMA`;' | grep -E '^ALTER' | mariadb --skip-ssl -u "${CUSTOM_ROOT_USER}" --password="${MYSQL_ROOT_PASS}" -h "${MYSQL_HOST}" -P "${CUSTOM_PORT}"
157+
# Single quotes intentionally used for SQL syntax - variables expand on server side
158+
# shellcheck disable=SC2016
159+
mariadb --skip-ssl -u "${CUSTOM_ROOT_USER}" --password="${MYSQL_ROOT_PASS}" -h "${MYSQL_HOST}" -P "${CUSTOM_PORT}" -e 'SELECT concat("ALTER TABLE `",TABLE_SCHEMA,"`.`",TABLE_NAME,"` CONVERT TO CHARACTER SET '"${1}"' COLLATE '"${2}"';") as _sql FROM `information_schema`.`TABLES` where `TABLE_SCHEMA` like "'"${CUSTOM_DATABASE}"'" and `TABLE_TYPE`="BASE TABLE" group by `TABLE_SCHEMA`, `TABLE_NAME`;' | grep -E '^ALTER' | mariadb --skip-ssl -u "${CUSTOM_ROOT_USER}" --password="${MYSQL_ROOT_PASS}" -h "${MYSQL_HOST}" -P "${CUSTOM_PORT}"
143160
}
144161

145162
forceHttps() {
146163
sed -i 's@#RewriteEngine On@ RewriteEngine On@' /etc/apache2/conf.d/openemr.conf
147164
sed -i 's@#RewriteCond %{HTTPS} off@ RewriteCond %{HTTPS} off@' /etc/apache2/conf.d/openemr.conf
165+
# Single quotes intentionally used for sed replacement - $1 is a sed backreference
166+
# shellcheck disable=SC2016
148167
sed -i 's@#RewriteRule (\.\*) https://%{HTTP_HOST}/\$1 \[R,L\]@ RewriteRule (.*) https://%{HTTP_HOST}/$1 [R,L]@' /etc/apache2/conf.d/openemr.conf
149168
}
150169

151170
unForceHttps() {
152171
sed -i 's@[^#]RewriteEngine On@#RewriteEngine On@' /etc/apache2/conf.d/openemr.conf
153172
sed -i 's@[^#]RewriteCond %{HTTPS} off@#RewriteCond %{HTTPS} off@' /etc/apache2/conf.d/openemr.conf
173+
# Single quotes intentionally used for sed replacement - $1 is a sed backreference
174+
# shellcheck disable=SC2016
154175
sed -i 's@[^#]RewriteRule (\.\*) https://%{HTTP_HOST}/\$1 \[R,L\]@#RewriteRule (.*) https://%{HTTP_HOST}/$1 [R,L]@' /etc/apache2/conf.d/openemr.conf
155176
}
156177

@@ -160,7 +181,7 @@ setupClientCert() {
160181
mkdir -p "/certs/${1}"
161182
fi
162183
cp "/certs/${1}.zip" "/certs/${1}/"
163-
cd "/certs/${1}"
184+
cd "/certs/${1}" || exit
164185
unzip "${1}.zip"
165186
# server certificate
166187
cp "/certs/${1}/Server.crt" /etc/ssl/certs/customclientbased.cert.pem
@@ -219,11 +240,11 @@ importRandomPatients() {
219240
apk update
220241
apk add openjdk11-jre
221242
mkdir /root/synthea
222-
cd /root/synthea
243+
cd /root/synthea || exit
223244
wget https://github.com/synthetichealth/synthea/releases/download/master-branch-latest/synthea-with-dependencies.jar
224245
fi
225246
rm -fr /root/synthea/output
226-
cd /root/synthea
247+
cd /root/synthea || exit
227248
java -jar synthea-with-dependencies.jar --exporter.fhir.export false --exporter.ccda.export true --generate.only_alive_patients true -p "$1"
228249
sed -i "s@exit;@//exit;@" /var/www/localhost/htdocs/openemr/contrib/util/ccda_import/import_ccda.php
229250
php /var/www/localhost/htdocs/openemr/contrib/util/ccda_import/import_ccda.php --sourcePath=/root/synthea/output/ccda --site=default --openemrPath=/var/www/localhost/htdocs/openemr --isDev="$2"
@@ -255,7 +276,7 @@ generateMultisiteBank() {
255276
find /var/www/localhost/htdocs/openemr/sites/${run}/documents -type d -print0 | xargs -0 chmod 700
256277
find /var/www/localhost/htdocs/openemr/sites/${run}/documents -type f -print0 | xargs -0 chmod 700
257278
echo "completed adding ${run} multisite"
258-
a=$(expr ${a} + 1)
279+
a=$((a + 1))
259280
done
260281
sed -i "s@//exit;@exit;@" /var/www/localhost/htdocs/openemr/contrib/util/installScripts/InstallerAuto.php
261282
echo "Completed setting up a multisite bank with following number of multisites (labeled run1, run2, run3, ...): run1...run${1}"

0 commit comments

Comments
 (0)