Practical Applications of the Sleep Command
Now that you understand the basics of the sleep command and how to use it with variables, let's explore some practical applications. These examples demonstrate how the sleep command is used in real-world scenarios.
Creating a Simple Countdown Timer
Let's create a countdown timer that demonstrates a more complex use of the sleep command:
cd ~/project
touch countdown.sh
nano countdown.sh
Add the following content:
#!/bin/zsh
## Function to display a countdown
countdown() {
local seconds=$1
while [ $seconds -gt 0 ]; do
echo -ne "\rTime remaining: $seconds seconds "
sleep 1
((seconds--))
done
echo -e "\rCountdown complete! "
}
## Specify the countdown duration
echo "Starting a 5-second countdown:"
countdown 5
echo "Countdown script execution complete."
Save, exit nano, and make the script executable:
chmod +x countdown.sh
Run the script:
./countdown.sh
You should see a countdown from 5 seconds to 0, with the time updating in place:
Starting a 5-second countdown:
Time remaining: 5 seconds
Time remaining: 4 seconds
Time remaining: 3 seconds
Time remaining: 2 seconds
Time remaining: 1 seconds
Countdown complete!
Countdown script execution complete.
Simulating a Process with Progress Indicator
Let's create a script that simulates a long-running process with a simple progress indicator:
cd ~/project
touch progress.sh
nano progress.sh
Add the following content:
#!/bin/zsh
## Function to show a simple progress bar
show_progress() {
local duration=$1
local steps=10
local step_duration=$(echo "scale=2; $duration / $steps" | bc)
echo "Starting process..."
echo -n "Progress: ["
for i in {1..10}; do
sleep $step_duration
echo -n "#"
done
echo "] Done!"
}
## Run a process that takes 5 seconds with a progress indicator
show_progress 5
echo "Process completed successfully."
Save, exit nano, and make the script executable:
chmod +x progress.sh
Run the script:
./progress.sh
You should see a progress bar that fills up over 5 seconds:
Starting process...
Progress: [##########] Done!
Process completed successfully.
Controlling the Rate of Operations
In this example, we'll demonstrate how to use the sleep command to control the rate of operations, which is useful for rate-limiting API calls or processing large datasets:
cd ~/project
touch rate_limit.sh
nano rate_limit.sh
Add the following content:
#!/bin/zsh
## Define the rate limit (operations per second)
OPERATIONS_PER_SECOND=2
SLEEP_DURATION=$(echo "scale=3; 1 / $OPERATIONS_PER_SECOND" | bc)
echo "Performing operations at a rate of $OPERATIONS_PER_SECOND per second"
echo "Each operation will be followed by a $SLEEP_DURATION second delay"
## Simulate 6 operations with rate limiting
for i in {1..6}; do
echo "Performing operation $i at $(date +%H:%M:%S.%N | cut -c1-12)"
## Simulate the operation
sleep 0.1
## Rate-limiting delay between operations
if [ $i -lt 6 ]; then
sleep $SLEEP_DURATION
fi
done
echo "All operations completed"
Save, exit nano, and make the script executable:
chmod +x rate_limit.sh
Run the script:
./rate_limit.sh
You should see operations being performed at a controlled rate:
Performing operations at a rate of 2 per second
Each operation will be followed by a 0.500 second delay
Performing operation 1 at 10:30:45.12
Performing operation 2 at 10:30:45.72
Performing operation 3 at 10:30:46.32
Performing operation 4 at 10:30:46.92
Performing operation 5 at 10:30:47.52
Performing operation 6 at 10:30:48.12
All operations completed
These examples demonstrate how the sleep command can be used in more advanced scripting scenarios. The ability to control timing is a powerful tool in shell scripting that enables many practical applications.