Skip to content

Instantly share code, notes, and snippets.

@danmindru
Last active January 31, 2026 07:25
Show Gist options
  • Select an option

  • Save danmindru/24aea09065c89a69affe8308e7d2f36f to your computer and use it in GitHub Desktop.

Select an option

Save danmindru/24aea09065c89a69affe8308e7d2f36f to your computer and use it in GitHub Desktop.
Ralph Wiggum loop script

How to run:

  • install docker
  • run docker sandbox run --credentials host claude and log in to Claude

Then, make script executable:

chmod +x ralph.sh

Run it:

./ralph.sh 10 

This will run for 10 iterations.

#!/bin/bash
set -e
if [ -z "$1" ]; then
echo "Usage: $0 <iterations>"
exit 1
fi
# jq filter to extract streaming text from assistant messages
stream_text='select(.type == "assistant").message.content[]? | select(.type == "text").text // empty | gsub("\n"; "\r\n") | . + "\r\n\n"'
# jq filter to extract final result
final_result='select(.type == "result").result // empty'
for ((i=1; i<=$1; i++)); do
tmpfile=$(mktemp)
trap "rm -f $tmpfile" EXIT
docker sandbox run --credentials host claude \
--verbose \
--print \
--output-format stream-json \
"<your prompt here>" \
| grep --line-buffered '^{' \
| tee "$tmpfile" \
| jq --unbuffered -rj "$stream_text"
result=$(jq -r "$final_result" "$tmpfile")
if [[ "$result" == *"<promise>COMPLETE</promise>"* ]]; then
echo "Ralph complete after $i iterations."
exit 0
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment