meta-google: google-usb-network: Fix shellcheck

Change-Id: I214ce709f1ddfd62a0a94f2f974c103e3edc2c58
Signed-off-by: William A. Kennington III <wak@google.com>
diff --git a/meta-google/recipes-google/networking/google-usb-network/usb_network.sh b/meta-google/recipes-google/networking/google-usb-network/usb_network.sh
index 8b7bf53..b34aa0f 100755
--- a/meta-google/recipes-google/networking/google-usb-network/usb_network.sh
+++ b/meta-google/recipes-google/networking/google-usb-network/usb_network.sh
@@ -67,13 +67,13 @@
 
     local gadget_dir="${CONFIGFS_HOME}/usb_gadget/${GADGET_DIR_NAME}"
     mkdir -p "${gadget_dir}" || return
-    echo ${ID_VENDOR} > "${gadget_dir}/idVendor" || return
-    echo ${ID_PRODUCT} > "${gadget_dir}/idProduct" || return
+    echo "${ID_VENDOR}" >"${gadget_dir}/idVendor" || return
+    echo "${ID_PRODUCT}" >"${gadget_dir}/idProduct" || return
 
     local str_en_dir="${gadget_dir}/strings/0x409"
     mkdir -p "${str_en_dir}" || return
-    echo ${STR_EN_VENDOR} > "${str_en_dir}/manufacturer" || return
-    echo ${STR_EN_PRODUCT} > "${str_en_dir}/product" || return
+    echo "${STR_EN_VENDOR}" >"${str_en_dir}/manufacturer" || return
+    echo "${STR_EN_PRODUCT}" >"${str_en_dir}/product" || return
 
     local config_dir="${gadget_dir}/configs/c.1"
     mkdir -p "${config_dir}" || return
@@ -85,11 +85,11 @@
     mkdir -p "${func_dir}" || return
 
     if [[ -n $HOST_MAC_ADDR ]]; then
-        echo ${HOST_MAC_ADDR} >${func_dir}/host_addr || return
+        echo "${HOST_MAC_ADDR}" >"${func_dir}"/host_addr || return
     fi
 
     if [[ -n $DEV_MAC_ADDR ]]; then
-        echo ${DEV_MAC_ADDR} >${func_dir}/dev_addr || return
+        echo "${DEV_MAC_ADDR}" >"${func_dir}"/dev_addr || return
     fi
 
     ln -s "${func_dir}" "${config_dir}" || return
@@ -97,7 +97,7 @@
     # This only works on kernel 5.12+, we have to ignore failures for now
     echo "$IFACE_NAME" >"${func_dir}"/ifname || true
 
-    echo "${BIND_DEVICE}" >${gadget_dir}/UDC || return
+    echo "${BIND_DEVICE}" >"${gadget_dir}"/UDC || return
     # Try to reconfigure a few times in case we race with systemd-networkd
     local start=$SECONDS
     while (( SECONDS - start < 5 )); do
@@ -113,25 +113,25 @@
 
 gadget_stop() {
     local gadget_dir="${CONFIGFS_HOME}/usb_gadget/${GADGET_DIR_NAME}"
-    rm -f ${gadget_dir}/configs/c.1/${DEV_TYPE}.${IFACE_NAME}
-    rmdir ${gadget_dir}/functions/${DEV_TYPE}.${IFACE_NAME} \
-      ${gadget_dir}/configs/c.1/strings/0x409 \
-      ${gadget_dir}/configs/c.1 \
-      ${gadget_dir}/strings/0x409 \
-      ${gadget_dir} || true
+    rm -f "${gadget_dir}/configs/c.1/${DEV_TYPE}.${IFACE_NAME}"
+    rmdir "${gadget_dir}/functions/${DEV_TYPE}.${IFACE_NAME}" \
+      "${gadget_dir}/configs/c.1/strings/0x409" \
+      "${gadget_dir}/configs/c.1" \
+      "${gadget_dir}/strings/0x409" \
+      "${gadget_dir}" || true
 
     rm -f /run/systemd/network/+-bmc-"${IFACE_NAME}".network
     networkctl reload || true
 }
 
-opts=$(getopt \
+opts="$(getopt \
     --longoptions "$(printf "%s," "${ARGUMENT_LIST[@]}")" \
     --name "$(basename "$0")" \
     --options "" \
     -- "$@"
-)
+)"
 
-eval set --$opts
+eval set -- "$opts"
 
 CONFIGFS_HOME=${CONFIGFS_HOME:-/sys/kernel/config}
 ID_VENDOR="0x18d1" # Google
diff --git a/meta-google/recipes-google/networking/google-usb-network/usb_network_test.sh b/meta-google/recipes-google/networking/google-usb-network/usb_network_test.sh
index acbd23a..e4aa332 100755
--- a/meta-google/recipes-google/networking/google-usb-network/usb_network_test.sh
+++ b/meta-google/recipes-google/networking/google-usb-network/usb_network_test.sh
@@ -14,8 +14,7 @@
 # limitations under the License.
 
 
-TEMPDIRS=""
-TEMPFILES=""
+TEMPDIRS=()
 # Script under test
 SUT=$PWD/usb_network.sh
 
@@ -23,25 +22,21 @@
 
 test_setup() {
     echo -n "Testing $1 ..."
-    FAKE_CONFIGFS=$(mktemp -d)
-    TEMPDIRS="${TEMPDIRS} ${FAKE_CONFIGFS}"
-    FAKE_GADGETFS=$FAKE_CONFIGFS/usb_gadget
+    FAKE_CONFIGFS="$(mktemp -d)"
+    TEMPDIRS+=("${FAKE_CONFIGFS}")
+    FAKE_GADGETFS="$FAKE_CONFIGFS"/usb_gadget
     mkdir -p "$FAKE_GADGETFS"
 }
 
 test_teardown() {
     echo ${TEST_STATUS}
-    if test -n "$TEMPDIRS"; then
-        rm -rf ${TEMPDIRS}
-    fi
-    if test -n "$TEMPFILES"; then
-        rm -f $TEMPFILES
-    fi
+    rm -rf -- "${TEMPDIRS[@]}"
+    TEMPDIRS=()
 }
 
 test_fail() {
-    echo -n " $@ " >&2
-    TEST_STATUS="FAIL"
+    echo -n " $* " >&2
+    TEST_STATUS=FAIL
 
     test_teardown
     exit 1
@@ -51,12 +46,13 @@
     local filename="$1"
     local expected_content="$2"
 
-    if ! test -f "${filename}"; then
+    if [[ ! -f ${filename} ]]; then
         test_fail "File ${filename} does not exist!"
     fi
 
-    local actual_content=$(cat ${filename})
-    if [[ $expected_content != $actual_content ]]; then
+    local actual_content
+    actual_content=$(<"${filename}")
+    if [[ $expected_content != "$actual_content" ]]; then
         test_fail "Expected ${expected_content}, got ${actual_content}"
     fi
 }
@@ -67,57 +63,56 @@
     if [[ $gadget_dir == "" ]]; then
         gadget_dir="g1";
     else
-        extra_args=(${extra_args} --gadget-dir-name "${gadget_dir}")
+        extra_args+=(--gadget-dir-name "${gadget_dir}")
     fi
     local product_name="Souvenier BMC"
     local product_id="0xcafe"
     local host_mac="ab:cd:ef:10:11:12"
     local dev_mac="12:11:10:ef:cd:ab"
     local bind_device="f80002000.udc"
-    CONFIGFS_HOME=${FAKE_CONFIGFS} ${SUT} --product-id "${product_id}" \
+    if ! CONFIGFS_HOME="${FAKE_CONFIGFS}" "${SUT}" --product-id "${product_id}" \
         --product-name "${product_name}" \
         --host-mac "${host_mac}" \
         --dev-mac "${dev_mac}" \
         --bind-device "${bind_device}" \
-        ${extra_args[@]}
-
-    if test $? -ne 0; then
+        "${extra_args[@]}"; then
         test_fail "${SUT} failed"
     fi
 
-    if ! test -d "${FAKE_GADGETFS}/${gadget_dir}"; then
+    if [[ ! -d ${FAKE_GADGETFS}/${gadget_dir} ]]; then
         test_fail "Gadget was not created!"
     fi
 
-    check_file_content ${FAKE_GADGETFS}/${gadget_dir}/idVendor "0x18d1"
-    check_file_content ${FAKE_GADGETFS}/${gadget_dir}/idProduct "${product_id}"
-    check_file_content ${FAKE_GADGETFS}/${gadget_dir}/strings/0x409/manufacturer "Google"
-    check_file_content ${FAKE_GADGETFS}/${gadget_dir}/strings/0x409/product "${product_name}"
-    check_file_content ${FAKE_GADGETFS}/${gadget_dir}/configs/c.1/MaxPower "100"
-    check_file_content ${FAKE_GADGETFS}/${gadget_dir}/configs/c.1/strings/0x409/configuration "EEM"
-    check_file_content ${FAKE_GADGETFS}/${gadget_dir}/functions/eem.usb0/dev_addr "${dev_mac}"
-    check_file_content ${FAKE_GADGETFS}/${gadget_dir}/functions/eem.usb0/host_addr "${host_mac}"
+    check_file_content "${FAKE_GADGETFS}/${gadget_dir}/idVendor" "0x18d1"
+    check_file_content "${FAKE_GADGETFS}/${gadget_dir}/idProduct" "${product_id}"
+    check_file_content "${FAKE_GADGETFS}/${gadget_dir}/strings/0x409/manufacturer" "Google"
+    check_file_content "${FAKE_GADGETFS}/${gadget_dir}/strings/0x409/product" "${product_name}"
+    check_file_content "${FAKE_GADGETFS}/${gadget_dir}/configs/c.1/MaxPower" "100"
+    check_file_content "${FAKE_GADGETFS}/${gadget_dir}/configs/c.1/strings/0x409/configuration" "EEM"
+    check_file_content "${FAKE_GADGETFS}/${gadget_dir}/functions/eem.usb0/dev_addr" "${dev_mac}"
+    check_file_content "${FAKE_GADGETFS}/${gadget_dir}/functions/eem.usb0/host_addr" "${host_mac}"
 
-    if ! test -d ${FAKE_GADGETFS}/${gadget_dir}/functions/eem.usb0; then
+    if [[ ! -d ${FAKE_GADGETFS}/${gadget_dir}/functions/eem.usb0 ]]; then
         test_fail "Function directory was not created"
     fi
 
     local func_link="${FAKE_GADGETFS}/${gadget_dir}/configs/c.1/ee.usb0"
-    if ! test -L "${func_link}"; then
+    if [[ ! -L ${func_link} ]]; then
         test_fail "Symlink to the function was not created in the config"
     fi
 
-    local link_dest=$(realpath ${func_link})
-    if [[ $link_dest != ${FAKE_GADGETFS}/${gadget_dir}/functions/eem.usb0 ]]; then
+    local link_dest
+    link_dest="$(realpath "${func_link}")"
+    if [[ $link_dest != "${FAKE_GADGETFS}/${gadget_dir}/functions/eem.usb0" ]]; then
         test_fail "Symlink points to the wrong file/dir"
     fi
 
-    check_file_content ${FAKE_GADGETFS}/${gadget_dir}/UDC "${bind_device}"
+    check_file_content "${FAKE_GADGETFS}/${gadget_dir}/UDC" "${bind_device}"
 }
 
 test_gadget_creation_with_override() {
-    mkdir -p ${FAKE_GADGETFS}/g1/{strings,configs,functions}
-    touch ${FAKE_GADGETFS}/g1/{idVendor,idProduct}
+    mkdir -p "${FAKE_GADGETFS}"/g1/{strings,configs,functions}
+    touch "${FAKE_GADGETFS}"/g1/{idVendor,idProduct}
 
     test_gadget_creation_with_defaults
 }
@@ -129,16 +124,16 @@
     if [[ $gadget_dir == "" ]]; then
         gadget_dir="g1";
     else
-        extra_args=(${extra_args} --gadget-dir-name "${gadget_dir}")
+        extra_args+=(--gadget-dir-name "${gadget_dir}")
     fi
 
     if [[ $iface_name == "" ]]; then
         iface_name="usb0";
     else
-        extra_args=(${extra_args} --iface-name "${iface_name}")
+        extra_args+=(--iface-name "${iface_name}")
     fi
 
-    CONFIGFS_HOME=${FAKE_CONFIGFS} ${SUT} ${extra_args[@]} stop
+    CONFIGFS_HOME=${FAKE_CONFIGFS} ${SUT} "${extra_args[@]}" stop
 
     if test -d "${FAKE_GADGETFS}/${gadget_dir}"; then
         test_fail "Gadget was not removed!"
@@ -162,32 +157,33 @@
         test_fail "Gadget was not created!"
     fi
 
-    check_file_content ${FAKE_GADGETFS}/${gadget_dir}/idVendor "0x18d1"
-    check_file_content ${FAKE_GADGETFS}/${gadget_dir}/idProduct "${product_id}"
-    check_file_content ${FAKE_GADGETFS}/${gadget_dir}/strings/0x409/manufacturer "Google"
-    check_file_content ${FAKE_GADGETFS}/${gadget_dir}/strings/0x409/product "${product_name}"
-    check_file_content ${FAKE_GADGETFS}/${gadget_dir}/configs/c.1/MaxPower "100"
-    check_file_content ${FAKE_GADGETFS}/${gadget_dir}/configs/c.1/strings/0x409/configuration "EEM"
+    check_file_content "${FAKE_GADGETFS}/${gadget_dir}/idVendor" "0x18d1"
+    check_file_content "${FAKE_GADGETFS}/${gadget_dir}/idProduct" "${product_id}"
+    check_file_content "${FAKE_GADGETFS}/${gadget_dir}/strings/0x409/manufacturer" "Google"
+    check_file_content "${FAKE_GADGETFS}/${gadget_dir}/strings/0x409/product" "${product_name}"
+    check_file_content "${FAKE_GADGETFS}/${gadget_dir}/configs/c.1/MaxPower" "100"
+    check_file_content "${FAKE_GADGETFS}/${gadget_dir}/configs/c.1/strings/0x409/configuration" "EEM"
 
-    if test -e ${FAKE_GADGETFS}/${gadget_dir}/functions/eem.usb0/dev_addr; then
+    if [[ -e ${FAKE_GADGETFS}/${gadget_dir}/functions/eem.usb0/dev_addr ]]; then
         test_fail "dev_addr should not be set"
     fi
 
-    if test -e ${FAKE_GADGETFS}/${gadget_dir}/functions/eem.usb0/host_addr; then
+    if [[ -e ${FAKE_GADGETFS}/${gadget_dir}/functions/eem.usb0/host_addr ]]; then
         test_fail "host_addr should not be set"
     fi
 
     local func_link="${FAKE_GADGETFS}/${gadget_dir}/configs/c.1/eem.usb0"
-    if ! test -L "${func_link}"; then
+    if [[ ! -L ${func_link} ]]; then
         test_fail "Symlink to the function was not created in the config"
     fi
 
-    local link_dest=$(realpath ${func_link})
+    local link_dest
+    link_dest="$(realpath "${func_link}")"
     if [[ $link_dest != ${FAKE_GADGETFS}/${gadget_dir}/functions/eem.usb0 ]]; then
         test_fail "Symlink points to the wrong file/dir"
     fi
 
-    check_file_content ${FAKE_GADGETFS}/${gadget_dir}/UDC "${bind_device}"
+    check_file_content "${FAKE_GADGETFS}/${gadget_dir}/UDC" "${bind_device}"
 }
 
 test_gadget_creation_alt_iface() {
@@ -196,49 +192,48 @@
     local product_id="0xcafe"
     local bind_device="f80002000.udc"
     local iface_name="iface0"
-    CONFIGFS_HOME=${FAKE_CONFIGFS} ${SUT} --product-id "${product_id}" \
+    if ! CONFIGFS_HOME=${FAKE_CONFIGFS} ${SUT} --product-id "${product_id}" \
         --product-name "${product_name}" \
         --bind-device "${bind_device}" \
-        --iface-name "${iface_name}"
-
-    if test $? -ne 0; then
+        --iface-name "${iface_name}"; then
         test_fail "${SUT} failed"
     fi
 
-    if ! test -d "${FAKE_GADGETFS}/${gadget_dir}"; then
+    if [[ ! -d "${FAKE_GADGETFS}/${gadget_dir}" ]]; then
         test_fail "Gadget was not created!"
     fi
 
-    check_file_content ${FAKE_GADGETFS}/${gadget_dir}/idVendor "0x18d1"
-    check_file_content ${FAKE_GADGETFS}/${gadget_dir}/idProduct "${product_id}"
-    check_file_content ${FAKE_GADGETFS}/${gadget_dir}/strings/0x409/manufacturer "Google"
-    check_file_content ${FAKE_GADGETFS}/${gadget_dir}/strings/0x409/product "${product_name}"
-    check_file_content ${FAKE_GADGETFS}/${gadget_dir}/configs/c.1/MaxPower "100"
-    check_file_content ${FAKE_GADGETFS}/${gadget_dir}/configs/c.1/strings/0x409/configuration "EEM"
+    check_file_content "${FAKE_GADGETFS}/${gadget_dir}/idVendor" "0x18d1"
+    check_file_content "${FAKE_GADGETFS}/${gadget_dir}/idProduct" "${product_id}"
+    check_file_content "${FAKE_GADGETFS}/${gadget_dir}/strings/0x409/manufacturer" "Google"
+    check_file_content "${FAKE_GADGETFS}/${gadget_dir}/strings/0x409/product" "${product_name}"
+    check_file_content "${FAKE_GADGETFS}/${gadget_dir}/configs/c.1/MaxPower" "100"
+    check_file_content "${FAKE_GADGETFS}/${gadget_dir}/configs/c.1/strings/0x409/configuration" "EEM"
 
-    if ! test -d ${FAKE_GADGETFS}/${gadget_dir}/functions/eem.${iface_name}; then
+    if [[ ! -d ${FAKE_GADGETFS}/${gadget_dir}/functions/eem.${iface_name} ]]; then
         test_fail "Function directory was not created"
     fi
 
-    if test -e ${FAKE_GADGETFS}/${gadget_dir}/functions/eem.${iface_name}/dev_addr; then
+    if [[ -e ${FAKE_GADGETFS}/${gadget_dir}/functions/eem.${iface_name}/dev_addr ]]; then
         test_fail "dev_addr should not be set"
     fi
 
-    if test -e ${FAKE_GADGETFS}/${gadget_dir}/functions/eem.${iface_name}/host_addr; then
+    if [[ -e ${FAKE_GADGETFS}/${gadget_dir}/functions/eem.${iface_name}/host_addr ]]; then
         test_fail "host_addr should not be set"
     fi
 
     local func_link="${FAKE_GADGETFS}/${gadget_dir}/configs/c.1/eem.${iface_name}"
-    if ! test -L "${func_link}"; then
+    if [[ ! -L ${func_link} ]]; then
         test_fail "Symlink to the function was not created in the config"
     fi
 
-    local link_dest=$(realpath ${func_link})
-    if [[ $link_dest != ${FAKE_GADGETFS}/${gadget_dir}/functions/eem.${iface_name} ]]; then
+    local link_dest
+    link_dest="$(realpath "${func_link}")"
+    if [[ $link_dest != "${FAKE_GADGETFS}/${gadget_dir}/functions/eem.${iface_name}" ]]; then
         test_fail "Symlink points to the wrong file/dir"
     fi
 
-    check_file_content ${FAKE_GADGETFS}/${gadget_dir}/UDC "${bind_device}"
+    check_file_content "${FAKE_GADGETFS}/${gadget_dir}/UDC" "${bind_device}"
 }