#!/bin/bash
# Create project directory structure
mkdir -p clock-timer/.vscode
# Create the main timer script
cat > clock-timer/timer.sh << 'EOF'
#!/bin/bash
# Simple Clock Timer Utility
# Usage: ./timer.sh [options]
# Function to display usage information
show_help() {
echo "Clock Timer - A simple bash timer utility"
echo ""
echo "Usage: $0 [OPTION]"
echo "Options:"
echo " -c, --clock Display current time continuously"
echo " -t, --timer TIME Start countdown timer for TIME seconds"
echo " -s, --stopwatch Start a stopwatch"
echo " -h, --help Display this help message"
echo ""
echo "Examples:"
echo " $0 --clock # Run as a clock"
echo " $0 --timer 300 # 5-minute countdown timer"
echo " $0 --stopwatch # Run as a stopwatch"
}
# Function to display current time
clock_mode() {
echo "Clock Mode (Press Ctrl+C to exit)"
while true; do
clear
date +"%H:%M:%S"
sleep 1
done
}
# Function for countdown timer
countdown_timer() {
local seconds=$1
if ! [[ "$seconds" =~ ^[0-9]+$ ]]; then
echo "Error: Timer value must be a positive number"
exit 1
fi
echo "Countdown Timer: $seconds seconds (Press Ctrl+C to exit)"
while [ $seconds -gt 0 ]; do
clear
minutes=$((seconds / 60))
remainder=$((seconds % 60))
printf "%02d:%02d\n" $minutes $remainder
sleep 1
((seconds--))
done
clear
echo "Time's up!"
# Make a bell sound if terminal supports it
echo -e "\a"
}
# Function for stopwatch
stopwatch() {
local seconds=0
echo "Stopwatch (Press Ctrl+C to exit)"
while true; do
clear
hours=$((seconds / 3600))
minutes=$(((seconds % 3600) / 60))
secs=$((seconds % 60))
printf "%02d:%02d:%02d\n" $hours $minutes $secs
sleep 1
((seconds++))
done
}
# Main script logic
case "$1" in
-c|--clock)
clock_mode
;;
-t|--timer)
if [ -z "$2" ]; then
echo "Error: Timer duration not specified"
show_help
exit 1
fi
countdown_timer "$2"
;;
-s|--stopwatch)
stopwatch
;;
-h|--help|*)
show_help
;;
esac
exit 0
EOF
# Make the script executable
chmod +x clock-timer/timer.sh
# Create README.md
cat > clock-timer/README.md << 'EOF'
# Bash Clock Timer
A simple command-line clock and timer utility written in Bash.
## Features
- **Clock Mode**: Displays the current time, updating every second
- **Countdown Timer**: Counts down from a specified duration
- **Stopwatch**: Counts up from zero
## Usage
```bash
# Show current time
./timer.sh --clock
# Start a 5-minute countdown timer
./timer.sh --timer 300
# Start a stopwatch
./timer.sh --stopwatch
# Show help
./timer.sh --help