Bash terminal timer script with countdown in seconds and final sound alert using beep or alternative audio commands for Linux/Unix systems.
#!/bin/bash
# Terminal timer with countdown and final sound
# Usage: ./timer.sh <seconds>
# Function for the countdown
countdown() {
local seconds=$1
tput civis # Hide the cursor
tput sc # Save the cursor position
while [ $seconds -ge 0 ]; do
tput rc # Restore the cursor position
tput el # Clear the line
echo -n "Time remaining: $seconds seconds"
sleep 1
((seconds--))
done
tput cnorm # Restore the cursor
echo
}
# Parameter check
if [ -z "$1" ]; then
echo "Usage: $0 <seconds>"
exit 1
fi
# Verify that the parameter is a positive integer
if ! [[ "$1" =~ ^[0-9]+$ ]]; then
echo "Error: the parameter must be a positive integer."
exit 1
fi
# Execute countdown
countdown $1
# Play final sound
# Try to use beep, if not available try paplay or aplay
if command -v beep >/dev/null 2>&1; then
beep -f 1000 -l 500 -r 3
elif command -v paplay >/dev/null 2>&1; then
paplay /usr/share/sounds/freedesktop/stereo/complete.oga
elif command -v aplay >/dev/null 2>&1; then
aplay /usr/share/sounds/alsa/Front_Center.wav
else
# If no audio command available, use ASCII beep
echo -e "\a"
fiLinux/Unix terminal with bash installed
'sleep' command for time pause
'tput' command to manipulate the cursor and clear the line
'beep' or 'paplay' or 'aplay' command to play a sound (optional, depends on the system)
Configuration: ensure the 'beep' command is installed (sudo apt install beep) or use 'paplay /usr/share/sounds/freedesktop/stereo/complete.oga' on systems with PulseAudio
Arduino: not required in this project