Minor Bash improvements

Based on output of static analysis tool.

https://github.com/koalaman/shellcheck/wiki/SC2166
https://github.com/koalaman/shellcheck/wiki/SC2207
https://github.com/koalaman/shellcheck/wiki/SC2145
https://github.com/koalaman/shellcheck/wiki/SC2001
https://github.com/koalaman/shellcheck/wiki/SC2129
https://github.com/koalaman/shellcheck/wiki/SC2128
https://github.com/koalaman/shellcheck/wiki/SC2004
https://github.com/koalaman/shellcheck/wiki/SC2048

Closes #3162.

PiperOrigin-RevId: 160261247
diff --git a/compile.sh b/compile.sh
index a8819be..ce59be4 100755
--- a/compile.sh
+++ b/compile.sh
@@ -203,7 +203,7 @@
 
   ndk_target="$(get_bind_target //external:android_ndk_for_testing)"
   sdk_target="$(get_bind_target //external:android_sdk_for_testing)"
-  if [ "$ndk_target" = "//:dummy" -o "$sdk_target" = "//:dummy" ]; then
+  if [ "$ndk_target" = "//:dummy" ] || [ "$sdk_target" = "//:dummy" ]; then
     display "$WARNING Android SDK or NDK are not set in the WORKSPACE file. Android tests will not be run."
   fi
 
diff --git a/scripts/ci/build.sh b/scripts/ci/build.sh
index 1f1dade..9ead03c 100755
--- a/scripts/ci/build.sh
+++ b/scripts/ci/build.sh
@@ -120,7 +120,7 @@
     mkdir -p $1/packages
     cp bazel-bin/src/bazel $1/bazel
     # The version with a bundled JDK may not exist on all platforms.
-    if [ "${JAVA_VERSION}" = "1.8" -a -e "bazel-bin/scripts/packages/with-jdk/install.sh" ]; then
+    if [ "${JAVA_VERSION}" = "1.8" ] && [ -e "bazel-bin/scripts/packages/with-jdk/install.sh" ]; then
       cp bazel-bin/scripts/packages/with-jdk/install.sh $1/bazel-${release_label}-installer.sh
       cp bazel-bin/scripts/packages/without-jdk/install.sh $1/bazel-${release_label}-without-jdk-installer.sh
     else
diff --git a/scripts/ci/ci.sh b/scripts/ci/ci.sh
index e25f8d5..546c3b4 100755
--- a/scripts/ci/ci.sh
+++ b/scripts/ci/ci.sh
@@ -50,15 +50,15 @@
 # Get a list of the current files in package form by querying Bazel.
 files=()
 for file in $(git diff --name-only ${COMMIT_RANGE} ); do
-  files+=($(bazel query $file))
-  echo $(bazel query $file)
+  IFS=$'\n' read -r -a files <<< "$(bazel query $file)"
+  bazel query $file
 done
 
 # Query for the associated buildables
 buildables=$(bazel query \
     --keep_going \
     --noshow_progress \
-    "kind(.*_binary, rdeps(//..., set(${files[@]})))")
+    "kind(.*_binary, rdeps(//..., set(${files[*]})))")
 # Run the tests if there were results
 if [[ ! -z $buildables ]]; then
   echo "Building binaries"
@@ -68,7 +68,7 @@
 tests=$(bazel query \
     --keep_going \
     --noshow_progress \
-    "kind(test, rdeps(//..., set(${files[@]}))) except attr('tags', 'manual', //...)")
+    "kind(test, rdeps(//..., set(${files[*]}))) except attr('tags', 'manual', //...)")
 # Run the tests if there were results
 if [[ ! -z $tests ]]; then
   echo "Running tests"
diff --git a/scripts/release/common.sh b/scripts/release/common.sh
index 7cd6d0b..ca8ebd4 100755
--- a/scripts/release/common.sh
+++ b/scripts/release/common.sh
@@ -89,7 +89,7 @@
 # candidate in this release.
 function get_release_branch() {
   local branch_name=$(git_get_branch)
-  if [ -z "$(get_release_name)" -o -z "$(get_release_candidate)" ]; then
+  if [ -z "$(get_release_name)" ] || [ -z "$(get_release_candidate)" ]; then
     echo "Not a release branch: ${branch_name}." >&2
     exit 1
   fi
diff --git a/scripts/release/release.sh b/scripts/release/release.sh
index e3fb691..13c874c 100755
--- a/scripts/release/release.sh
+++ b/scripts/release/release.sh
@@ -102,10 +102,12 @@
   trap "rm -f ${tmpfile}" EXIT
   echo -n "## ${infos}" >${tmpfile}
   if [ -f "${changelog_path}" ]; then
-    echo >>${tmpfile}
-    echo >>${tmpfile}
-    cat "${changelog_path}" >>${tmpfile}
-    echo >>${tmpfile}
+    {
+      echo
+      echo
+      cat "${changelog_path}"
+      echo
+    } >> ${tmpfile}
   fi
   cat "${tmpfile}" > ${changelog_path}
   git add ${changelog_path}
@@ -313,7 +315,7 @@
 
   echo -n "You are about to release branch ${branch} in tag ${tag_name}, confirm? [y/N] "
   read answer
-  if [ "$answer" = "y" -o "$answer" = "Y" ]; then
+  if [ "$answer" = "y" ] || [ "$answer" = "Y" ]; then
     echo "Creating the release commit"
     create_release_commit "${tag_name}"
     set_release_name "${tag_name}"
@@ -352,7 +354,7 @@
   local tag_name=$(get_release_name)
   echo -n "You are about to abandon release ${tag_name}, confirm? [y/N] "
   read answer
-  if [ "$answer" = "y" -o "$answer" = "Y" ]; then
+  if [ "$answer" = "y" ] || [ "$answer" = "Y" ]; then
     git notes --ref=release remove 2>/dev/null || true
     git notes --ref=release-candidate remove 2>/dev/null || true
     git checkout -q master >/dev/null
diff --git a/src/test/shell/testenv.sh b/src/test/shell/testenv.sh
index b109997..f4f7992 100755
--- a/src/test/shell/testenv.sh
+++ b/src/test/shell/testenv.sh
@@ -122,7 +122,7 @@
 # Third-party
 MACHINE_TYPE="$(uname -m)"
 MACHINE_IS_64BIT='no'
-if [ "${MACHINE_TYPE}" = 'amd64' -o "${MACHINE_TYPE}" = 'x86_64' -o "${MACHINE_TYPE}" = 's390x' ]; then
+if [ "${MACHINE_TYPE}" = 'amd64' ] || [ "${MACHINE_TYPE}" = 'x86_64' ] || [ "${MACHINE_TYPE}" = 's390x' ]; then
   MACHINE_IS_64BIT='yes'
 fi
 
@@ -164,6 +164,8 @@
 function copy_tools_directory() {
   cp -RL ${tools_dir}/* tools
   # tools/jdk/BUILD file for JDK 7 is generated.
+  # Only works if there's 0 or 1 matches.
+  # If there are multiple, the test fails.
   if [ -f tools/jdk/BUILD.* ]; then
     cp tools/jdk/BUILD.* tools/jdk/BUILD
   fi
@@ -418,7 +420,7 @@
     done
     touch WORKSPACE
   fi
-  for i in ${workspaces}; do
+  for i in "${workspaces[@]}"; do
     if [ "$i" != "${WORKSPACE_DIR:-}" ]; then
       rm -fr $i
     fi
@@ -435,9 +437,9 @@
       if rm -fr "${BAZEL_INSTALL_BASE}" ; then
         break
       fi
-      if (( $i == 10 )) || (( $i == 30 )) || (( $i == 60 )) ; then
+      if (( i == 10 )) || (( i == 30 )) || (( i == 60 )) ; then
         echo "Test cleanup: couldn't delete ${BAZEL_INSTALL_BASE} \ after $i seconds"
-        echo "(Timeout in $((120-$i)) seconds.)"
+        echo "(Timeout in $((120-i)) seconds.)"
         sleep 1
       fi
     done