Merge pull request #59572 from ipuustin/shell-bugfix2

Automatic merge from submit-queue. If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>.

Fix various bash scripts to make them more robust and uniform

**What this PR does / why we need it**:

The shellcheck tool (https://github.com/koalaman/shellcheck) finds a bunch of issues in kubernetes bash scripts. I started fixing the issues to make the scripts more robust and correct. This PR fixes some "low-hanging fruit" and also cleans up file `hack/update-godep-licenses.sh` in a more complete fashion.

**Special notes for your reviewer**:

**Release note**:

```release-note
NONE
```
pull/8/head
Kubernetes Submit Queue 2018-03-23 22:34:24 -07:00 committed by GitHub
commit 595ed7f613
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 30 additions and 27 deletions

View File

@ -81,10 +81,10 @@ flex_clean() {
umount_silent ${MOUNTER_PATH}
rm -rf ${MOUNTER_PATH}
if [ -n ${IMAGE_URL:-} ]; then
if [[ -n ${IMAGE_URL:-} ]]; then
docker rmi -f ${IMAGE_URL} &> /dev/null || /bin/true
fi
if [ -n ${MOUNTER_DEFAULT_NAME:-} ]; then
if [[ -n ${MOUNTER_DEFAULT_NAME:-} ]]; then
docker rm -f ${MOUNTER_DEFAULT_NAME} &> /dev/null || /bin/true
fi
}

View File

@ -57,7 +57,7 @@ function gcloud-compute-list() {
fi
echo -e "Attempt ${attempt} failed to list ${resource}. Retrying." >&2
attempt=$(($attempt+1))
if [[ ${attempt} > 5 ]]; then
if [[ ${attempt} -gt 5 ]]; then
echo -e "List ${resource} failed!" >&2
exit 2
fi

View File

@ -53,7 +53,7 @@ function detect-k8s-subnetwork() {
local subnetwork_url=$(gcloud compute instances describe \
${KUBE_MASTER} --project=${PROJECT} --zone=${ZONE} \
--format='value(networkInterfaces[0].subnetwork)')
if [ -n ${subnetwork_url} ]; then
if [[ -n ${subnetwork_url} ]]; then
IP_ALIAS_SUBNETWORK=$(echo ${subnetwork_url##*/})
fi
}

View File

@ -74,10 +74,10 @@ process_content () {
# Start search at package root
case ${package} in
github.com/*|golang.org/*|bitbucket.org/*)
package_root=$(echo ${package} |awk -F/ '{ print $1"/"$2"/"$3 }')
package_root=$(echo "${package}" |awk -F/ '{ print $1"/"$2"/"$3 }')
;;
go4.org/*)
package_root=$(echo ${package} |awk -F/ '{ print $1 }')
package_root=$(echo "${package}" |awk -F/ '{ print $1 }')
;;
gopkg.in/*)
# Root of gopkg.in package always ends with '.v(number)' and my contain
@ -85,10 +85,10 @@ process_content () {
# - gopkg.in/yaml.v2
# - gopkg.in/inf.v0
# - gopkg.in/square/go-jose.v2
package_root=$(echo ${package} |grep -oh '.*\.v[0-9]')
package_root=$(echo "${package}" |grep -oh '.*\.v[0-9]')
;;
*)
package_root=$(echo ${package} |awk -F/ '{ print $1"/"$2 }')
package_root=$(echo "${package}" |awk -F/ '{ print $1"/"$2 }')
;;
esac
@ -98,7 +98,7 @@ process_content () {
[[ -d ${DEPS_DIR}/${dir_root} ]] || continue
# One (set) of these is fine
find ${DEPS_DIR}/${dir_root} \
find "${DEPS_DIR}/${dir_root}" \
-xdev -follow -maxdepth ${find_maxdepth} \
-type f "${find_names[@]}"
done | sort -u))
@ -107,9 +107,14 @@ process_content () {
local f
index="${package}-${type}"
if [[ -z "${CONTENT[${index}]-}" ]]; then
for f in ${local_files[@]-}; do
for f in "${local_files[@]-}"; do
if [[ -z "$f" ]]; then
# Set the default value and then check it to prevent
# accessing potentially empty array
continue
fi
# Find some copyright info in any file and break
if egrep -i -wq "${ensure_pattern}" "${f}"; then
if grep -E -i -wq "${ensure_pattern}" "${f}"; then
CONTENT[${index}]="${f}"
break
fi
@ -151,19 +156,17 @@ declare -Ag CONTENT
echo "================================================================================"
echo "= Kubernetes licensed under: ="
echo
cat ${LICENSE_ROOT}/LICENSE
cat "${LICENSE_ROOT}/LICENSE"
echo
echo "= LICENSE $(cat ${LICENSE_ROOT}/LICENSE | md5sum | awk '{print $1}')"
echo "= LICENSE $(cat "${LICENSE_ROOT}/LICENSE" | md5sum | awk '{print $1}')"
echo "================================================================================"
) > ${TMP_LICENSE_FILE}
# Loop through every package in Godeps.json
for PACKAGE in $(cat Godeps/Godeps.json | \
jq -r ".Deps[].ImportPath" | \
sort -f); do
process_content ${PACKAGE} LICENSE
process_content ${PACKAGE} COPYRIGHT
process_content ${PACKAGE} COPYING
for PACKAGE in $(jq -r ".Deps[].ImportPath" < Godeps/Godeps.json | sort -f); do
process_content "${PACKAGE}" LICENSE
process_content "${PACKAGE}" COPYRIGHT
process_content "${PACKAGE}" COPYING
# display content
echo
@ -195,7 +198,7 @@ __EOF__
cat "${file}"
echo
echo "= ${file} $(cat ${file} | md5sum | awk '{print $1}')"
echo "= ${file} $(cat "${file}" | md5sum | awk '{print $1}')"
echo "================================================================================"
echo
done >> ${TMP_LICENSE_FILE}

View File

@ -30,7 +30,7 @@ make -C "${KUBE_ROOT}" WHAT="${BINS[*]}"
clicheck=$(kube::util::find-binary "clicheck")
if ! output=`$clicheck 2>&1`
if ! output=$($clicheck 2>&1)
then
echo "$output"
echo

View File

@ -65,7 +65,7 @@ _kubetmp="${_kubetmp}/kubernetes"
# Do all our work in the new GOPATH
export GOPATH="${_tmpdir}"
pushd "${_kubetmp}" 2>&1 > /dev/null
pushd "${_kubetmp}" > /dev/null 2>&1
# Restore the Godeps into our temp directory
hack/godep-restore.sh
@ -78,11 +78,11 @@ pushd "${_kubetmp}" 2>&1 > /dev/null
# Recreate the Godeps using the nice clean set we just downloaded
hack/godep-save.sh
popd 2>&1 > /dev/null
popd > /dev/null 2>&1
ret=0
pushd "${KUBE_ROOT}" 2>&1 > /dev/null
pushd "${KUBE_ROOT}" > /dev/null 2>&1
# Test for diffs
if ! _out="$(diff -Naupr --ignore-matching-lines='^\s*\"GoVersion\":' --ignore-matching-line='^\s*\"GodepVersion\":' --ignore-matching-lines='^\s*\"Comment\":' Godeps/Godeps.json ${_kubetmp}/Godeps/Godeps.json)"; then
echo "Your Godeps.json is different:" >&2
@ -117,9 +117,9 @@ pushd "${KUBE_ROOT}" 2>&1 > /dev/null
fi
ret=1
fi
popd 2>&1 > /dev/null
popd > /dev/null 2>&1
if [[ ${ret} > 0 ]]; then
if [[ ${ret} -gt 0 ]]; then
exit ${ret}
fi

View File

@ -41,7 +41,7 @@ pushd "${BASH_DIR}" > /dev/null
done
popd > /dev/null
if [[ ${ret} > 0 ]]; then
if [[ ${ret} -gt 0 ]]; then
exit ${ret}
fi