Skip to content

Commit a2469aa

Browse files
committed
make go-xcat pass ShellCheck
1 parent 3ab3147 commit a2469aa

File tree

1 file changed

+39
-36
lines changed
  • xCAT-server/share/xcat/tools

1 file changed

+39
-36
lines changed

xCAT-server/share/xcat/tools/go-xcat

Lines changed: 39 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
function usage()
2929
{
3030
local script="${0##*/}"
31-
local version="$(version)"
31+
local -r version="$(version)"
3232

3333
while read -r ; do echo "${REPLY}" ; done <<-EOF
3434
${script} version ${version}
@@ -81,7 +81,7 @@ function usage()
8181
#
8282
# verbose_usage This function be will be called when user run
8383
# `go-xcat --long-help'.
84-
# Including a bunch of secert usage.
84+
# Including a bunch of secret usage.
8585
#
8686
function verbose_usage()
8787
(
@@ -94,8 +94,7 @@ function verbose_usage()
9494
local -i i
9595
for (( i = 0 ; i < "$1" ; ++i ))
9696
do
97-
read -r -u 42
98-
[[ "$?" -ne "0" ]] && break
97+
read -r -u 42 || break
9998
echo "${REPLY}"
10099
done
101100
}
@@ -110,7 +109,7 @@ function verbose_usage()
110109
while read -r ; do echo "${REPLY}" ; done <<-EOF
111110
check check the version of the installed packages
112111
of xCAT and packages in the repository
113-
smoke test preform basic tests of the xCAT installation
112+
smoke test perform basic tests of the xCAT installation
114113
EOF
115114
println 11
116115
while read -r ; do echo "${REPLY}" ; done <<-EOF
@@ -201,7 +200,7 @@ function warn_if_bad()
201200

202201
# Broken
203202
shift
204-
echo "${script}: $@" >&2
203+
echo "${script}: $*" >&2
205204
return "${rc}"
206205
}
207206

@@ -490,17 +489,17 @@ function check_arch()
490489

491490
function check_linux_distro()
492491
{
493-
local distro="$(source /etc/os-release >/dev/null 2>&1 &&
494-
echo "${ID}")"
492+
local distro
493+
distro="$(awk -F= '/^ID=/{print $2}' /etc/os-release 2>/dev/null)"
495494
[[ -z "${distro}" && -f /etc/redhat-release ]] && distro="rhel"
496495
[[ -z "${distro}" && -f /etc/SuSE-release ]] && distro="sles"
497496
echo "${distro}"
498497
}
499498

500499
function check_linux_version()
501500
{
502-
local ver="$(source /etc/os-release >/dev/null 2>&1 &&
503-
echo "${VERSION_ID}")"
501+
local ver
502+
ver="$(awk -F= '/^VERSION_ID=/{print $2}' /etc/os-release 2>/dev/null)"
504503
[[ -z "${ver}" && -f /etc/redhat-release ]] &&
505504
# Need gawk to do this trick
506505
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()
581580
582581
function check_package_version()
583582
{
584-
function_dispatch "${FUNCNAME}" "$@"
583+
function_dispatch "${FUNCNAME[0]}" "$@"
585584
}
586585
587586
# $@ package names
@@ -679,7 +678,11 @@ function check_repo_version_zypper()
679678
function check_repo_version_apt()
680679
{
681680
type apt-cache >/dev/null 2>&1 || return 255
681+
# Shellcheck doesn't handle identical names in local contexts
682+
# well: https://github.com/koalaman/shellcheck/wiki/SC2178#exceptions
683+
# shellcheck disable=SC2178
682684
local name=""
685+
# shellcheck disable=SC2178
683686
local ver=""
684687
while read -r name ver
685688
do
@@ -708,7 +711,7 @@ function check_repo_version_apt()
708711
709712
function check_repo_version()
710713
{
711-
function_dispatch "${FUNCNAME}" "$@"
714+
function_dispatch "${FUNCNAME[0]}" "$@"
712715
}
713716
714717
# $1 repo id
@@ -783,13 +786,13 @@ function get_package_list_apt()
783786
# $1 repo id
784787
function get_package_list()
785788
{
786-
function_dispatch "${FUNCNAME}" "$@"
789+
function_dispatch "${FUNCNAME[0]}" "$@"
787790
}
788791
789792
function download_file_curl()
790793
{
791794
local script="${0##*/}"
792-
local version="$(version)"
795+
local -r version="$(version)"
793796
local user_agent="${script}/${version} (${GO_XCAT_OS}; ${GO_XCAT_ARCH}; ${GO_XCAT_LINUX_DISTRO} ${GO_XCAT_LINUX_VERSION})"
794797
type curl >/dev/null 2>&1 || return 255
795798
local url="$1"
@@ -896,7 +899,7 @@ function download_file_curl()
896899
function download_file_wget()
897900
{
898901
local script="${0##*/}"
899-
local version="$(version)"
902+
local -r version="$(version)"
900903
local user_agent="${script}/${version} (${GO_XCAT_OS}; ${GO_XCAT_ARCH}; ${GO_XCAT_LINUX_DISTRO} ${GO_XCAT_LINUX_VERSION})"
901904
type wget >/dev/null 2>&1 || return 255
902905
local url="$1"
@@ -928,7 +931,7 @@ function download_file_wget()
928931
929932
function download_file()
930933
{
931-
function_dispatch "${FUNCNAME}" "$@"
934+
function_dispatch "${FUNCNAME[0]}" "$@"
932935
}
933936
934937
# $1 repo file
@@ -1026,7 +1029,7 @@ function add_repo_by_file_apt()
10261029
10271030
function add_repo_by_file()
10281031
{
1029-
function_dispatch "${FUNCNAME}" "$@"
1032+
function_dispatch "${FUNCNAME[0]}" "$@"
10301033
}
10311034
10321035
# $1 archive
@@ -1037,7 +1040,7 @@ function extract_archive()
10371040
local archive="$1"
10381041
local repo_id="$2"
10391042
local install_path="$3"
1040-
local umask="$(umask)"
1043+
local -r umask="$(umask)"
10411044
local -i ret=0
10421045
10431046
[[ -f "${archive}" ]]
@@ -1057,7 +1060,7 @@ function extract_archive()
10571060
[[ "${PIPESTATUS[0]}" -eq 0 && "${PIPESTATUS[1]}" -eq 0 &&
10581061
"${PIPESTATUS[2]}" -eq 1 ]]
10591062
warn_if_bad "$?" "${archive}: bad compressed tarball" || return 1
1060-
rm -rf "${install_path}/${repo_id}"
1063+
rm -rf "${install_path:-${GO_XCAT_DEFAULT_INSTALL_PATH}}/${repo_id}"
10611064
uncompress -c "${archive}" | ( cd "${install_path}" && tar -x -f - )
10621065
;;
10631066
*".tz"|*".tgz"|*".tar.gz")
@@ -1066,7 +1069,7 @@ function extract_archive()
10661069
[[ "${PIPESTATUS[0]}" -eq 0 && "${PIPESTATUS[1]}" -eq 0 &&
10671070
"${PIPESTATUS[2]}" -eq 1 ]]
10681071
exit_if_bad "$?" "${archive}: bad gzipped tarball" || return 1
1069-
rm -rf "${install_path}/${repo_id}"
1072+
rm -rf "${install_path:-${GO_XCAT_DEFAULT_INSTALL_PATH}}/${repo_id}"
10701073
gzip -d -c "${archive}" | ( cd "${install_path}" && tar -x -f - )
10711074
;;
10721075
*".tbz"|*".tbz2"|*".tar.bz"|*".tar.bz2")
@@ -1075,7 +1078,7 @@ function extract_archive()
10751078
[[ "${PIPESTATUS[0]}" -eq 0 && "${PIPESTATUS[1]}" -eq 0 &&
10761079
"${PIPESTATUS[2]}" -eq 1 ]]
10771080
warn_if_bad "$?" "${archive}: bad bzipped tarball" || return 1
1078-
rm -rf "${install_path}/${repo_id}"
1081+
rm -rf "${install_path:-${GO_XCAT_DEFAULT_INSTALL_PATH}}/${repo_id}"
10791082
bzip2 -d -c "${archive}" | ( cd "${install_path}" && tar -x -f - )
10801083
;;
10811084
*".txz"|*".tar.xz")
@@ -1084,15 +1087,15 @@ function extract_archive()
10841087
[[ "${PIPESTATUS[0]}" -eq 0 && "${PIPESTATUS[1]}" -eq 0 &&
10851088
"${PIPESTATUS[2]}" -eq 1 ]]
10861089
warn_if_bad "$?" "${archive}: bad xzed tarball" || return 1
1087-
rm -rf "${install_path}/${repo_id}"
1090+
rm -rf "${install_path:-${GO_XCAT_DEFAULT_INSTALL_PATH}}/${repo_id}"
10881091
xz -d -c "${archive}" | ( cd "${install_path}" && tar -x -f - )
10891092
;;
10901093
*".tar")
10911094
check_executes tar grep || return 1
10921095
tar -t -f "${archive}" | grep -v "^${repo_id}/"
10931096
[[ "${PIPESTATUS[0]}" -eq 0 && "${PIPESTATUS[1]}" -eq 1 ]]
10941097
warn_if_bad "$?" "${archive}: bad tarball" || return 1
1095-
rm -rf "${install_path}/${repo_id}"
1098+
rm -rf "${install_path:-${GO_XCAT_DEFAULT_INSTALL_PATH}}/${repo_id}"
10961099
( cd "${install_path}" && tar -x -f - ) <"${archive}"
10971100
;;
10981101
*)
@@ -1197,8 +1200,7 @@ function add_repo_by_url_apt()
11971200
local repo_id="$2"
11981201
local tmp=""
11991202
local install_path="${GO_XCAT_DEFAULT_INSTALL_PATH}"
1200-
local codename="$(source /etc/lsb-release >/dev/null 2>&1 &&
1201-
echo "${DISTRIB_CODENAME}")"
1203+
local -r codename="$(awk -F= '/^DISTRIB_CODENAME=/{print $2}' /etc/lsb-release 2>/dev/null)"
12021204
[[ -n "${codename}" ]]
12031205
warn_if_bad "$?" "unknown debian/ubuntu codename" || return 1
12041206
case "${url%%://*}" in
@@ -1248,7 +1250,7 @@ function add_repo_by_url_apt()
12481250
12491251
function add_repo_by_url()
12501252
{
1251-
function_dispatch "${FUNCNAME}" "$@"
1253+
function_dispatch "${FUNCNAME[0]}" "$@"
12521254
}
12531255
12541256
# $1 repo id
@@ -1259,7 +1261,7 @@ function remove_repo_yum()
12591261
# This deleting method is not good enough. Since there could be more
12601262
# than one repository definitions in a single repo file.
12611263
# This is a quick and dirty method.
1262-
rm -f $(grep -l "^\[${repo_id}\]$" "/etc/yum.repos.d/"*".repo" 2>/dev/null)
1264+
grep -l "^\[${repo_id}\]$" "/etc/yum.repos.d/"*".repo" 2>/dev/null | xargs -r rm -f
12631265
case "${repo_id}" in
12641266
"xcat-core")
12651267
mv /etc/yum.repos.d/xCAT-core.repo{,.nouse} 2>/dev/null
@@ -1299,7 +1301,7 @@ function remove_repo_apt()
12991301
13001302
function remove_repo()
13011303
{
1302-
function_dispatch "${FUNCNAME}" "$@"
1304+
function_dispatch "${FUNCNAME[0]}" "$@"
13031305
}
13041306
13051307
# $1 URL
@@ -1363,7 +1365,7 @@ function add_xcat_core_repo_apt()
13631365
13641366
function add_xcat_core_repo()
13651367
{
1366-
function_dispatch "${FUNCNAME}" "$@"
1368+
function_dispatch "${FUNCNAME[0]}" "$@"
13671369
}
13681370
13691371
function add_xcat_dep_repo_yum_or_zypper()
@@ -1449,7 +1451,7 @@ function add_xcat_dep_repo_apt()
14491451
14501452
function add_xcat_dep_repo()
14511453
{
1452-
function_dispatch "${FUNCNAME}" "$@"
1454+
function_dispatch "${FUNCNAME[0]}" "$@"
14531455
}
14541456
14551457
function update_repo_dnf()
@@ -1483,7 +1485,7 @@ function update_repo_apt()
14831485
14841486
function update_repo()
14851487
{
1486-
function_dispatch "${FUNCNAME}" "$@"
1488+
function_dispatch "${FUNCNAME[0]}" "$@"
14871489
}
14881490
14891491
function install_packages_dnf()
@@ -1530,7 +1532,7 @@ function install_packages_apt()
15301532
15311533
function install_packages()
15321534
{
1533-
function_dispatch "${FUNCNAME}" "$@"
1535+
function_dispatch "${FUNCNAME[0]}" "$@"
15341536
}
15351537
15361538
function remove_package_dnf()
@@ -1576,7 +1578,7 @@ function remove_package_apt()
15761578
15771579
function remove_package()
15781580
{
1579-
function_dispatch "${FUNCNAME}" "$@"
1581+
function_dispatch "${FUNCNAME[0]}" "$@"
15801582
}
15811583
15821584
# $1 -y
@@ -1596,7 +1598,7 @@ function purge_package_others()
15961598
# $1 -y
15971599
function purge_package()
15981600
{
1599-
function_dispatch "${FUNCNAME}" "$@"
1601+
function_dispatch "${FUNCNAME[0]}" "$@"
16001602
}
16011603
16021604
# $1 -y
@@ -1705,7 +1707,8 @@ function list_xcat_packages()
17051707
[ "${#GO_XCAT_DEP_PACKAGE_LIST[@]}" -gt "0" ]
17061708
warn_if_bad "$?" "Failed to get package list from repository \`xcat-dep'." || return 1
17071709
1708-
local -i cols="$(type tput >/dev/null 2>&1 && tput cols)"
1710+
local -i cols
1711+
cols="$(type tput >/dev/null 2>&1 && tput cols)"
17091712
[[ "${cols}" -lt 80 ]] && cols=80
17101713
[[ "${cols}" -gt 90 ]] && cols=90
17111714
[[ -t 1 ]] || cols=90
@@ -1848,7 +1851,7 @@ function test_case_001_xcatd()
18481851
# Check if command lsdef can be run
18491852
function test_case_002_lsdef()
18501853
{
1851-
(source /etc/profile.d/xcat.sh && lsdef) >/dev/null 2>&1
1854+
bash -c "source /etc/profile.d/xcat.sh && lsdef" >/dev/null 2>&1
18521855
warn_if_bad "$?" "Attempt of run \`lsdef' failed"
18531856
}
18541857

0 commit comments

Comments
 (0)