Skip to content

Fix grep -c exit code causing arithmetic syntax error#161

Merged
frankbria merged 1 commit into
frankbria:mainfrom
labolado:fix/grep-count-exit-code-bug
Feb 4, 2026
Merged

Fix grep -c exit code causing arithmetic syntax error#161
frankbria merged 1 commit into
frankbria:mainfrom
labolado:fix/grep-count-exit-code-bug

Conversation

@labolado
Copy link
Copy Markdown
Contributor

@labolado labolado commented Feb 3, 2026

Summary

  • grep -c outputs "0" to stdout when finding 0 matches, but exits with code 1
  • || echo "0" fallback triggers on exit code 1, appending another "0" to the output
  • Variable ends up as "0\n0" instead of "0", causing bash arithmetic error: line 500: 0 0: syntax error in expression
  • Affects 3 locations in ralph_loop.sh (lines 498, 499, 605)

Fix

Replace || echo "0" with || true so grep's stdout output is preserved. Add [[ -z "$var" ]] && var=0 fallback for actual errors (e.g. missing file).

Test plan

  • Run Ralph with a fix_plan.md that has only unchecked - [ ] items (0 completed) — previously crashed here
  • Run Ralph with a fix_plan.md that has some - [x] items
  • Run Ralph without a fix_plan.md present

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Improved error handling resilience when processing task data. The system now gracefully handles missing or inaccessible files without emitting errors, while maintaining accurate task counts.

When `grep -c` finds 0 matches, it outputs "0" to stdout but exits
with code 1 (no matches found). The `|| echo "0"` fallback then
triggers, appending another "0" to the command substitution output.
This results in the variable containing "0\n0" instead of just "0",
causing a bash arithmetic syntax error on the $((...)) expression:
  line 500: 0 0: syntax error in expression (error token is "0")

Fix: Replace `|| echo "0"` with `|| true` so the grep output is
preserved as-is, with a fallback to set 0 if the variable is empty
(which handles actual grep errors like missing files).

Affects 3 locations in ralph_loop.sh (lines 498, 499, 605).

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Feb 3, 2026

Walkthrough

Modified error-handling patterns in two functions of ralph_loop.sh. Replaced inline echo "0" defaults on grep failure with explicit guard patterns using || true, then normalize empty results to 0, improving resilience when target files are missing.

Changes

Cohort / File(s) Summary
Error handling refactoring
ralph_loop.sh
Updated should_exit_gracefully and build_loop_context functions to use || true guards with explicit zero normalization instead of inline echo "0" defaults on grep failure, improving robustness when files are missing.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Possibly related PRs

Poem

🐰 A rabbit hops through bash with care,
Where grep once failed without a prayer,
Now guards stand firm with || true dance,
And zeros bloom when files don't prance! 🌰

🚥 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 describes the primary fix: addressing a grep exit code issue causing arithmetic syntax errors in bash.
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

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.

@macroscopeapp
Copy link
Copy Markdown
Contributor

macroscopeapp Bot commented Feb 3, 2026

Fix arithmetic syntax errors by making ralph_loop.sh::should_exit_gracefully and ralph_loop.sh::build_loop_context use numeric grep counts for fix_plan.md task checkboxes

Replace grep -cE ... || echo "0" with grep -cE ... || true and explicit empty checks to set counts to 0, ensuring numeric values for checkbox counts in fix_plan.md within ralph_loop.sh.

📍Where to Start

Start with should_exit_gracefully in ralph_loop.sh, then review build_loop_context.


Macroscope summarized 241b585.

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.

2 participants