Files
scripts/interrupt_example.sh
T
2026-09-29 15:24:09 -04:00

28 lines
800 B
Bash
Executable File

#!/usr/bin/env bash
# An example of how to ensure a user really wants to interrupt an operation
# Useful for scripts where interruption could be harmful
interrupt_count=0
on_interrupt() {
if [ $interrupt_count -lt 1 ]; then
echo "Canceling this operation can cause errors."
echo "Press Ctrl-C again if you are sure you want to cancel."
interrupt_count=$((interrupt_count + 1))
else
# Convention is to use the status code 130 for interrupted scripts.
echo "Canceling long operation"
printf "$(tput cr)$(tput el)"
exit 130
fi
}
trap on_interrupt INT
total_time=0
while [ $total_time -le 600 ]; do
printf "$(tput cr)$(tput el)Long operation: ${total_time} seconds elapsed\r"
sleep 3
total_time=$((total_time + 3))
done