|
Do you ever have the need to set a timer to let you know when something is done, or when a certain length of time has been expended? How about the need to time how long it takes for some process to finish? This script takes care of both needs. The script takes two command line parameters. The first parameter is the interval in seconds between timer display updates, and adjusts the granularity of how often you want to see the timer display updated. The second parameter is the duration you would like the timer script to run in seconds. Pressing any key that has a character associated with it will exit the timer script after the current interval expires. This ability is to enable using the script as a stop watch. You set the timer duration to run longer than you think it will take for the process you are timing, and when you press a key, the script stops, and displays the elapsed time in minutes, and seconds. To get the most accurate reading in stop watch mode, set the timer display update interval to 1 second. The default value for the timer display update interval is one second, and the default timer duration is 300 seconds. These values will be used if the script is called with no command line parameters. In the following code box is the source code for Timer.sh. Copy, and paste this code into a new file, and name it Timer.sh, or some similar name. Next use your File Manager to set the permissions on the new script to read, and execute for the users who will be using it. You can save the script to any folder within your user folders, and it will run from there.
#!/usr/bin/env bash
# Usage: /folderpath/Timer.sh n n
# Where n and n are numeric values expressed in seconds. The first n is the interval in seconds of how often you would like to see updates to the
# timer display.
#
# The second n is the duration in seconds you would like the timer to run before shutting off. This is for use as an event timer.
#
# Replace folderpath with the actual path of folder names leading to the scripts location.
#
# To stop the script at any point press a character key in Terminal. By doing this you can use the Timer script as a stop watch, just set a long
# enough duration to cover at least the event length you are attempting to time, and hit any letter, number, or punctuation key to stop timing
# when the event is over. Adjust the Interval to the granularity of accuracy you need from the stop watch mode of operation.
#
# initialize variables
# elapsed Minutes
declare -i Minutes=0
# elapsed Seconds
declare -i Seconds=0
# Elapsed keeps track of how long the timer has been running.
declare -i Elapsed=0
# Duration is how long you want the timer to run in seconds. if Duration is blank (unspecified) set default interval of 300 seconds.
Duration=${2:-300}
# Number of seconds left before timer ends.
Remainder=$Duration
# number of seconds between timer display updates. if Interval is blank (unspecified) set default interval of one second
Interval=${1:-1}
# clear the screen to begin displaying timer output.
clear
# display timer output
echo "Elapsed Time: $Minutes Minutes $Seconds Seconds"
echo "$Interval Second Interval" # echo interval setting
# read timeout for any key of one second forces us to trim 1 second from the Interval for proper execution.
(( Interval -=1 ))
# if Duration is blank (unspecified)
if [[ "$Duration" == "" ]]
then
Duration=300 # set default interval of 300 seconds.
fi
# finish first timer display screen
echo "$Duration Duration" # echo Duration setting
echo "$Remainder Seconds Remaining"
echo "To exit timer press any letter, number, or punctuation key."
echo "Timer will then exit after the next interval expires."
# While elapsed time is less than the Duration setting do loop till done.
while [[ $Elapsed -lt $Duration ]]
do
# take care of overrun condition when Duration is not evenly divisible by Interval, resulting in leftover seconds,
if [[ "$Remainder" -lt "$Interval" ]]
then
# sleep only the number of seconds left to satisfy Duration.
sleep $Remainder
# update Seconds to proper value
Seconds=$(( $Seconds +$Remainder ))
# clear the screen for last timer display
clear
# put up last timer display
echo "Elapsed Time: $Minutes Minute $Seconds Seconds"
echo "$(( $Interval +1 )) Second Interval" # echo interval setting
echo "$Duration Seconds Duration" # echo Duration setting
echo "0 Seconds Remaining"
echo "Timer has expired."
# we're done, so exit the script.
exit
fi
# sleep for the interval specified if interval is 1 or greater
if [[ $Interval -ne 0 ]]
then
sleep "$Interval"
fi
# Increment Seconds and Elapsed by the Interval setting + the 1 second read timeout value.
(( Seconds += Interval + 1 ))
(( Elapsed += Interval + 1 ))
# if Seconds equals 60, or is greater than 60 seconds, trim 60 from Seconds value, and add one to Minutes value.
[[ $Seconds -ge 60 ]] && (( Minutes += 1 )) && (( Seconds -= 60 ))
# check to see how much time is left to run
Remainder=$(( $Duration -$Elapsed ))
# clear the screen between each timer update
clear
# Update the timer display to the current values
if [ $Minutes == 1 ]
then
echo "Elapsed Time: $Minutes Minute $Seconds Seconds"
echo "$(( $Interval +1 )) Second Interval" # echo interval setting
echo "$Duration Seconds Duration" # echo Duration setting
echo "$Remainder Seconds Remaining"
if [[ $Remainder -ne 0 ]]
then
echo "To exit timer press any letter, number, or punctuation key."
echo "Timer will then exit after the next interval expires."
else
echo "Timer has expired."
fi
else
echo "Elapsed Time: $Minutes Minutes $Seconds Seconds"
echo "$(( $Interval +1 )) Second Interval" # echo interval setting
echo "$Duration Seconds Duration" # echo Duration setting
echo "$Remainder Seconds Remaining"
if [[ $Remainder -ne 0 ]]
then
echo "To exit timer press any letter, number, or punctuation key."
echo "Timer will then exit after the next interval expires."
else
echo "Timer has expired."
fi
fi
# check to see if the user wants to exit the script by polling for a key press.
read -t 1 -n 1 ExitFlag
# if a key was pressed, exit the script.
if [ "$ExitFlag" != "" ]
then
exit
fi
done
exit
To use the script issue the following command in Terminal, or whatever console program you use. Replace username, and foldername with the actual names in use on your system, and replace I with the interval in seconds you wish to use. Replace D with the duration is seconds you wish the timer script to run. I hereby release Timer.sh as open source software for Linux computers. Help me keep writing code for others to use. Please consider making a donation of whatever you can afford if you use my scripts, even something as small as one dime would help a lot. Thanks. |