Skip to content

Commit 628007a

Browse files
committed
make go-xcat pass ShellCheck
1 parent c8cb1d6 commit 628007a

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
@@ -52,7 +52,7 @@
5252
function usage()
5353
{
5454
local script="${0##*/}"
55-
local version="$(version)"
55+
local -r version="$(version)"
5656

5757
while read -r ; do echo "${REPLY}" ; done <<-EOF
5858
${script} version ${version}
@@ -106,7 +106,7 @@ function usage()
106106
#
107107
# verbose_usage This function be will be called when user run
108108
# `go-xcat --long-help'.
109-
# Including a bunch of secert usage.
109+
# Including a bunch of secret usage.
110110
#
111111
function verbose_usage()
112112
(
@@ -119,8 +119,7 @@ function verbose_usage()
119119
local -i i
120120
for (( i = 0 ; i < "$1" ; ++i ))
121121
do
122-
read -r -u 42
123-
[[ "$?" -ne "0" ]] && break
122+
read -r -u 42 || break
124123
echo "${REPLY}"
125124
done
126125
}
@@ -135,7 +134,7 @@ function verbose_usage()
135134
while read -r ; do echo "${REPLY}" ; done <<-EOF
136135
check check the version of the installed packages
137136
of xCAT and packages in the repository
138-
smoke test preform basic tests of the xCAT installation
137+
smoke test perform basic tests of the xCAT installation
139138
EOF
140139
println 11
141140
while read -r ; do echo "${REPLY}" ; done <<-EOF
@@ -230,7 +229,7 @@ function warn_if_bad()
230229

231230
# Broken
232231
shift
233-
echo "${script}: $@" >&2
232+
echo "${script}: $*" >&2
234233
return "${rc}"
235234
}
236235

@@ -519,8 +518,8 @@ function check_arch()
519518

520519
function check_linux_distro()
521520
{
522-
local distro="$(source /etc/os-release >/dev/null 2>&1 &&
523-
echo "${ID}")"
521+
local distro
522+
distro="$(awk -F= '/^ID=/{print $2}' /etc/os-release 2>/dev/null)"
524523
[[ -z "${distro}" && -f /etc/redhat-release ]] && distro="rhel"
525524
[[ -z "${distro}" && -f /etc/SuSE-release ]] && distro="sles"
526525
[[ -z "${distro}" && -f /etc/SUSE-brand ]] && distro="sles"
@@ -529,8 +528,8 @@ function check_linux_distro()
529528

530529
function check_linux_version()
531530
{
532-
local ver="$(source /etc/os-release >/dev/null 2>&1 &&
533-
echo "${VERSION_ID}")"
531+
local ver
532+
ver="$(awk -F= '/^VERSION_ID=/{print $2}' /etc/os-release 2>/dev/null)"
534533
[[ -z "${ver}" && -f /etc/redhat-release ]] &&
535534
# Need gawk to do this trick
536535
ver="$(awk '{ match($0, /([.0-9]+)/, a); print substr($0, a[1, "start"], a[1, "length"]); }' \
@@ -613,7 +612,7 @@ function check_package_version_deb()
613612
614613
function check_package_version()
615614
{
616-
function_dispatch "${FUNCNAME}" "$@"
615+
function_dispatch "${FUNCNAME[0]}" "$@"
617616
}
618617
619618
# $@ package names
@@ -898,7 +897,11 @@ function check_repo_version_zypper()
898897
function check_repo_version_apt()
899898
{
900899
type apt-cache >/dev/null 2>&1 || return 255
900+
# Shellcheck doesn't handle identical names in local contexts
901+
# well: https://github.com/koalaman/shellcheck/wiki/SC2178#exceptions
902+
# shellcheck disable=SC2178
901903
local name=""
904+
# shellcheck disable=SC2178
902905
local ver=""
903906
while read -r name ver
904907
do
@@ -927,7 +930,7 @@ function check_repo_version_apt()
927930
928931
function check_repo_version()
929932
{
930-
function_dispatch "${FUNCNAME}" "$@"
933+
function_dispatch "${FUNCNAME[0]}" "$@"
931934
}
932935
933936
# $1 repo id
@@ -1002,13 +1005,13 @@ function get_package_list_apt()
10021005
# $1 repo id
10031006
function get_package_list()
10041007
{
1005-
function_dispatch "${FUNCNAME}" "$@"
1008+
function_dispatch "${FUNCNAME[0]}" "$@"
10061009
}
10071010
10081011
function download_file_curl()
10091012
{
10101013
local script="${0##*/}"
1011-
local version="$(version)"
1014+
local -r version="$(version)"
10121015
local user_agent="${script}/${version} (${GO_XCAT_OS}; ${GO_XCAT_ARCH}; ${GO_XCAT_LINUX_DISTRO} ${GO_XCAT_LINUX_VERSION})"
10131016
type curl >/dev/null 2>&1 || return 255
10141017
local url="$1"
@@ -1115,7 +1118,7 @@ function download_file_curl()
11151118
function download_file_wget()
11161119
{
11171120
local script="${0##*/}"
1118-
local version="$(version)"
1121+
local -r version="$(version)"
11191122
local user_agent="${script}/${version} (${GO_XCAT_OS}; ${GO_XCAT_ARCH}; ${GO_XCAT_LINUX_DISTRO} ${GO_XCAT_LINUX_VERSION})"
11201123
type wget >/dev/null 2>&1 || return 255
11211124
local url="$1"
@@ -1147,7 +1150,7 @@ function download_file_wget()
11471150
11481151
function download_file()
11491152
{
1150-
function_dispatch "${FUNCNAME}" "$@"
1153+
function_dispatch "${FUNCNAME[0]}" "$@"
11511154
}
11521155
11531156
# $1 repo file
@@ -1245,7 +1248,7 @@ function add_repo_by_file_apt()
12451248
12461249
function add_repo_by_file()
12471250
{
1248-
function_dispatch "${FUNCNAME}" "$@"
1251+
function_dispatch "${FUNCNAME[0]}" "$@"
12491252
}
12501253
12511254
# $1 archive
@@ -1256,7 +1259,7 @@ function extract_archive()
12561259
local archive="$1"
12571260
local repo_id="$2"
12581261
local install_path="$3"
1259-
local umask="$(umask)"
1262+
local -r umask="$(umask)"
12601263
local -i ret=0
12611264
12621265
[[ -f "${archive}" ]]
@@ -1276,7 +1279,7 @@ function extract_archive()
12761279
[[ "${PIPESTATUS[0]}" -eq 0 && "${PIPESTATUS[1]}" -eq 0 &&
12771280
"${PIPESTATUS[2]}" -eq 1 ]]
12781281
warn_if_bad "$?" "${archive}: bad compressed tarball" || return 1
1279-
rm -rf "${install_path}/${repo_id}"
1282+
rm -rf "${install_path:-${GO_XCAT_DEFAULT_INSTALL_PATH}}/${repo_id}"
12801283
uncompress -c "${archive}" | ( cd "${install_path}" && tar -x -f - )
12811284
;;
12821285
*".tz"|*".tgz"|*".tar.gz")
@@ -1285,7 +1288,7 @@ function extract_archive()
12851288
[[ "${PIPESTATUS[0]}" -eq 0 && "${PIPESTATUS[1]}" -eq 0 &&
12861289
"${PIPESTATUS[2]}" -eq 1 ]]
12871290
exit_if_bad "$?" "${archive}: bad gzipped tarball" || return 1
1288-
rm -rf "${install_path}/${repo_id}"
1291+
rm -rf "${install_path:-${GO_XCAT_DEFAULT_INSTALL_PATH}}/${repo_id}"
12891292
gzip -d -c "${archive}" | ( cd "${install_path}" && tar -x -f - )
12901293
;;
12911294
*".tbz"|*".tbz2"|*".tar.bz"|*".tar.bz2")
@@ -1294,7 +1297,7 @@ function extract_archive()
12941297
[[ "${PIPESTATUS[0]}" -eq 0 && "${PIPESTATUS[1]}" -eq 0 &&
12951298
"${PIPESTATUS[2]}" -eq 1 ]]
12961299
warn_if_bad "$?" "${archive}: bad bzipped tarball" || return 1
1297-
rm -rf "${install_path}/${repo_id}"
1300+
rm -rf "${install_path:-${GO_XCAT_DEFAULT_INSTALL_PATH}}/${repo_id}"
12981301
bzip2 -d -c "${archive}" | ( cd "${install_path}" && tar -x -f - )
12991302
;;
13001303
*".txz"|*".tar.xz")
@@ -1303,15 +1306,15 @@ function extract_archive()
13031306
[[ "${PIPESTATUS[0]}" -eq 0 && "${PIPESTATUS[1]}" -eq 0 &&
13041307
"${PIPESTATUS[2]}" -eq 1 ]]
13051308
warn_if_bad "$?" "${archive}: bad xzed tarball" || return 1
1306-
rm -rf "${install_path}/${repo_id}"
1309+
rm -rf "${install_path:-${GO_XCAT_DEFAULT_INSTALL_PATH}}/${repo_id}"
13071310
xz -d -c "${archive}" | ( cd "${install_path}" && tar -x -f - )
13081311
;;
13091312
*".tar")
13101313
check_executes tar grep || return 1
13111314
tar -t -f "${archive}" | grep -v "^${repo_id}/"
13121315
[[ "${PIPESTATUS[0]}" -eq 0 && "${PIPESTATUS[1]}" -eq 1 ]]
13131316
warn_if_bad "$?" "${archive}: bad tarball" || return 1
1314-
rm -rf "${install_path}/${repo_id}"
1317+
rm -rf "${install_path:-${GO_XCAT_DEFAULT_INSTALL_PATH}}/${repo_id}"
13151318
( cd "${install_path}" && tar -x -f - ) <"${archive}"
13161319
;;
13171320
*)
@@ -1416,8 +1419,7 @@ function add_repo_by_url_apt()
14161419
local repo_id="$2"
14171420
local tmp=""
14181421
local install_path="${GO_XCAT_DEFAULT_INSTALL_PATH}"
1419-
local codename="$(source /etc/lsb-release >/dev/null 2>&1 &&
1420-
echo "${DISTRIB_CODENAME}")"
1422+
local -r codename="$(awk -F= '/^DISTRIB_CODENAME=/{print $2}' /etc/lsb-release 2>/dev/null)"
14211423
[[ -n "${codename}" ]]
14221424
warn_if_bad "$?" "unknown debian/ubuntu codename" || return 1
14231425
case "${url%%://*}" in
@@ -1467,7 +1469,7 @@ function add_repo_by_url_apt()
14671469
14681470
function add_repo_by_url()
14691471
{
1470-
function_dispatch "${FUNCNAME}" "$@"
1472+
function_dispatch "${FUNCNAME[0]}" "$@"
14711473
}
14721474
14731475
# $1 repo id
@@ -1478,7 +1480,7 @@ function remove_repo_yum()
14781480
# This deleting method is not good enough. Since there could be more
14791481
# than one repository definitions in a single repo file.
14801482
# This is a quick and dirty method.
1481-
rm -f $(grep -l "^\[${repo_id}\]$" "/etc/yum.repos.d/"*".repo" 2>/dev/null)
1483+
grep -l "^\[${repo_id}\]$" "/etc/yum.repos.d/"*".repo" 2>/dev/null | xargs -r rm -f
14821484
case "${repo_id}" in
14831485
"xcat-core")
14841486
mv /etc/yum.repos.d/xCAT-core.repo{,.nouse} 2>/dev/null
@@ -1518,7 +1520,7 @@ function remove_repo_apt()
15181520
15191521
function remove_repo()
15201522
{
1521-
function_dispatch "${FUNCNAME}" "$@"
1523+
function_dispatch "${FUNCNAME[0]}" "$@"
15221524
}
15231525
15241526
# $1 URL
@@ -1584,7 +1586,7 @@ function add_xcat_core_repo_apt()
15841586
15851587
function add_xcat_core_repo()
15861588
{
1587-
function_dispatch "${FUNCNAME}" "$@"
1589+
function_dispatch "${FUNCNAME[0]}" "$@"
15881590
}
15891591
15901592
function add_xcat_dep_repo_yum_or_zypper()
@@ -1675,7 +1677,7 @@ function add_xcat_dep_repo_apt()
16751677
16761678
function add_xcat_dep_repo()
16771679
{
1678-
function_dispatch "${FUNCNAME}" "$@"
1680+
function_dispatch "${FUNCNAME[0]}" "$@"
16791681
}
16801682
16811683
function update_repo_dnf()
@@ -1709,7 +1711,7 @@ function update_repo_apt()
17091711
17101712
function update_repo()
17111713
{
1712-
function_dispatch "${FUNCNAME}" "$@"
1714+
function_dispatch "${FUNCNAME[0]}" "$@"
17131715
}
17141716
17151717
function install_packages_dnf()
@@ -1819,7 +1821,7 @@ function install_packages_apt()
18191821
18201822
function install_packages()
18211823
{
1822-
function_dispatch "${FUNCNAME}" "$@"
1824+
function_dispatch "${FUNCNAME[0]}" "$@"
18231825
}
18241826
18251827
function remove_package_dnf()
@@ -1870,7 +1872,7 @@ function remove_package_apt()
18701872
18711873
function remove_package()
18721874
{
1873-
function_dispatch "${FUNCNAME}" "$@"
1875+
function_dispatch "${FUNCNAME[0]}" "$@"
18741876
}
18751877
18761878
# $1 -y
@@ -1895,7 +1897,7 @@ function purge_package_others()
18951897
# $1 -y
18961898
function purge_package()
18971899
{
1898-
function_dispatch "${FUNCNAME}" "$@"
1900+
function_dispatch "${FUNCNAME[0]}" "$@"
18991901
}
19001902
19011903
# $1 -y
@@ -2004,7 +2006,8 @@ function list_xcat_packages()
20042006
[ "${#GO_XCAT_DEP_PACKAGE_LIST[@]}" -gt "0" ]
20052007
warn_if_bad "$?" "Failed to get package list from repository \`xcat-dep'." || return 1
20062008
2007-
local -i cols="$(type tput >/dev/null 2>&1 && tput cols)"
2009+
local -i cols
2010+
cols="$(type tput >/dev/null 2>&1 && tput cols)"
20082011
[[ "${cols}" -lt 80 ]] && cols=80
20092012
[[ "${cols}" -gt 90 ]] && cols=90
20102013
[[ -t 1 ]] || cols=90
@@ -2147,7 +2150,7 @@ function test_case_001_xcatd()
21472150
# Check if command lsdef can be run
21482151
function test_case_002_lsdef()
21492152
{
2150-
(source /etc/profile.d/xcat.sh && lsdef) >/dev/null 2>&1
2153+
bash -c "source /etc/profile.d/xcat.sh && lsdef" >/dev/null 2>&1
21512154
warn_if_bad "$?" "Attempt of run \`lsdef' failed"
21522155
}
21532156

0 commit comments

Comments
 (0)