#!/bin/bash fail_fast=false times=0 interrupted=false # Function to handle interrupt handle_interrupt() { echo -e "\nInterrupt received. Stopping execution." interrupted=true } # Set up the interrupt trap trap handle_interrupt SIGINT # Parse command line arguments while [[ $# -gt 0 ]]; do case $1 in --fail-fast) fail_fast=true shift ;; *) if [[ $times -eq 0 ]]; then times=$1 shift else break fi ;; esac done echo "Running command $@ $times times. (fail_fast=$fail_fast)" successes=0 failures=0 for ((n=0; n < $times; n++)); do if $interrupted; then break fi "$@" if [[ $? -eq 0 ]]; then ((successes++)) else ((failures++)) if [[ $fail_fast == true ]]; then echo -e "\nFailed on iteration $((n+1)). Stopping execution." break fi fi echo -e "\nSuccesses: $successes" echo -e "Failures: $failures\n" done if $interrupted; then echo "Execution interrupted." echo "Final results:" echo -e "Successes: $successes" echo -e "Failures: $failures\n" echo "Exiting with status 130 (interrupt)." exit 130 # Standard exit code for interrupt elif [[ $failures == 0 ]]; then echo "Exiting with status 0 (success)." exit 0 else echo "Exiting with status 1 (failure)." exit 1 fi