Last active
December 9, 2024 07:21
-
-
Save AfroThundr3007730/48156633e3d2b8ac6594281057c6e125 to your computer and use it in GitHub Desktop.
Bash function to record duration, with concurrent timer support
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Record time duration, concurrent timers | |
timer() { | |
[[ $1 =~ ^[0-9]+$ ]] && { | |
[[ $n -gt $1 ]] || n=$1; local i=$1; shift; } | |
[[ $1 == -n ]] && { n=$(( n + 1 )); shift; } | |
[[ $1 == -p ]] && { n=$(( n - 1 )); shift; } | |
[[ $1 == -c ]] && { local i=$n; shift; } | |
[[ -n $1 ]] || say -n err 'No timer action specified.' | |
[[ $1 == start ]] && tstart[$i]=$SECONDS | |
[[ $1 == stop ]] && { | |
[[ -n ${tstart[$i]} ]] || say -n err 'Timer %s not started.' "$i" | |
tstop[$i]=$SECONDS; duration[$i]=$(( tstop[i] - tstart[i] )); } | |
[[ $1 == show ]] && { | |
[[ -n ${tstop[$i]} ]] || duration[$i]=$(( SECONDS - tstart[i] )) | |
echo ${duration[$i]}; } | |
return 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This uses my
say
function from before, but those lines could be changed to useprintf
instead.