From a2469aaf9dc0f3e2d86a4c1c8e9c2fdec7d90a65 Mon Sep 17 00:00:00 2001 From: Samveen Date: Fri, 5 Jul 2019 14:30:57 +0530 Subject: [PATCH] make go-xcat pass ShellCheck --- xCAT-server/share/xcat/tools/go-xcat | 75 +++++++++++++++------------- 1 file changed, 39 insertions(+), 36 deletions(-) diff --git a/xCAT-server/share/xcat/tools/go-xcat b/xCAT-server/share/xcat/tools/go-xcat index 75b15ea980..6c95c90923 100755 --- a/xCAT-server/share/xcat/tools/go-xcat +++ b/xCAT-server/share/xcat/tools/go-xcat @@ -28,7 +28,7 @@ function usage() { local script="${0##*/}" - local version="$(version)" + local -r version="$(version)" while read -r ; do echo "${REPLY}" ; done <<-EOF ${script} version ${version} @@ -81,7 +81,7 @@ function usage() # # verbose_usage This function be will be called when user run # `go-xcat --long-help'. -# Including a bunch of secert usage. +# Including a bunch of secret usage. # function verbose_usage() ( @@ -94,8 +94,7 @@ function verbose_usage() local -i i for (( i = 0 ; i < "$1" ; ++i )) do - read -r -u 42 - [[ "$?" -ne "0" ]] && break + read -r -u 42 || break echo "${REPLY}" done } @@ -110,7 +109,7 @@ function verbose_usage() while read -r ; do echo "${REPLY}" ; done <<-EOF check check the version of the installed packages of xCAT and packages in the repository - smoke test preform basic tests of the xCAT installation + smoke test perform basic tests of the xCAT installation EOF println 11 while read -r ; do echo "${REPLY}" ; done <<-EOF @@ -201,7 +200,7 @@ function warn_if_bad() # Broken shift - echo "${script}: $@" >&2 + echo "${script}: $*" >&2 return "${rc}" } @@ -490,8 +489,8 @@ function check_arch() function check_linux_distro() { - local distro="$(source /etc/os-release >/dev/null 2>&1 && - echo "${ID}")" + local distro + distro="$(awk -F= '/^ID=/{print $2}' /etc/os-release 2>/dev/null)" [[ -z "${distro}" && -f /etc/redhat-release ]] && distro="rhel" [[ -z "${distro}" && -f /etc/SuSE-release ]] && distro="sles" echo "${distro}" @@ -499,8 +498,8 @@ function check_linux_distro() function check_linux_version() { - local ver="$(source /etc/os-release >/dev/null 2>&1 && - echo "${VERSION_ID}")" + local ver + ver="$(awk -F= '/^VERSION_ID=/{print $2}' /etc/os-release 2>/dev/null)" [[ -z "${ver}" && -f /etc/redhat-release ]] && # Need gawk to do this trick ver="$(awk '{ match($0, /([.0-9]+)/, a); print substr($0, a[1, "start"], a[1, "length"]); }' \ @@ -581,7 +580,7 @@ function check_package_version_deb() function check_package_version() { - function_dispatch "${FUNCNAME}" "$@" + function_dispatch "${FUNCNAME[0]}" "$@" } # $@ package names @@ -679,7 +678,11 @@ function check_repo_version_zypper() function check_repo_version_apt() { type apt-cache >/dev/null 2>&1 || return 255 + # Shellcheck doesn't handle identical names in local contexts + # well: https://github.com/koalaman/shellcheck/wiki/SC2178#exceptions + # shellcheck disable=SC2178 local name="" + # shellcheck disable=SC2178 local ver="" while read -r name ver do @@ -708,7 +711,7 @@ function check_repo_version_apt() function check_repo_version() { - function_dispatch "${FUNCNAME}" "$@" + function_dispatch "${FUNCNAME[0]}" "$@" } # $1 repo id @@ -783,13 +786,13 @@ function get_package_list_apt() # $1 repo id function get_package_list() { - function_dispatch "${FUNCNAME}" "$@" + function_dispatch "${FUNCNAME[0]}" "$@" } function download_file_curl() { local script="${0##*/}" - local version="$(version)" + local -r version="$(version)" local user_agent="${script}/${version} (${GO_XCAT_OS}; ${GO_XCAT_ARCH}; ${GO_XCAT_LINUX_DISTRO} ${GO_XCAT_LINUX_VERSION})" type curl >/dev/null 2>&1 || return 255 local url="$1" @@ -896,7 +899,7 @@ function download_file_curl() function download_file_wget() { local script="${0##*/}" - local version="$(version)" + local -r version="$(version)" local user_agent="${script}/${version} (${GO_XCAT_OS}; ${GO_XCAT_ARCH}; ${GO_XCAT_LINUX_DISTRO} ${GO_XCAT_LINUX_VERSION})" type wget >/dev/null 2>&1 || return 255 local url="$1" @@ -928,7 +931,7 @@ function download_file_wget() function download_file() { - function_dispatch "${FUNCNAME}" "$@" + function_dispatch "${FUNCNAME[0]}" "$@" } # $1 repo file @@ -1026,7 +1029,7 @@ function add_repo_by_file_apt() function add_repo_by_file() { - function_dispatch "${FUNCNAME}" "$@" + function_dispatch "${FUNCNAME[0]}" "$@" } # $1 archive @@ -1037,7 +1040,7 @@ function extract_archive() local archive="$1" local repo_id="$2" local install_path="$3" - local umask="$(umask)" + local -r umask="$(umask)" local -i ret=0 [[ -f "${archive}" ]] @@ -1057,7 +1060,7 @@ function extract_archive() [[ "${PIPESTATUS[0]}" -eq 0 && "${PIPESTATUS[1]}" -eq 0 && "${PIPESTATUS[2]}" -eq 1 ]] warn_if_bad "$?" "${archive}: bad compressed tarball" || return 1 - rm -rf "${install_path}/${repo_id}" + rm -rf "${install_path:-${GO_XCAT_DEFAULT_INSTALL_PATH}}/${repo_id}" uncompress -c "${archive}" | ( cd "${install_path}" && tar -x -f - ) ;; *".tz"|*".tgz"|*".tar.gz") @@ -1066,7 +1069,7 @@ function extract_archive() [[ "${PIPESTATUS[0]}" -eq 0 && "${PIPESTATUS[1]}" -eq 0 && "${PIPESTATUS[2]}" -eq 1 ]] exit_if_bad "$?" "${archive}: bad gzipped tarball" || return 1 - rm -rf "${install_path}/${repo_id}" + rm -rf "${install_path:-${GO_XCAT_DEFAULT_INSTALL_PATH}}/${repo_id}" gzip -d -c "${archive}" | ( cd "${install_path}" && tar -x -f - ) ;; *".tbz"|*".tbz2"|*".tar.bz"|*".tar.bz2") @@ -1075,7 +1078,7 @@ function extract_archive() [[ "${PIPESTATUS[0]}" -eq 0 && "${PIPESTATUS[1]}" -eq 0 && "${PIPESTATUS[2]}" -eq 1 ]] warn_if_bad "$?" "${archive}: bad bzipped tarball" || return 1 - rm -rf "${install_path}/${repo_id}" + rm -rf "${install_path:-${GO_XCAT_DEFAULT_INSTALL_PATH}}/${repo_id}" bzip2 -d -c "${archive}" | ( cd "${install_path}" && tar -x -f - ) ;; *".txz"|*".tar.xz") @@ -1084,7 +1087,7 @@ function extract_archive() [[ "${PIPESTATUS[0]}" -eq 0 && "${PIPESTATUS[1]}" -eq 0 && "${PIPESTATUS[2]}" -eq 1 ]] warn_if_bad "$?" "${archive}: bad xzed tarball" || return 1 - rm -rf "${install_path}/${repo_id}" + rm -rf "${install_path:-${GO_XCAT_DEFAULT_INSTALL_PATH}}/${repo_id}" xz -d -c "${archive}" | ( cd "${install_path}" && tar -x -f - ) ;; *".tar") @@ -1092,7 +1095,7 @@ function extract_archive() tar -t -f "${archive}" | grep -v "^${repo_id}/" [[ "${PIPESTATUS[0]}" -eq 0 && "${PIPESTATUS[1]}" -eq 1 ]] warn_if_bad "$?" "${archive}: bad tarball" || return 1 - rm -rf "${install_path}/${repo_id}" + rm -rf "${install_path:-${GO_XCAT_DEFAULT_INSTALL_PATH}}/${repo_id}" ( cd "${install_path}" && tar -x -f - ) <"${archive}" ;; *) @@ -1197,8 +1200,7 @@ function add_repo_by_url_apt() local repo_id="$2" local tmp="" local install_path="${GO_XCAT_DEFAULT_INSTALL_PATH}" - local codename="$(source /etc/lsb-release >/dev/null 2>&1 && - echo "${DISTRIB_CODENAME}")" + local -r codename="$(awk -F= '/^DISTRIB_CODENAME=/{print $2}' /etc/lsb-release 2>/dev/null)" [[ -n "${codename}" ]] warn_if_bad "$?" "unknown debian/ubuntu codename" || return 1 case "${url%%://*}" in @@ -1248,7 +1250,7 @@ function add_repo_by_url_apt() function add_repo_by_url() { - function_dispatch "${FUNCNAME}" "$@" + function_dispatch "${FUNCNAME[0]}" "$@" } # $1 repo id @@ -1259,7 +1261,7 @@ function remove_repo_yum() # This deleting method is not good enough. Since there could be more # than one repository definitions in a single repo file. # This is a quick and dirty method. - rm -f $(grep -l "^\[${repo_id}\]$" "/etc/yum.repos.d/"*".repo" 2>/dev/null) + grep -l "^\[${repo_id}\]$" "/etc/yum.repos.d/"*".repo" 2>/dev/null | xargs -r rm -f case "${repo_id}" in "xcat-core") mv /etc/yum.repos.d/xCAT-core.repo{,.nouse} 2>/dev/null @@ -1299,7 +1301,7 @@ function remove_repo_apt() function remove_repo() { - function_dispatch "${FUNCNAME}" "$@" + function_dispatch "${FUNCNAME[0]}" "$@" } # $1 URL @@ -1363,7 +1365,7 @@ function add_xcat_core_repo_apt() function add_xcat_core_repo() { - function_dispatch "${FUNCNAME}" "$@" + function_dispatch "${FUNCNAME[0]}" "$@" } function add_xcat_dep_repo_yum_or_zypper() @@ -1449,7 +1451,7 @@ function add_xcat_dep_repo_apt() function add_xcat_dep_repo() { - function_dispatch "${FUNCNAME}" "$@" + function_dispatch "${FUNCNAME[0]}" "$@" } function update_repo_dnf() @@ -1483,7 +1485,7 @@ function update_repo_apt() function update_repo() { - function_dispatch "${FUNCNAME}" "$@" + function_dispatch "${FUNCNAME[0]}" "$@" } function install_packages_dnf() @@ -1530,7 +1532,7 @@ function install_packages_apt() function install_packages() { - function_dispatch "${FUNCNAME}" "$@" + function_dispatch "${FUNCNAME[0]}" "$@" } function remove_package_dnf() @@ -1576,7 +1578,7 @@ function remove_package_apt() function remove_package() { - function_dispatch "${FUNCNAME}" "$@" + function_dispatch "${FUNCNAME[0]}" "$@" } # $1 -y @@ -1596,7 +1598,7 @@ function purge_package_others() # $1 -y function purge_package() { - function_dispatch "${FUNCNAME}" "$@" + function_dispatch "${FUNCNAME[0]}" "$@" } # $1 -y @@ -1705,7 +1707,8 @@ function list_xcat_packages() [ "${#GO_XCAT_DEP_PACKAGE_LIST[@]}" -gt "0" ] warn_if_bad "$?" "Failed to get package list from repository \`xcat-dep'." || return 1 - local -i cols="$(type tput >/dev/null 2>&1 && tput cols)" + local -i cols + cols="$(type tput >/dev/null 2>&1 && tput cols)" [[ "${cols}" -lt 80 ]] && cols=80 [[ "${cols}" -gt 90 ]] && cols=90 [[ -t 1 ]] || cols=90 @@ -1848,7 +1851,7 @@ function test_case_001_xcatd() # Check if command lsdef can be run function test_case_002_lsdef() { - (source /etc/profile.d/xcat.sh && lsdef) >/dev/null 2>&1 + bash -c "source /etc/profile.d/xcat.sh && lsdef" >/dev/null 2>&1 warn_if_bad "$?" "Attempt of run \`lsdef' failed" }