Terminal timer with countdown and final sound

Bash terminal timer script with countdown in seconds and final sound alert using beep or alternative audio commands for Linux/Unix systems.

Brief Introduction

Terminal timer with countdown and final sound (bash)
DESCRIPTION:
Bash script that implements a terminal timer with a countdown in seconds and plays a final sound at the end of the set time.

ADDITIONAL NOTES:
The script requires bash and standard system commands. For playing the final sound, if 'beep' is not available, the script can be modified to use other audio commands. The script accepts the timer duration in seconds as a parameter.

EXPLANATIONS:
- The countdown() function manages the countdown showing the remaining seconds.
- The 'tput sc' command saves the cursor position, 'tput rc' restores it, and 'tput el' clears the line to update the timer cleanly.
- At the end of the countdown, a sound is played with 'beep' or an alternative command.
- If no parameter is passed, the script shows a usage message and exits.

Example Code: bash

#!/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"
fi
×
×
×
Do you want to regenerate the answer?
×
Export chat
Choose export format
Generazione PDF in corso…
Allegati
×
You are about to close the chat and switch to the image generator. If you are not logged in, you will lose our chat. Do you confirm?
You are viewing a shared chat in temporary mode. It will not be saved.
×
Saved prompts
×
Private note
×
Label
×
Search all chats
×
Your insights
Analyzing…
×
AI Preferences
Assistant name 30
Answer length
AI model
Structured answers
Include examples
Use emojis
Guide me, don't solve Hints and questions instead of a ready answer
More settings →
×
Share this chat
Anyone opening this link can view the chat or add it to their profile as their own chat.
Note: the chat attachments will be shared too. Anyone who adds it will receive a copy of the files in their own folder.
Shared chat
Someone shared a chat with you. Do you want to just view it or add it to your chats?
Note: the chat attachments will be shared too. Anyone who adds it will receive a copy of the files in their own folder.
×

Saved messages

Loading...

×

Chat History

example-programs · CHAT HISTORY

Loading...

AI Preferences

×
  • BasicQuick and essential answers for study
  • MediumHigher quality for study and programming
  • AdvancedComplex reasoning and detailed analysis
Explain Steps

What do you need?

Linux/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

Similar Topics

Basic Disk Usage Monitor with On-Screen Alert
Bash script to monitor disk usage with on-screen alerts using notify-send, configurable threshold and target device for Linux systems.
Text menu to launch frequent commands
Simple bash script providing a text menu to launch frequent commands, automating tasks without typing full commands each time.
Script to download files with curl and check the result
Bash script to download files using curl, verify success, and handle errors with clear messages for reliable command-line file transfers.
Batch Image Converter with ImageMagick from bash
Batch Image Converter bash script using ImageMagick to resize and convert images in bulk, supporting multiple formats and maintaining proportions.
Bash Script to Search Text in Multiple Files and Show Context
Bash script to search text in multiple files with context lines, enabling quick and recursive searches in specified directories using grep.
Script to compare two folders and show file differences
Bash script to compare two folders, highlighting unique and modified files using standard Unix commands like diff, find, sort, and comm.
Random Command Line Password Generator
Simple bash script to generate secure random passwords with customizable length using alphanumeric characters and symbols on Linux/Unix systems.
Bash Script to Merge Multiple Text Files into a Single File with Header
Bash script to merge multiple text files into one with a custom header showing date and number of files merged, handling missing files gracefully.