Skip to content

Commit

Permalink
Use bash arithmetic checks (#1836)
Browse files Browse the repository at this point in the history
Resolves #1780

This PR align our bash scripts with the [Google Shell Style
Guide](https://google.github.io/styleguide/shellguide.html#s6.9-arithmetic):
> For preference, don’t use `[[ … ]]` at all for numeric comparisons,
use `(( … ))` instead.

Notes:
1. I've replaced numerics checks using `[[ -ne ]]` with `(( != ))`
2. I've replaced numerics checks using `[[ == ]]` with `(( == ))`

<a data-ca-tag
href="https://codeapprove.com/pr/tiny-pilot/tinypilot/1836"><img
src="https://codeapprove.com/external/github-tag-allbg.png" alt="Review
on CodeApprove" /></a>
  • Loading branch information
jdeanwallace authored Aug 13, 2024
1 parent c8b01b2 commit e485124
Show file tree
Hide file tree
Showing 10 changed files with 29 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ while getopts "hq" opt; do
esac
done

if [[ "${EUID}" -ne 0 ]]; then
if (( "${EUID}" != 0 )); then
echo "This script requires root privileges." >&2
echo "Please re-run with sudo:" >&2
echo " sudo ${0}" >&2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Prints the contents of marker sections from a file.
EOF
)"

[[ "${status}" == 0 ]]
(( "${status}" == 0 ))
[[ "${output}" == "${expected_output}" ]]
}

Expand All @@ -34,7 +34,7 @@ Use the '--help' flag for more information
EOF
)"

[[ "${status}" == 1 ]]
(( "${status}" == 1 ))
[[ "${output}" == "${expected_output}" ]]
}

Expand All @@ -46,7 +46,7 @@ Use the '--help' flag for more information
EOF
)"

[[ "${status}" == 1 ]]
(( "${status}" == 1 ))
[[ "${output}" == "${expected_output}" ]]
}

Expand All @@ -58,7 +58,7 @@ Use the '--help' flag for more information
EOF
)"

[[ "${status}" == 1 ]]
(( "${status}" == 1 ))
[[ "${output}" == "${expected_output}" ]]
}

Expand All @@ -71,7 +71,7 @@ Use the '--help' flag for more information
EOF
)"

[[ "${status}" == 1 ]]
(( "${status}" == 1 ))
[[ "${output}" == "${expected_output}" ]]
}

Expand All @@ -84,7 +84,7 @@ line 3
EOF
run print-marker-sections "${target_file}"

[[ "${status}" == 0 ]]
(( "${status}" == 0 ))
[[ "${output}" == "" ]]
}

Expand All @@ -106,7 +106,7 @@ printed
EOF
)"

[[ "${status}" == 0 ]]
(( "${status}" == 0 ))
[[ "${output}" == "${expected_output}" ]]
}

Expand All @@ -131,7 +131,7 @@ printed
EOF
)"

[[ "${status}" == 0 ]]
(( "${status}" == 0 ))
[[ "${output}" == "${expected_output}" ]]
}

Expand All @@ -144,7 +144,7 @@ to be printed
EOF
run print-marker-sections "${target_file}"

[[ "${status}" == 1 ]]
(( "${status}" == 1 ))
[[ "${output}" == "Unmatched start marker" ]]
}

Expand All @@ -157,6 +157,6 @@ final line
EOF
run print-marker-sections "${target_file}"

[[ "${status}" == 1 ]]
(( "${status}" == 1 ))
[[ "${output}" == 'Unmatched end marker' ]]
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ set -e
# Treat undefined environment variables as errors.
set -u

if [[ "${EUID}" -ne 0 ]]; then
if (( "${EUID}" != 0 )); then
echo "This script requires root privileges." >&2
echo "Please re-run with sudo:" >&2
echo " sudo ${0}" >&2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,36 +22,36 @@ Strips TinyPilot marker sections from a file.
EOF
)"

[[ "${status}" == 0 ]]
(( "${status}" == 0 ))
[[ "${output}" == "${expected_output}" ]]
}

rejects-missing-input-arg() { #@test
run strip-marker-sections

[[ "${status}" == 1 ]]
(( "${status}" == 1 ))
[[ "${output}" == 'Input parameter missing: TARGET_FILE' ]]
}

rejects-illegal-flag() { #@test
run strip-marker-sections --foo

[[ "${status}" == 1 ]]
(( "${status}" == 1 ))
[[ "${output}" == 'Illegal option: --foo' ]]
}

rejects-non-existing-file() { #@test
run strip-marker-sections foo-file.txt

[[ "${status}" == 1 ]]
(( "${status}" == 1 ))
[[ "${output}" == 'Not a file: foo-file.txt' ]]
}

rejects-non-file() { #@test
tmp_dir="$(mktemp --directory)"
run strip-marker-sections "${tmp_dir}"

[[ "${status}" == 1 ]]
(( "${status}" == 1 ))
[[ "${output}" == "Not a file: ${tmp_dir}" ]]
}

Expand All @@ -71,7 +71,7 @@ line 3
EOF
)"

[[ "${status}" == 0 ]]
(( "${status}" == 0 ))
[[ "${output}" == "" ]]
[[ "${actual_contents}" == "${expected_contents}" ]]
}
Expand All @@ -92,7 +92,7 @@ EOF
EOF
)"

[[ "${status}" == 0 ]]
(( "${status}" == 0 ))
[[ "${output}" == "" ]]
[[ "${actual_contents}" == "${expected_contents}" ]]
}
Expand All @@ -116,7 +116,7 @@ final line
EOF
)"

[[ "${status}" == 0 ]]
(( "${status}" == 0 ))
[[ "${output}" == "" ]]
[[ "${actual_contents}" == "${expected_contents}" ]]
}
Expand Down Expand Up @@ -145,7 +145,7 @@ final line
EOF
)"

[[ "${status}" == 0 ]]
(( "${status}" == 0 ))
[[ "${output}" == "" ]]
[[ "${actual_contents}" == "${expected_contents}" ]]
}
Expand All @@ -166,7 +166,7 @@ to be stripped
EOF
)"

[[ "${status}" == 1 ]]
(( "${status}" == 1 ))
[[ "${output}" == "Unmatched start marker" ]]
[[ "${actual_contents}" == "${expected_contents}" ]]
}
Expand All @@ -187,7 +187,7 @@ final line
EOF
)"

[[ "${status}" == 1 ]]
(( "${status}" == 1 ))
[[ "${output}" == 'Unmatched end marker' ]]
[[ "${actual_contents}" == "${expected_contents}" ]]
}
2 changes: 1 addition & 1 deletion dev-scripts/check-privilege-guard
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ if [[ -n "${MATCHES}" ]]; then
>&2 echo "${MATCHES}"
>&2 echo 'Please add the following check (or similar) to the above scripts:'
>&2 cat <<'EOF'
if [[ "${EUID}" == 0 ]]; then
if (( "${EUID}" == 0 )); then
>&2 echo "This script doesn't require root privileges."
>&2 echo 'Please re-run as tinypilot:'
>&2 echo " runuser tinypilot --command '$0 $*'"
Expand Down
2 changes: 1 addition & 1 deletion dev-scripts/device/install-bundle
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ set -u
# Exit on first error.
set -e

if [[ "${EUID}" -ne 0 ]]; then
if (( "${EUID}" != 0 )); then
echo "This script requires root privileges." >&2
echo "Please re-run with sudo:" >&2
echo " sudo $0 $*" >&2
Expand Down
2 changes: 1 addition & 1 deletion dev-scripts/enable-mock-scripts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# Exit on first failure.
set -e

if [[ "${EUID}" -ne 0 ]]; then
if (( "${EUID}" != 0 )); then
echo "This script requires root privileges." >&2
echo "Please re-run with sudo:" >&2
echo " sudo ${0}" >&2
Expand Down
2 changes: 1 addition & 1 deletion dev-scripts/enable-passwordless-sudo
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
# To undo this script's changes, run:
# sudo rm /etc/sudoers.d/tinypilot

if [[ "${EUID}" -ne 0 ]]; then
if (( "${EUID}" != 0 )); then
echo "This script requires root privileges." >&2
echo "Please re-run with sudo:" >&2
echo " sudo ${0}" >&2
Expand Down
2 changes: 1 addition & 1 deletion scripts/is-ssh-enabled
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ set -u
# Exit on first error.
set -e

if [[ "${EUID}" == 0 ]]; then
if (( "${EUID}" == 0 )); then
>&2 echo "This script doesn't require root privileges."
>&2 echo 'Please re-run as tinypilot:'
>&2 echo " runuser tinypilot --command '$0 $*'"
Expand Down
2 changes: 1 addition & 1 deletion scripts/streaming-mode
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ set -e
# Exit on unset variable.
set -u

if [[ "${EUID}" == 0 ]]; then
if (( "${EUID}" == 0 )); then
>&2 echo "This script doesn't require root privileges."
>&2 echo 'Please re-run as tinypilot:'
>&2 echo " runuser tinypilot --command '$0 $*'"
Expand Down

0 comments on commit e485124

Please sign in to comment.