Fix grep -c exit code causing arithmetic syntax error

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>
This commit is contained in:
LaboLado 2026-02-03 12:41:24 +08:00
parent aa753c9158
commit 241b5854fc

View file

@ -495,8 +495,10 @@ should_exit_gracefully() {
# Fix #144: Only match valid markdown checkboxes, not date entries like [2026-01-29]
# Valid patterns: "- [ ]" (uncompleted) and "- [x]" or "- [X]" (completed)
if [[ -f "$RALPH_DIR/fix_plan.md" ]]; then
local uncompleted_items=$(grep -cE "^[[:space:]]*- \[ \]" "$RALPH_DIR/fix_plan.md" 2>/dev/null || echo "0")
local completed_items=$(grep -cE "^[[:space:]]*- \[[xX]\]" "$RALPH_DIR/fix_plan.md" 2>/dev/null || echo "0")
local uncompleted_items=$(grep -cE "^[[:space:]]*- \[ \]" "$RALPH_DIR/fix_plan.md" 2>/dev/null || true)
[[ -z "$uncompleted_items" ]] && uncompleted_items=0
local completed_items=$(grep -cE "^[[:space:]]*- \[[xX]\]" "$RALPH_DIR/fix_plan.md" 2>/dev/null || true)
[[ -z "$completed_items" ]] && completed_items=0
local total_items=$((uncompleted_items + completed_items))
if [[ $total_items -gt 0 ]] && [[ $completed_items -eq $total_items ]]; then
@ -602,7 +604,8 @@ build_loop_context() {
# Extract incomplete tasks from fix_plan.md
# Bug #3 Fix: Support indented markdown checkboxes with [[:space:]]* pattern
if [[ -f "$RALPH_DIR/fix_plan.md" ]]; then
local incomplete_tasks=$(grep -cE "^[[:space:]]*- \[ \]" "$RALPH_DIR/fix_plan.md" 2>/dev/null || echo "0")
local incomplete_tasks=$(grep -cE "^[[:space:]]*- \[ \]" "$RALPH_DIR/fix_plan.md" 2>/dev/null || true)
[[ -z "$incomplete_tasks" ]] && incomplete_tasks=0
context+="Remaining tasks: ${incomplete_tasks}. "
fi