From dd0fa0d34cd3256b08eb90dbaabb4a58069480da Mon Sep 17 00:00:00 2001 From: Jonathan Hui Date: Thu, 14 May 2020 14:56:47 -0700 Subject: [PATCH] [shell] resolve shellcheck warnings (#4966) --- bootstrap | 8 ++---- etc/docker/ot_sim/add_node | 2 +- script/check-ncp-rcp-migrate | 6 ++--- script/check-posix-build-autotools | 2 -- script/check-posix-build-cmake | 8 +++--- script/check-posix-pty | 36 +++++++++++++++---------- script/test | 6 ++--- tests/toranj/build.sh | 42 +++++++++++++++--------------- tests/toranj/start.sh | 24 ++++++++--------- tools/gerrit/git-squash-merge.sh | 19 +++++--------- tools/harness-automation/start.sh | 4 +-- 11 files changed, 76 insertions(+), 81 deletions(-) diff --git a/bootstrap b/bootstrap index 7900a5cda..b91f74894 100755 --- a/bootstrap +++ b/bootstrap @@ -36,10 +36,6 @@ nlbuild_autotools_stem="third_party/nlbuild-autotools/repo" -# Establish some key directories +abs_srcdir=$(cd "$(dirname "${0}")" && pwd) -srcdir=$(dirname ${0}) -abs_srcdir=$(pwd) -abs_top_srcdir="${abs_srcdir}" - -exec ${srcdir}/${nlbuild_autotools_stem}/scripts/bootstrap -I "${abs_top_srcdir}/${nlbuild_autotools_stem}" $* +(cd "$abs_srcdir" && exec "$abs_srcdir/$nlbuild_autotools_stem/scripts/bootstrap" -I "$abs_srcdir/$nlbuild_autotools_stem" "${@}") diff --git a/etc/docker/ot_sim/add_node b/etc/docker/ot_sim/add_node index 262527927..223045560 100755 --- a/etc/docker/ot_sim/add_node +++ b/etc/docker/ot_sim/add_node @@ -1,2 +1,2 @@ #!/bin/bash -docker exec -it otsim node $1 +docker exec -it otsim node "$1" diff --git a/script/check-ncp-rcp-migrate b/script/check-ncp-rcp-migrate index 1ab6a179c..db976383f 100755 --- a/script/check-ncp-rcp-migrate +++ b/script/check-ncp-rcp-migrate @@ -93,7 +93,7 @@ EOF echo "Step 2. Start retrieving dataset from Radio..." RADIO_NCP_PATH="$(pwd)/$(ls output/*linux*/bin/ot-ncp-ftd)" - "$(pwd)/$(ls output/posix/*linux*/bin/ot-ncp)" -n --radio-version --ncp-dataset -- ${RADIO_NCP_PATH} 1 + "$(pwd)/$(ls output/posix/*linux*/bin/ot-ncp)" -n --radio-version --ncp-dataset -- "${RADIO_NCP_PATH}" 1 echo "Step 3. Start posix app and check whether PAN dataset is the same..." RADIO_RCP_PATH="$(pwd)/$(ls output/*linux*/bin/ot-rcp)" @@ -126,8 +126,8 @@ expect eof EOF echo "Step 4. Start posix app and check whether it can get radio firmware version..." - RADIO_VERSION="$("$(pwd)/$(ls output/posix/*linux*/bin/ot-cli)" -n --radio-version --ncp-dataset -- ${RADIO_RCP_PATH} 1)" || true - test -n "{RADIO_VERSION}" + RADIO_VERSION="$("$(pwd)/$(ls output/posix/*linux*/bin/ot-cli)" -n --radio-version --ncp-dataset -- "${RADIO_RCP_PATH}" 1)" || true + test -n "${RADIO_VERSION}" } main() diff --git a/script/check-posix-build-autotools b/script/check-posix-build-autotools index 2d9429c6f..a8cc3d248 100755 --- a/script/check-posix-build-autotools +++ b/script/check-posix-build-autotools @@ -29,8 +29,6 @@ set -euxo pipefail -readonly OT_BUILD_JOBS=$(getconf _NPROCESSORS_ONLN) - reset_source() { rm -rf build output tmp diff --git a/script/check-posix-build-cmake b/script/check-posix-build-cmake index 2910923a5..2cf7c3236 100755 --- a/script/check-posix-build-cmake +++ b/script/check-posix-build-cmake @@ -77,12 +77,12 @@ build() "-DOT_FULL_LOGS=on" ) - mkdir -p $OT_BUILDDIR - cd $OT_BUILDDIR - cmake -GNinja "${options[@]}" $OT_SRCDIR + mkdir -p "$OT_BUILDDIR" + cd "$OT_BUILDDIR" + cmake -GNinja "${options[@]}" "$OT_SRCDIR" ninja - cd $OT_SRCDIR + cd "$OT_SRCDIR" } main() diff --git a/script/check-posix-pty b/script/check-posix-pty index 8382f1722..b4567a148 100755 --- a/script/check-posix-pty +++ b/script/check-posix-pty @@ -32,7 +32,7 @@ set -x die() { - echo " *** ERROR: " $* + echo " *** ERROR: " "$*" exit 1 } @@ -62,7 +62,7 @@ check() OT_OUTPUT=/tmp/ot-output socat -d -d pty,raw,echo=0 pty,raw,echo=0 >/dev/null 2>$SOCAT_OUTPUT & while true; do - if test $(head -n2 $SOCAT_OUTPUT | wc -l) = 2; then + if test "$(head -n2 $SOCAT_OUTPUT | wc -l)" = 2; then RADIO_PTY=$(head -n1 $SOCAT_OUTPUT | grep -o '/dev/.\+') CORE_PTY=$(head -n2 $SOCAT_OUTPUT | tail -n1 | grep -o '/dev/.\+') break @@ -70,11 +70,13 @@ check() echo 'Waiting for socat ready...' sleep 1 done - echo 'RADIO_PTY' $DEVICE_PTY - echo 'CORE_PTY' $CORE_PTY + echo 'RADIO_PTY' "$DEVICE_PTY" + echo 'CORE_PTY' "$CORE_PTY" RADIO_NCP_PATH="$(pwd)/$(ls output/*linux*/bin/ot-rcp)" - $RADIO_NCP_PATH 1 >$RADIO_PTY <$RADIO_PTY & + + # shellcheck disable=SC2094 + $RADIO_NCP_PATH 1 >"$RADIO_PTY" <"$RADIO_PTY" & # Cover setting a valid network interface name. readonly VALID_NETIF_NAME="wan$(date +%H%M%S)" @@ -84,26 +86,28 @@ check() ) if [[ ${DAEMON} == 1 ]]; then - sudo "$(pwd)/$(ls output/posix/*linux*/bin/ot-daemon)" "${options[@]}" -I "${VALID_NETIF_NAME}" ${CORE_PTY} & + sudo "$(pwd)/$(ls output/posix/*linux*/bin/ot-daemon)" "${options[@]}" -I "${VALID_NETIF_NAME}" "${CORE_PTY}" & sleep 1 OT_CLI_CMD="$(pwd)/$(ls output/posix/*linux*/bin/ot-ctl)" + OT_CLI_ARG= else - OT_CLI_CMD="$(pwd)/$(ls output/posix/*linux*/bin/ot-cli) "${options[@]}" ${CORE_PTY}" + OT_CLI_CMD="$(pwd)/$(ls output/posix/*linux*/bin/ot-cli)" + OT_CLI_ARG="${CORE_PTY}" fi - sudo ${OT_CLI_CMD} -I "${VALID_NETIF_NAME}" -n + sudo "${OT_CLI_CMD}" "${OT_CLI_ARG}" -I "${VALID_NETIF_NAME}" -n # Cover setting a too long(max is 15 characters) network interface name. # Expect exit code to be 2(OT_EXIT_INVALID_ARGUMENTS). readonly INVALID_NETIF_NAME="wan0123456789123" - sudo ${OT_CLI_CMD} -I "${INVALID_NETIF_NAME}" -n || test $? = 2 + sudo "${OT_CLI_CMD}" "${OT_CLI_ARG}" -I "${INVALID_NETIF_NAME}" -n || test $? = 2 if [[ ${DAEMON} == 1 ]]; then - sudo ${OT_CLI_CMD} panid 0xface | grep 'Done' || die 'failed to set panid with ot-ctl' + sudo "${OT_CLI_CMD}" "${OT_CLI_ARG}" panid 0xface | grep 'Done' || die 'failed to set panid with ot-ctl' fi - sudo expect <"${OT_OUTPUT}" & -spawn ${OT_CLI_CMD} + sudo expect <" + echo "Usage: $(basename "$0") [options] " echo " can be:" echo " ncp : Build OpenThread NCP FTD mode with simulation platform" echo " rcp : Build OpenThread RCP (NCP in radio mode) with simulation platform" @@ -51,10 +48,13 @@ display_usage() die() { - echo " *** ERROR: " $* + echo " *** ERROR: " "$*" exit 1 } +cd "$(dirname "$0")" || die "cd failed" +cd ../.. || die "cd failed" + coverage=no tests=no @@ -83,13 +83,13 @@ fi build_config=$1 -configure_options=" \ - --disable-docs \ - --enable-tests=$tests \ - --enable-coverage=$coverage \ - --enable-ftd \ - --enable-ncp \ - " +configure_options=( + "--disable-docs" + "--enable-tests=$tests" + "--enable-coverage=$coverage" + "--enable-ftd" + "--enable-ncp" +) if [ -n "${top_builddir}" ]; then top_srcdir=$(pwd) @@ -104,13 +104,13 @@ case ${build_config} in echo "===================================================================================================" echo "Building OpenThread NCP FTD mode with POSIX platform" echo "===================================================================================================" - ./bootstrap || die - cd "${top_builddir}" + ./bootstrap || die "bootstrap failed" + cd "${top_builddir}" || die "cd failed" cppflags_config='-DOPENTHREAD_PROJECT_CORE_CONFIG_FILE=\"../tests/toranj/openthread-core-toranj-config-simulation.h\"' ${top_srcdir}/configure \ CPPFLAGS="$cppflags_config" \ --with-examples=simulation \ - $configure_options || die + "${configure_options[@]}" || die make -j 8 || die ;; @@ -118,8 +118,8 @@ case ${build_config} in echo "====================================================================================================" echo "Building OpenThread RCP (NCP in radio mode) with POSIX platform" echo "====================================================================================================" - ./bootstrap || die - cd "${top_builddir}" + ./bootstrap || die "bootstrap failed" + cd "${top_builddir}" || die "cd failed" cppflags_config='-DOPENTHREAD_PROJECT_CORE_CONFIG_FILE=\"../tests/toranj/openthread-core-toranj-config-simulation.h\"' ${top_srcdir}/configure \ CPPFLAGS="$cppflags_config " \ @@ -136,13 +136,13 @@ case ${build_config} in echo "====================================================================================================" echo "Building OpenThread POSIX App NCP" echo "====================================================================================================" - ./bootstrap || die - cd "${top_builddir}" + ./bootstrap || die "bootstrap failed" + cd "${top_builddir}" || die "cd failed" cppflags_config='-DOPENTHREAD_PROJECT_CORE_CONFIG_FILE=\"../tests/toranj/openthread-core-toranj-config-posix.h\"' ${top_srcdir}/configure \ CPPFLAGS="$cppflags_config" \ --with-platform=posix \ - $configure_options || die + "${configure_options[@]}" || die make -j 8 || die ;; diff --git a/tests/toranj/start.sh b/tests/toranj/start.sh index 4601a2157..4dcd6a123 100755 --- a/tests/toranj/start.sh +++ b/tests/toranj/start.sh @@ -1,4 +1,4 @@ -#!/bin/sh +#!/bin/bash # # Copyright (c) 2018, The OpenThread Authors. # All rights reserved. @@ -29,7 +29,7 @@ die() { - echo " *** ERROR: " $* + echo " *** ERROR: " "$*" exit 1 } @@ -37,16 +37,14 @@ cleanup() { # Clear logs and flash files sudo rm tmp/*.flash tmp/*.data tmp/*.swap >/dev/null 2>&1 - sudo rm *.log >/dev/null 2>&1 + sudo rm ./*.log >/dev/null 2>&1 # Clear any wpantund instances sudo killall wpantund >/dev/null 2>&1 - wpan_interfaces=$(ifconfig 2>/dev/null | grep -o wpan[0-9]*) - - for interface in $wpan_interfaces; do - sudo ip link delete $interface >/dev/null 2>&1 - done + while read -r interface; do + sudo ip link delete "$interface" >/dev/null 2>&1 + done < <(ifconfig 2>/dev/null | grep -o "wpan[0-9]*") sleep 0.3 } @@ -56,7 +54,7 @@ run() counter=0 while true; do - if sudo -E python $1; then + if sudo -E python "$1"; then cleanup return fi @@ -76,7 +74,7 @@ run() done } -cd $(dirname $0) +cd "$(dirname "$0")" || die "cd failed" if [ "$COVERAGE" = 1 ]; then coverage_option="--enable-coverage" @@ -94,11 +92,11 @@ case $TORANJ_POSIX_RCP_MODEL in esac if [ "$use_posix_with_rcp" = "no" ]; then - ./build.sh ${coverage_option} ncp || die + ./build.sh ${coverage_option} ncp || die "ncp build failed" else - ./build.sh ${coverage_option} rcp || die - ./build.sh ${coverage_option} posix || die + ./build.sh ${coverage_option} rcp || die "rcp build failed" + ./build.sh ${coverage_option} posix || die "posix build failed" fi cleanup diff --git a/tools/gerrit/git-squash-merge.sh b/tools/gerrit/git-squash-merge.sh index 764f344d1..eff4ee73d 100755 --- a/tools/gerrit/git-squash-merge.sh +++ b/tools/gerrit/git-squash-merge.sh @@ -1,4 +1,4 @@ -#!/bin/sh +#!/bin/bash # # Copyright (c) 2018, The OpenThread Authors. # All rights reserved. @@ -29,7 +29,7 @@ die() { - echo " *** ERROR: " $* + echo " *** ERROR: " "$*" exit 1 } @@ -41,7 +41,7 @@ display_usage() echo "By default, the changes are committed with a commit message which includes the list of all squashed commits" echo "(use --no-commit and --no-list options to change default behavior)." echo "" - echo "Usage: $(basename $0) [--no-list] [--no-commit] []" + echo "Usage: $(basename "$0") [--no-list] [--no-commit] []" echo "" echo " Specifies the name of branch to merge into current branch" echo " An optional parameter specifying text to add to the commit message" @@ -69,10 +69,6 @@ while test $# != 0; do ;; --debug) set -x ;; --*) die "Unknown argument $1" ;; - --) - shift - break - ;; -*) die "Unknown argument $1" ;; *) break @@ -100,7 +96,7 @@ cur_branch=$(git rev-parse --abbrev-ref HEAD) # Note that the list starts with older commits if ${SHOULD_ADD_LIST}; then - commit_list=$(git log HEAD..$branch --oneline --decorate=no --reverse) + commit_list=$(git log HEAD.."$branch" --oneline --decorate=no --reverse) else commit_list="" fi @@ -113,18 +109,17 @@ else commit_msg="${commit_msg_header}${NEWLINE}${NEWLINE}$2${NEWLINE}${NEWLINE}${commit_list}" fi -git merge --squash $branch 1>/dev/null 2>/dev/null || die "Failed to perform 'git merge -squash $branch'" +git merge --squash "$branch" 1>/dev/null 2>/dev/null || die "Failed to perform 'git merge -squash $branch'" # Check if there is anything staged -git diff --cached --quiet -if [ $? -eq 0 ]; then +if ! git diff --cached --quiet; then echo "No changes to commit when squash merging branch '$branch' into '$cur_branch'" exit 0 fi if ${SHOULD_COMMIT}; then # Commit the staged changes - git commit -m "$commit_msg" 1>/dev/null 2>/dev/null || die "git commit failed${NEWLINE}${NEWLINE}$(cat $TMP)" + git commit -m "$commit_msg" 1>/dev/null 2>/dev/null || die "git commit failed${NEWLINE}${NEWLINE}$(cat "$TMP")" git log -1 diff --git a/tools/harness-automation/start.sh b/tools/harness-automation/start.sh index d5d639bcd..b3217dacb 100755 --- a/tools/harness-automation/start.sh +++ b/tools/harness-automation/start.sh @@ -1,3 +1,3 @@ #!/bin/sh -cd $(dirname $0) -python -u -m autothreadharness.runner $@ +cd "$(dirname "$0")" || exit 1 +python -u -m autothreadharness.runner "$@"