Skip to content

Instantly share code, notes, and snippets.

@mathew-fleisch
Created October 18, 2023 18:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mathew-fleisch/cc0f66c833ecf15f3192ef0f416b62c5 to your computer and use it in GitHub Desktop.
Save mathew-fleisch/cc0f66c833ecf15f3192ef0f416b62c5 to your computer and use it in GitHub Desktop.
Bash timer
#!/bin/bash
convertsecs() {
((h=${1}/3600))
((m=(${1}%3600)/60))
((s=${1}%60))
printf "%02d:%02d:%02d\n" $h $m $s
}
started=$(date +%s)
current=$started
# timeout: 10 seconds
timeout=$((started+10))
while [ $current -lt $timeout ]; do
current=$(date +%s)
diff=$((timeout-current))
echo "timeout[$(convertsecs $diff)]"
sleep 1
done
echo "Timeout!"
@mathew-fleisch
Copy link
Author

mathew-fleisch commented Oct 18, 2023

$ ./timeout.sh 
timeout[00:00:10]
timeout[00:00:09]
timeout[00:00:08]
timeout[00:00:07]
timeout[00:00:06]
timeout[00:00:05]
timeout[00:00:04]
timeout[00:00:03]
timeout[00:00:02]
timeout[00:00:01]
timeout[00:00:00]
Timeout!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment