Skip to content

Instantly share code, notes, and snippets.

@erichummel
Created February 7, 2025 15:28
Show Gist options
  • Save erichummel/9b831324e8254e4b4a0366f8ebb09b2c to your computer and use it in GitHub Desktop.
Save erichummel/9b831324e8254e4b4a0366f8ebb09b2c to your computer and use it in GitHub Desktop.
a bash function for repeating a command with emoji status updates and a report of all failures at the end
# usage: repeat_with_status <iterations> <command>
# note that due to the nature of `script`, locally defined aliases will not work in the <command>
function repeat_with_status {
local count=$1
shift
local success_count=0
local error_count=0
local output
declare -a failures=()
# Force color output
export FORCE_COLOR=true
export CLICOLOR_FORCE=1
for ((i=0; i<count; i++)); do
if ((i % 10 == 0)); then
if ((i != 0)); then
printf "\n"
fi
printf "run $((i+1))..."
if ((i == 0)); then
printf " "
fi
fi
output=$(script -q /dev/null "$@" 2>&1)
if [ $? -eq 0 ]; then
((success_count++))
printf "✅"
else
((error_count++))
printf "❌"
failures+=("Failed on iteration $((i+1)):")
failures+=("$output")
failures+=("---")
fi
done
printf "\n"
echo "good runs: $success_count"
echo "bad runs: $error_count"
# Print failure messages if there were any errors
if ((error_count > 0)); then
echo -e "\nFailure details:"
printf '%s\n' "${failures[@]}"
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment