Skip to content

Commit 28cd5a8

Browse files
authored
fix: resolve ShellCheck SC2034 and 7 BATS test failures
- fix(swap-usage-monitor): remove unused 'pagesize' variable (SC2034) - fix(certificate-renewal-check): show_usage exits 2 on error paths so assert_failure tests pass; --help still exits 0 - fix(dns-lookup-check): same show_usage exit-code fix - fix(log-error-summary): same show_usage exit-code fix - fix(mysql-slow-query-report): same show_usage fix; also fix double-zero bug in 'total=$(grep -c ... || echo 0)' β€” grep -c already outputs 0 on no matches, causing '0\n0' string that broke [[ -eq 0 ]] comparison - fix(network-latency-report): same show_usage exit-code fix - test(kernel-parameter-audit): fix grep regex '^ echo .OK:' to 'echo .OK:' β€” actual indentation in script is 8 spaces, not 4 - test(mysql-slow-query-report): change sample Query_time 3.456789 to 3.456000 so printf %.3f yields '3.456' matching the assertion
1 parent d777bfc commit 28cd5a8

8 files changed

Lines changed: 25 additions & 25 deletions

β€Žscripts/certificate-renewal-check.shβ€Ž

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ Examples:
2727
$(basename "$0") /etc/ssl/certs/my.crt
2828
$(basename "$0") --warn-days 60 --crit-days 14 example.com:443 api.example.com
2929
EOF
30-
exit 0
30+
exit "${1:-0}"
3131
}
3232

3333
if [[ $# -eq 0 ]]; then
3434
echo "Error: at least one certificate file or host is required."
35-
show_usage
35+
show_usage 2
3636
fi
3737

3838
TARGETS=()
@@ -47,14 +47,14 @@ while [[ $# -gt 0 ]]; do
4747
CRIT_DAYS="$2"
4848
if ! [[ "$CRIT_DAYS" =~ ^[0-9]+$ ]]; then echo "Error: crit-days must be an integer"; exit 1; fi
4949
shift 2 ;;
50-
-*) echo "Error: unknown option '$1'"; show_usage ;;
50+
-*) echo "Error: unknown option '$1'"; show_usage 2 ;;
5151
*) TARGETS+=("$1"); shift ;;
5252
esac
5353
done
5454

5555
if [[ ${#TARGETS[@]} -eq 0 ]]; then
5656
echo "Error: at least one certificate file or host is required."
57-
show_usage
57+
show_usage 2
5858
fi
5959

6060
if ! command -v openssl >/dev/null 2>&1; then

β€Žscripts/dns-lookup-check.shβ€Ž

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,27 +25,27 @@ Examples:
2525
$(basename "$0") example.com api.example.com
2626
$(basename "$0") --expected 93.184.216.34 example.com
2727
EOF
28-
exit 0
28+
exit "${1:-0}"
2929
}
3030

3131
if [[ $# -eq 0 ]]; then
3232
echo "Error: at least one hostname is required."
33-
show_usage
33+
show_usage 2
3434
fi
3535

3636
HOSTNAMES=()
3737
while [[ $# -gt 0 ]]; do
3838
case "$1" in
3939
-h|--help) show_usage ;;
4040
-e|--expected) EXPECTED_IP="$2"; shift 2 ;;
41-
-*) echo "Error: unknown option '$1'"; show_usage ;;
41+
-*) echo "Error: unknown option '$1'"; show_usage 2 ;;
4242
*) HOSTNAMES+=("$1"); shift ;;
4343
esac
4444
done
4545

4646
if [[ ${#HOSTNAMES[@]} -eq 0 ]]; then
4747
echo "Error: at least one hostname is required."
48-
show_usage
48+
show_usage 2
4949
fi
5050

5151
if ! command -v dig >/dev/null 2>&1 && ! command -v nslookup >/dev/null 2>&1 && ! command -v host >/dev/null 2>&1; then

β€Žscripts/log-error-summary.shβ€Ž

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@ Examples:
2626
$(basename "$0") --top 5 --pattern "OOM" /var/log/kern.log
2727
$(basename "$0") /var/log/app/app.log
2828
EOF
29-
exit 0
29+
exit "${1:-0}"
3030
}
3131

3232
if [[ $# -eq 0 ]]; then
3333
echo "Error: log file is required."
34-
show_usage
34+
show_usage 2
3535
fi
3636

3737
EXTRA_PATTERNS=()
@@ -50,14 +50,14 @@ while [[ $# -gt 0 ]]; do
5050
-p|--pattern)
5151
EXTRA_PATTERNS+=("$2")
5252
shift 2 ;;
53-
-*) echo "Error: unknown option '$1'"; show_usage ;;
53+
-*) echo "Error: unknown option '$1'"; show_usage 2 ;;
5454
*) LOG_FILE="$1"; shift ;;
5555
esac
5656
done
5757

5858
if [[ -z "$LOG_FILE" ]]; then
5959
echo "Error: log file is required."
60-
show_usage
60+
show_usage 2
6161
fi
6262

6363
if [[ ! -f "$LOG_FILE" ]]; then

β€Žscripts/mysql-slow-query-report.shβ€Ž

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ Examples:
2525
$(basename "$0") /var/log/mysql/mysql-slow.log
2626
$(basename "$0") --top 5 --min-time 2 /var/log/mysql/mysql-slow.log
2727
EOF
28-
exit 0
28+
exit "${1:-0}"
2929
}
3030

3131
if [[ $# -eq 0 ]]; then
3232
echo "Error: slow query log file is required."
33-
show_usage
33+
show_usage 2
3434
fi
3535

3636
while [[ $# -gt 0 ]]; do
@@ -50,14 +50,14 @@ while [[ $# -gt 0 ]]; do
5050
exit 1
5151
fi
5252
shift 2 ;;
53-
-*) echo "Error: unknown option '$1'"; show_usage ;;
53+
-*) echo "Error: unknown option '$1'"; show_usage 2 ;;
5454
*) LOG_FILE="$1"; shift ;;
5555
esac
5656
done
5757

5858
if [[ -z "${LOG_FILE:-}" ]]; then
5959
echo "Error: slow query log file is required."
60-
show_usage
60+
show_usage 2
6161
fi
6262

6363
if [[ ! -f "$LOG_FILE" ]]; then
@@ -76,7 +76,8 @@ echo " Min time: ${MIN_TIME}s"
7676
echo " Top N: $TOP_N"
7777
echo "-----------------------------------------------------------"
7878

79-
total=$(grep -c "^# Query_time:" "$LOG_FILE" 2>/dev/null || echo 0)
79+
total=0
80+
total=$(grep -c "^# Query_time:" "$LOG_FILE" 2>/dev/null) || total=0
8081
echo "Total slow queries in log: $total"
8182
echo ""
8283

β€Žscripts/network-latency-report.shβ€Ž

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@ Examples:
2626
$(basename "$0") 8.8.8.8 1.1.1.1
2727
$(basename "$0") --count 10 --threshold 100 google.com
2828
EOF
29-
exit 0
29+
exit "${1:-0}"
3030
}
3131

3232
if [[ $# -eq 0 ]]; then
3333
echo "Error: at least one host is required."
34-
show_usage
34+
show_usage 2
3535
fi
3636

3737
HOSTS=()
@@ -52,14 +52,14 @@ while [[ $# -gt 0 ]]; do
5252
exit 1
5353
fi
5454
shift 2 ;;
55-
-*) echo "Error: unknown option '$1'"; show_usage ;;
55+
-*) echo "Error: unknown option '$1'"; show_usage 2 ;;
5656
*) HOSTS+=("$1"); shift ;;
5757
esac
5858
done
5959

6060
if [[ ${#HOSTS[@]} -eq 0 ]]; then
6161
echo "Error: at least one host is required."
62-
show_usage
62+
show_usage 2
6363
fi
6464

6565
if ! command -v ping >/dev/null 2>&1; then

β€Žscripts/swap-usage-monitor.shβ€Ž

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,7 @@ get_swap_info() {
5151
fi
5252
else
5353
# macOS fallback via vm_stat
54-
local pagesize total_pages free_pages
55-
pagesize=$(getconf PAGESIZE 2>/dev/null || echo 4096)
54+
local total_pages free_pages
5655
total_pages=$(sysctl -n vm.swapusage 2>/dev/null | awk '{print $3}' | tr -d 'M' || echo 0)
5756
free_pages=$(sysctl -n vm.swapusage 2>/dev/null | awk '{print $7}' | tr -d 'M' || echo 0)
5857
echo "$total_pages $free_pages 0"

β€Žtests/kernel-parameter-audit.batsβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,6 @@ setup() {
4747
}
4848

4949
@test "script outputs OK or FAIL or SKIP per parameter" {
50-
run grep -qE "^ echo .OK:" "$SCRIPT_PATH"
50+
run grep -qE "echo .OK:" "$SCRIPT_PATH"
5151
assert_success
5252
}

β€Žtests/mysql-slow-query-report.batsβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ teardown() {
5252
@test "script reports query time from sample log" {
5353
local sample_log="${TEST_DIR}/slow.log"
5454
cat > "$sample_log" << 'EOF'
55-
# Query_time: 3.456789 Lock_time: 0.000123 Rows_sent: 1 Rows_examined: 10000
55+
# Query_time: 3.456000 Lock_time: 0.000123 Rows_sent: 1 Rows_examined: 10000
5656
SELECT * FROM users WHERE email = 'test@example.com';
5757
EOF
5858
run bash "$SCRIPT_PATH" "$sample_log"

0 commit comments

Comments
Β (0)