Skip to content

fix: setup.sh cross-shell grep -c arithmetic error#915

Merged
marcusquinn merged 1 commit intomainfrom
hotfix/setup-sh-grep-fix
Feb 10, 2026
Merged

fix: setup.sh cross-shell grep -c arithmetic error#915
marcusquinn merged 1 commit intomainfrom
hotfix/setup-sh-grep-fix

Conversation

@marcusquinn
Copy link
Owner

@marcusquinn marcusquinn commented Feb 10, 2026

Summary

  • Fixes arithmetic error in setup_shell_compatibility() at line ~1553 of setup.sh
  • grep -cE outputs 0 on no match (exit code 1), then || echo "0" appends a second 0 line, producing "0\n0" which breaks $(( )) arithmetic
  • Fix: use || true to suppress exit code without appending output, and ${n:-0} as safety fallback

Root Cause

# Before (broken): n becomes "0\n0" (two lines)
n=$(grep -cE '^\s*alias\s+' "$file" 2>/dev/null || echo "0")

# After (fixed): n becomes "0" (single line)
n=$(grep -cE '^\s*alias\s+' "$file" 2>/dev/null) || true
total=$((total + ${n:-0}))

Impact

This bug was introduced by PR #900 and blocks all autonomous deploys via run_deploy_for_task() in the supervisor. The error is non-fatal (setup.sh continues) but produces confusing output and may cause incorrect cross-shell compatibility detection.

Testing

  • Verified fix produces clean single-line values for all bash config files
  • Full setup.sh --non-interactive completes with zero errors
  • ShellCheck clean (no new violations)

Summary by CodeRabbit

  • Bug Fixes
    • Improved setup script stability by enhancing error handling in shell compatibility checks, ensuring accurate configuration counts and preventing initialization errors when certain conditions are not found.

grep -cE outputs '0' on no match (exit 1), then '|| echo 0' appends
a second '0' line, producing '0\n0' which breaks $(( )) arithmetic.

Fix: use '|| true' to suppress exit code without appending output,
and ${n:-0} as safety fallback for empty values.
@gemini-code-assist
Copy link

Warning

You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again!

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Feb 10, 2026

Walkthrough

The setup.sh script's shell compatibility detection function is enhanced with more robust grep pattern matching. Parameter expansion defaults are introduced to handle zero-match scenarios, ensuring accurate counting of exports, aliases, and PATH modifications without unintended failures or implicit echoing.

Changes

Cohort / File(s) Summary
Shell Compatibility Grep Enhancement
setup.sh
Improved error handling in setup_shell_compatibility function by replacing grep invocations with parameter expansion defaults (${n:-0}) to gracefully handle zero-match cases and prevent arithmetic errors during shell configuration counting.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

Poem

Grep dances gently through the night,
Zero matches now shine bright,
Parameter expansion saves the day,
Shell scripts guard each every way! 🛡️
Robust and true, without a care,
DevOps excellence fills the air ✨

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately and concisely describes the main fix: resolving an arithmetic error in setup.sh related to grep -c cross-shell compatibility.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch hotfix/setup-sh-grep-fix

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@github-actions
Copy link

🔍 Code Quality Report

�[0;35m[MONITOR]�[0m Code Review Monitoring Report

�[0;34m[INFO]�[0m Latest Quality Status:
SonarCloud: 0 bugs, 0 vulnerabilities, 33 code smells

�[0;34m[INFO]�[0m Recent monitoring activity:
Tue Feb 10 04:24:57 UTC 2026: Code review monitoring started
Tue Feb 10 04:24:58 UTC 2026: SonarCloud - Bugs: 0, Vulnerabilities: 0, Code Smells: 33

📈 Current Quality Metrics

  • BUGS: 0
  • CODE SMELLS: 33
  • VULNERABILITIES: 0

Generated on: Tue Feb 10 04:25:00 UTC 2026


Generated by AI DevOps Framework Code Review Monitoring

@sonarqubecloud
Copy link

@marcusquinn marcusquinn merged commit c56f34b into main Feb 10, 2026
9 of 10 checks passed
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (2)
setup.sh (2)

2688-2689: ⚠️ Potential issue | 🟡 Minor

Same grep -c ... || echo "0" bug exists here (unfixed).

Line 2688 uses the identical pre-fix pattern that was just corrected in setup_shell_compatibility(). Under set -euo pipefail, when grep -c finds no matches it prints "0" (exit 1), then || echo "0" appends a second "0", producing "0\n0". The subsequent arithmetic comparison on line 2689 ([[ "$disabled_count" -gt 0 ]]) would fail with a non-numeric value.

Proposed fix
-        disabled_count=$(grep -c "disableDynamicTitle: true" "$tabby_config" 2>/dev/null || echo "0")
-        if [[ "$disabled_count" -gt 0 ]]; then
+        disabled_count=$(grep -c "disableDynamicTitle: true" "$tabby_config" 2>/dev/null) || true
+        if [[ "${disabled_count:-0}" -gt 0 ]]; then

4849-4849: ⚠️ Potential issue | 🟡 Minor

Same grep -c ... || echo "0" bug exists here too (unfixed).

Line 4849 has the identical pattern. While key_count is only used in an informational print_info message (so the impact is cosmetic — the message would show "0\n0" instead of "0"), it's worth applying the same fix for consistency.

Proposed fix
-        key_count=$(grep -c "^export " "$HOME/.config/aidevops/credentials.sh" 2>/dev/null || echo "0")
+        key_count=$(grep -c "^export " "$HOME/.config/aidevops/credentials.sh" 2>/dev/null) || true

@marcusquinn marcusquinn deleted the hotfix/setup-sh-grep-fix branch February 21, 2026 01:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant