Skip to content

Instantly share code, notes, and snippets.

@aprell
Created September 4, 2019 13:19
Show Gist options
  • Save aprell/d790229f13287e5b156d1dbca596469c to your computer and use it in GitHub Desktop.
Save aprell/d790229f13287e5b156d1dbca596469c to your computer and use it in GitHub Desktop.
Repeat a command until it fails with a non-zero exit code
#!/usr/bin/env bash
# ShellChecked with no issues detected
#
# Example usage for debugging a sporadically failing program:
# repeat.sh -- gdb --eval-command=run --eval-command=quit --args ./a.out
while [[ $1 != "--" ]]; do
n=$1
shift
done
shift
if [[ -n $n ]]; then
i=0
ret=0
while [[ $i -lt $n && $ret -eq 0 ]]; do
eval "$@"
ret=$?
i=$((i + 1))
done
else
ret=0
while [[ $ret -eq 0 ]]; do
eval "$@"
ret=$?
done
fi
@aprell
Copy link
Author

aprell commented Sep 4, 2019

alias debug="repeat.sh -- gdb --eval-command=run --eval-command=quit --args"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment