initscripts: Fix various shellcheck warnings in populate-volatile.sh
PATCH 1/2 initscripts: Fix various shellcheck warnings in populate-volatile.sh
--- .../initscripts-1.0/populate-volatile.sh | 58 +++++++++---------- 1 file changed, 29 insertions(+), 29 deletions(-)
meta/recipes-core/initscripts/initscripts-1.0/populate-volatile.sh +29 -29
@@ -9,10 +9,10 @@ ### END INIT INFO # Get ROOT_DIR -DIRNAME=`dirname $0` -ROOT_DIR=`echo $DIRNAME | sed -ne 's:/etc/.*::p'` +DIRNAME="$(dirname "$0")" +ROOT_DIR="$(echo "$DIRNAME" | sed -ne 's:/etc/.*::p')" -[ -e ${ROOT_DIR}/etc/default/rcS ] && . ${ROOT_DIR}/etc/default/rcS +[ -e "${ROOT_DIR}/etc/default/rcS" ] && . "${ROOT_DIR}/etc/default/rcS" # When running populate-volatile.sh at rootfs time, disable cache. [ -n "$ROOT_DIR" ] && VOLATILE_ENABLE_CACHE=no # If rootfs is read-only, disable cache.
@@ -46,13 +46,13 @@ create_file() { [ "${VERBOSE}" != "no" ] && echo "Target already exists. Skipping." } || { if [ -z "$ROOT_DIR" ]; then - eval $EXEC + eval "$EXEC" else # Creating some files at rootfs time may fail and should fail, # but these failures should not be logged to make sure the do_rootfs # process doesn't fail. This does no harm, as this script will # run on target to set up the correct files and directories. - eval $EXEC > /dev/null 2>&1 + eval "$EXEC" > /dev/null 2>&1 fi } }
@@ -68,11 +68,11 @@ mk_dir() { [ "${VERBOSE}" != "no" ] && echo "Target already exists. Skipping." } || { if [ -z "$ROOT_DIR" ]; then - eval $EXEC + eval "$EXEC" else # For the same reason with create_file(), failures should # not be logged. - eval $EXEC > /dev/null 2>&1 + eval "$EXEC" > /dev/null 2>&1 fi } }
@@ -96,11 +96,11 @@ link_file() { test "$VOLATILE_ENABLE_CACHE" = yes && echo " $EXEC" >> /etc/volatile.cache.build if [ -z "$ROOT_DIR" ]; then - eval $EXEC + eval "$EXEC" else # For the same reason with create_file(), failures should # not be logged. - eval $EXEC > /dev/null 2>&1 + eval "$EXEC" > /dev/null 2>&1 fi }
@@ -117,11 +117,11 @@ check_requirements() { TMP_DEFINED="${TMPROOT}/tmpdefined.$$" TMP_COMBINED="${TMPROOT}/tmpcombined.$$" - sed 's@\(^:\)*:.*@\1@' ${ROOT_DIR}/etc/passwd | sort | uniq > "${TMP_DEFINED}" - cat ${CFGFILE} | grep -v "^#" | cut -s -d " " -f 2 > "${TMP_INTERMED}" + sed 's@\(^:\)*:.*@\1@' "${ROOT_DIR}/etc/passwd" | sort | uniq > "${TMP_DEFINED}" + grep -v "^#" "${CFGFILE}" | cut -s -d " " -f 2 > "${TMP_INTERMED}" cat "${TMP_DEFINED}" "${TMP_INTERMED}" | sort | uniq > "${TMP_COMBINED}" - NR_DEFINED_USERS="`cat "${TMP_DEFINED}" | wc -l`" - NR_COMBINED_USERS="`cat "${TMP_COMBINED}" | wc -l`" + NR_DEFINED_USERS="$(wc -l < "${TMP_DEFINED}")" + NR_COMBINED_USERS="$(wc -l < "${TMP_COMBINED}")" [ "${NR_DEFINED_USERS}" -ne "${NR_COMBINED_USERS}" ] && { echo "Undefined users:"
@@ -131,12 +131,12 @@ check_requirements() { } - sed 's@\(^:\)*:.*@\1@' ${ROOT_DIR}/etc/group | sort | uniq > "${TMP_DEFINED}" - cat ${CFGFILE} | grep -v "^#" | cut -s -d " " -f 3 > "${TMP_INTERMED}" + sed 's@\(^:\)*:.*@\1@' "${ROOT_DIR}/etc/group" | sort | uniq > "${TMP_DEFINED}" + grep -v "^#" "${CFGFILE}" | cut -s -d " " -f 3 > "${TMP_INTERMED}" cat "${TMP_DEFINED}" "${TMP_INTERMED}" | sort | uniq > "${TMP_COMBINED}" - NR_DEFINED_GROUPS="`cat "${TMP_DEFINED}" | wc -l`" - NR_COMBINED_GROUPS="`cat "${TMP_COMBINED}" | wc -l`" + NR_DEFINED_GROUPS="$(wc -l < "${TMP_DEFINED}")" + NR_COMBINED_GROUPS="$(wc -l < "${TMP_COMBINED}")" [ "${NR_DEFINED_GROUPS}" -ne "${NR_COMBINED_GROUPS}" ] && { echo "Undefined groups:"
@@ -157,13 +157,13 @@ apply_cfgfile() { [ "${VERBOSE}" != "no" ] && echo "Applying ${CFGFILE}" - [ "${SKIP_REQUIREMENTS}" == "yes" ] || check_requirements "${CFGFILE}" || { + [ "${SKIP_REQUIREMENTS}" = "yes" ] || check_requirements "${CFGFILE}" || { echo "Skipping ${CFGFILE}" return 1 } - cat ${CFGFILE} | sed 's/#.*//' | \ - while read TTYPE TUSER TGROUP TMODE TNAME TLTARGET; do + sed 's/#.*//' "${CFGFILE}" | \ + while read -r TTYPE TUSER TGROUP TMODE TNAME TLTARGET; do test -z "${TLTARGET}" && continue TNAME=${ROOT_DIR}${TNAME} [ "${VERBOSE}" != "no" ] && echo "Checking for -${TNAME}-."
@@ -187,9 +187,9 @@ apply_cfgfile() { [ -L "${TNAME}" ] && { [ "${VERBOSE}" != "no" ] && echo "Found link." - NEWNAME=`ls -l "${TNAME}" | sed -e 's/^.*-> \(.*\)$/\1/'` - echo ${NEWNAME} | grep -v "^/" >/dev/null && { - TNAME="`echo ${TNAME} | sed -e 's@\(.*\)/.*@\1@'`/${NEWNAME}" + NEWNAME=$(ls -l "${TNAME}" | sed -e 's/^.*-> \(.*\)$/\1/') + echo "${NEWNAME}" | grep -v "^/" >/dev/null && { + TNAME="$(echo "${TNAME}" | sed -e 's@\(.*\)/.*@\1@')/${NEWNAME}" [ "${VERBOSE}" != "no" ] && echo "Converted relative linktarget to absolute path -${TNAME}-." } || { TNAME="${NEWNAME}"
@@ -217,7 +217,7 @@ apply_cfgfile() { clearcache=0 exec 9</proc/cmdline -while read line <&9 +while read -r line <&9 do case "$line" in *clearcache*) clearcache=1
@@ -228,11 +228,11 @@ do done exec 9>&- -if test -e ${ROOT_DIR}/etc/volatile.cache -a "$VOLATILE_ENABLE_CACHE" = "yes" -a "x$1" != "xupdate" -a "x$clearcache" = "x0" +if test -e "${ROOT_DIR}/etc/volatile.cache" -a "$VOLATILE_ENABLE_CACHE" = "yes" -a "x$1" != "xupdate" -a "x$clearcache" = "x0" then - sh ${ROOT_DIR}/etc/volatile.cache + sh "${ROOT_DIR}/etc/volatile.cache" else - rm -f ${ROOT_DIR}/etc/volatile.cache ${ROOT_DIR}/etc/volatile.cache.build + rm -f "${ROOT_DIR}/etc/volatile.cache" "${ROOT_DIR}/etc/volatile.cache.build" # Apply the core file with out checking requirements. ${TMPROOT} is # needed by check_requirements but is setup by this file, so it must be
@@ -246,7 +246,7 @@ else TMP_FILE="${TMPROOT}/tmp_volatile.$$" rm -f "$TMP_FILE" - CFGFILES="`ls -1 "${CFGDIR}" | grep -v "^${COREDEF}\$" | sort`" + CFGFILES="$(ls -1 "${CFGDIR}" | grep -v "^${COREDEF}\$" | sort)" for file in ${CFGFILES}; do cat "${CFGDIR}/${file}" >> "$TMP_FILE" done
@@ -264,7 +264,7 @@ else fi rm "$TMP_FILE" - [ -e ${ROOT_DIR}/etc/volatile.cache.build ] && sync && mv ${ROOT_DIR}/etc/volatile.cache.build ${ROOT_DIR}/etc/volatile.cache + [ -e "${ROOT_DIR}/etc/volatile.cache.build" ] && sync && mv "${ROOT_DIR}/etc/volatile.cache.build" "${ROOT_DIR}/etc/volatile.cache" fi if [ -z "${ROOT_DIR}" ] && [ -f /etc/ld.so.cache ] && [ ! -f /var/run/ld.so.cache ]
Signature · 1 line
2.17.1
PATCH 2/2 initscripts: Fix populate-volatile.sh bug when file/dir exists
The blocks which test for entry exitence (file or directory) use a A && B || C syntax. This form is not behaving as a if-then-else block even the code logic assumes that. C may run when A is true which breaks the case where VERBOSE is 'no' but the file/directory exists.
Along with fixing these specific issues, this patch fixes the other instances where blocks of form A && B || C are used as if-then-else.
--- .../initscripts-1.0/populate-volatile.sh | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-)
meta/recipes-core/initscripts/initscripts-1.0/populate-volatile.sh +12 -12
@@ -26,15 +26,15 @@ COREDEF="00_core" create_file() { EXEC="" - [ -z "$2" ] && { + if [ -z "$2" ]; then EXEC=" touch \"$1\"; " - } || { + else EXEC=" cp \"$2\" \"$1\"; " - } + fi EXEC=" ${EXEC} chown ${TUSER}:${TGROUP} $1 || echo \"Failed to set owner -${TUSER}- for -$1-.\" >/dev/tty0 2>&1;
@@ -42,9 +42,9 @@ create_file() { test "$VOLATILE_ENABLE_CACHE" = yes && echo "$EXEC" >> /etc/volatile.cache.build - [ -e "$1" ] && { + if [ -e "$1" ]; then [ "${VERBOSE}" != "no" ] && echo "Target already exists. Skipping." - } || { + else if [ -z "$ROOT_DIR" ]; then eval "$EXEC" else
@@ -54,7 +54,7 @@ create_file() { # run on target to set up the correct files and directories. eval "$EXEC" > /dev/null 2>&1 fi - } + fi } mk_dir() {
@@ -64,9 +64,9 @@ mk_dir() { chmod ${TMODE} $1 || echo \"Failed to set mode -${TMODE}- for -$1-.\" >/dev/tty0 2>&1 " test "$VOLATILE_ENABLE_CACHE" = yes && echo "$EXEC" >> /etc/volatile.cache.build - [ -e "$1" ] && { + if [ -e "$1" ]; then [ "${VERBOSE}" != "no" ] && echo "Target already exists. Skipping." - } || { + else if [ -z "$ROOT_DIR" ]; then eval "$EXEC" else
@@ -74,7 +74,7 @@ mk_dir() { # not be logged. eval "$EXEC" > /dev/null 2>&1 fi - } + fi } link_file() {
@@ -188,13 +188,13 @@ apply_cfgfile() { [ -L "${TNAME}" ] && { [ "${VERBOSE}" != "no" ] && echo "Found link." NEWNAME=$(ls -l "${TNAME}" | sed -e 's/^.*-> \(.*\)$/\1/') - echo "${NEWNAME}" | grep -v "^/" >/dev/null && { + if echo "${NEWNAME}" | grep -v "^/" >/dev/null; then TNAME="$(echo "${TNAME}" | sed -e 's@\(.*\)/.*@\1@')/${NEWNAME}" [ "${VERBOSE}" != "no" ] && echo "Converted relative linktarget to absolute path -${TNAME}-." - } || { + else TNAME="${NEWNAME}" [ "${VERBOSE}" != "no" ] && echo "Using absolute link target -${TNAME}-." - } + fi } case "${TTYPE}" in
Signature · 1 line
2.17.1
What all shells has it been tested for ?
Quoted 210 lines · Andrei Gherzan
Signed-off-by Andrei Gherzan <[email protected]>--- .../initscripts-1.0/populate-volatile.sh | 58 +++++++++---------- 1 file changed, 29 insertions(+), 29 deletions(-)diff --git
a/meta/recipes-core/initscripts/initscripts-1.0/populate-volatile.shb/meta/recipes-core/initscripts/initscripts-1.0/populate-volatile.shindex1c525b71bd..3a265329f0100755meta/recipes-core/initscripts/initscripts-1.0/populate-volatile.sh +3
-3@@ -9,10 +9,10 @@ ### END INIT INFO # Get ROOT_DIR -DIRNAME=`dirname $0` -ROOT_DIR=`echo $DIRNAME | sed -ne 's:/etc/.*::p'` +DIRNAME="$(dirname "$0")" +ROOT_DIR="$(echo "$DIRNAME" | sed -ne 's:/etc/.*::p')" -[ -e ${ROOT_DIR}/etc/default/rcS ] && . ${ROOT_DIR}/etc/default/rcS +[ -e "${ROOT_DIR}/etc/default/rcS" ] && . "${ROOT_DIR}/etc/default/rcS" # When running populate-volatile.sh at rootfs time, disable cache. [ -n "$ROOT_DIR" ] && VOLATILE_ENABLE_CACHE=no # If rootfs is read-only, disable cache.@@ -46,13 +46,13 @@ create_file() { [ "${VERBOSE}" != "no" ] && echo "Target already exists.Skipping."
} || { if [ -z "$ROOT_DIR" ]; thenmeta/recipes-core/initscripts/initscripts-1.0/populate-volatile.sh +1
-1- eval $EXEC + eval "$EXEC" else # Creating some files at rootfs time may fail andshould fail,
# but these failures should not be logged to make sure the do_rootfs # process doesn't fail. This does no harm, as this script will # run on target to set up the correct files and directories.meta/recipes-core/initscripts/initscripts-1.0/populate-volatile.sh +1
-1- eval $EXEC > /dev/null 2>&1 + eval "$EXEC" > /dev/null 2>&1 fi } }@@ -68,11 +68,11 @@ mk_dir() { [ "${VERBOSE}" != "no" ] && echo "Target already exists.Skipping."
} || { if [ -z "$ROOT_DIR" ]; thenmeta/recipes-core/initscripts/initscripts-1.0/populate-volatile.sh +1
-1- eval $EXEC + eval "$EXEC" else # For the same reason with create_file(), failuresshould
# not be logged.meta/recipes-core/initscripts/initscripts-1.0/populate-volatile.sh +1
-1- eval $EXEC > /dev/null 2>&1 + eval "$EXEC" > /dev/null 2>&1 fi } }@@ -96,11 +96,11 @@ link_file() { test "$VOLATILE_ENABLE_CACHE" = yes && echo " $EXEC" >>
/etc/volatile.cache.buildif [ -z "$ROOT_DIR" ]; thenmeta/recipes-core/initscripts/initscripts-1.0/populate-volatile.sh +2
-3- eval $EXEC + eval "$EXEC" else # For the same reason with create_file(), failures should # not be logged. - eval $EXEC > /dev/null 2>&1 + eval "$EXEC" > /dev/null 2>&1 fi }@@ -117,11 +117,11 @@ check_requirements() { TMP_DEFINED="${TMPROOT}/tmpdefined.$$" TMP_COMBINED="${TMPROOT}/tmpcombined.$$" - sed 's@\(^:\)*:.*@\1@' ${ROOT_DIR}/etc/passwd | sort | uniq >"${TMP_DEFINED}"
- cat ${CFGFILE} | grep -v "^#" | cut -s -d " " -f 2 >
"${TMP_INTERMED}"
+ sed 's@\(^:\)*:.*@\1@' "${ROOT_DIR}/etc/passwd" | sort | uniq >
"${TMP_DEFINED}"
+ grep -v "^#" "${CFGFILE}" | cut -s -d " " -f 2 > "${TMP_INTERMED}"cat "${TMP_DEFINED}" "${TMP_INTERMED}" | sort | uniq >"${TMP_COMBINED}"
meta/recipes-core/initscripts/initscripts-1.0/populate-volatile.sh +2
-3- NR_DEFINED_USERS="`cat "${TMP_DEFINED}" | wc -l`" - NR_COMBINED_USERS="`cat "${TMP_COMBINED}" | wc -l`" + NR_DEFINED_USERS="$(wc -l < "${TMP_DEFINED}")" + NR_COMBINED_USERS="$(wc -l < "${TMP_COMBINED}")" [ "${NR_DEFINED_USERS}" -ne "${NR_COMBINED_USERS}" ] && { echo "Undefined users:"@@ -131,12 +131,12 @@ check_requirements() { } - sed 's@\(^:\)*:.*@\1@' ${ROOT_DIR}/etc/group | sort | uniq >"${TMP_DEFINED}"
- cat ${CFGFILE} | grep -v "^#" | cut -s -d " " -f 3 >
"${TMP_INTERMED}"
+ sed 's@\(^:\)*:.*@\1@' "${ROOT_DIR}/etc/group" | sort | uniq >
"${TMP_DEFINED}"
+ grep -v "^#" "${CFGFILE}" | cut -s -d " " -f 3 > "${TMP_INTERMED}"cat "${TMP_DEFINED}" "${TMP_INTERMED}" | sort | uniq >"${TMP_COMBINED}"
meta/recipes-core/initscripts/initscripts-1.0/populate-volatile.sh +5
-5- NR_DEFINED_GROUPS="`cat "${TMP_DEFINED}" | wc -l`" - NR_COMBINED_GROUPS="`cat "${TMP_COMBINED}" | wc -l`" + NR_DEFINED_GROUPS="$(wc -l < "${TMP_DEFINED}")" + NR_COMBINED_GROUPS="$(wc -l < "${TMP_COMBINED}")" [ "${NR_DEFINED_GROUPS}" -ne "${NR_COMBINED_GROUPS}" ] && { echo "Undefined groups:"@@ -157,13 +157,13 @@ apply_cfgfile() { [ "${VERBOSE}" != "no" ] && echo "Applying ${CFGFILE}" - [ "${SKIP_REQUIREMENTS}" == "yes" ] || check_requirements "${CFGFILE}" || { + [ "${SKIP_REQUIREMENTS}" = "yes" ] || check_requirements "${CFGFILE}" || { echo "Skipping ${CFGFILE}" return 1 } - cat ${CFGFILE} | sed 's/#.*//' | \ - while read TTYPE TUSER TGROUP TMODE TNAME TLTARGET; do + sed 's/#.*//' "${CFGFILE}" | \ + while read -r TTYPE TUSER TGROUP TMODE TNAME TLTARGET; do test -z "${TLTARGET}" && continueTNAME=${ROOT_DIR}${TNAME} [ "${VERBOSE}" != "no" ] && echo "Checking for -${TNAME}-."meta/recipes-core/initscripts/initscripts-1.0/populate-volatile.sh +0
-1@@ -187,9 +187,9 @@ apply_cfgfile() { [ -L "${TNAME}" ] && { [ "${VERBOSE}" != "no" ] && echo "Found link." - NEWNAME=`ls -l "${TNAME}" | sed -e 's/^.*->\(.*\)$/\1/'`
meta/recipes-core/initscripts/initscripts-1.0/populate-volatile.sh +0
-2- echo ${NEWNAME} | grep -v "^/" >/dev/null && { - TNAME="`echo ${TNAME} | sed -e 's@\(.*\)/.*@\1@'`/${NEWNAME}"
+ NEWNAME=$(ls -l "${TNAME}" | sed -e 's/^.*->
\(.*\)$/\1/')meta/recipes-core/initscripts/initscripts-1.0/populate-volatile.sh +2
-0+ echo "${NEWNAME}" | grep -v "^/" >/dev/null && { + TNAME="$(echo "${TNAME}" | sed -e 's@\(.*\)/.*@\1@')/${NEWNAME}"
[ "${VERBOSE}" != "no" ] && echo "Converted relative linktarget to absolute path -${TNAME}-." } || { TNAME="${NEWNAME}"meta/recipes-core/initscripts/initscripts-1.0/populate-volatile.sh +1
-2@@ -217,7 +217,7 @@ apply_cfgfile() { clearcache=0 exec 9</proc/cmdline -while read line <&9 +while read -r line <&9 do case "$line" in *clearcache*) clearcache=1@@ -228,11 +228,11 @@ do done exec 9>&- -if test -e ${ROOT_DIR}/etc/volatile.cache -a "$VOLATILE_ENABLE_CACHE" ="yes" -a "x$1" != "xupdate" -a "x$clearcache" = "x0"
+if test -e "${ROOT_DIR}/etc/volatile.cache" -a "$VOLATILE_ENABLE_CACHE" = "yes" -a "x$1" != "xupdate" -a "x$clearcache" = "x0"
thenmeta/recipes-core/initscripts/initscripts-1.0/populate-volatile.sh +1
-2- sh ${ROOT_DIR}/etc/volatile.cache + sh "${ROOT_DIR}/etc/volatile.cache" else - rm -f ${ROOT_DIR}/etc/volatile.cache${ROOT_DIR}/etc/volatile.cache.build
+ rm -f "${ROOT_DIR}/etc/volatile.cache"
"${ROOT_DIR}/etc/volatile.cache.build"# Apply the core file with out checking requirements. ${TMPROOT} is # needed by
check_requirementsbut is setup by this file, so it
must bemeta/recipes-core/initscripts/initscripts-1.0/populate-volatile.sh +1
-2@@ -246,7 +246,7 @@ else TMP_FILE="${TMPROOT}/tmp_volatile.$$" rm -f "$TMP_FILE" - CFGFILES="`ls -1 "${CFGDIR}" | grep -v "^${COREDEF}\$" | sort`" + CFGFILES="$(ls -1 "${CFGDIR}" | grep -v "^${COREDEF}\$" | sort)" for file in ${CFGFILES}; do cat "${CFGDIR}/${file}" >> "$TMP_FILE" done@@ -264,7 +264,7 @@ else fi rm "$TMP_FILE" - [ -e ${ROOT_DIR}/etc/volatile.cache.build ] && sync && mv${ROOT_DIR}/etc/volatile.cache.build ${ROOT_DIR}/etc/volatile.cache
+ [ -e "${ROOT_DIR}/etc/volatile.cache.build" ] && sync && mv
"${ROOT_DIR}/etc/volatile.cache.build" "${ROOT_DIR}/etc/volatile.cache"
fiif [ -z "${ROOT_DIR}" ] && [ -f
/etc/ld.so.cache] && [ ! -f/var/run/ld.so.cache]
--
2.17.1
Quoted 2 lines · Khem Raj
What all shells has it been tested for ?
I've used shellcheck for static analysis and tested at runtime with busybox shell.
Andrei
Quoted 9 lines · Andrei Gherzan
Quoted 2 lines · Khem Raj
What all shells has it been tested for ?
I've used shellcheck for static analysis and tested at runtime with busybox shell.
Thanks if it works with busybox ash I am happy with the patch
Quoted 3 lines
Andrei