Created
April 17, 2022 18:20
-
-
Save IC-Tech/97471606440284912dd007d9c61334b0 to your computer and use it in GitHub Desktop.
simple sleep timer with bash
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 | |
| sec=0 | |
| function _t1 { | |
| b="$2" | |
| echo "${@:3}" | grep -oE "[0-9]+$1" | grep -oE '[0-9]+' | while read a ; do | |
| printf "$((a * b)) + " | |
| done | |
| } | |
| if [[ "$(echo "$1" | grep -oE '[HhMmSs]')" ]] ; then | |
| v=`_t1 '[sS]' 1 "$@"` | |
| sec="$(bash -c 'echo "$(('"$v"'0))"')" | |
| v=`_t1 '[mM]' 60 "$@"` | |
| v="$(bash -c 'echo "$(('"$v"'0))"')" | |
| sec="$((sec + v))" | |
| v=`_t1 '[hH]' 3600 "$@"` | |
| v="$(bash -c 'echo "$(('"$v"'0))"')" | |
| sec="$((sec + v))" | |
| else | |
| if [[ "$(echo "$1" | grep -oE '[^0123456789]')" ]]; then | |
| echo 'invalid input' 1>&2 | |
| exit 1 | |
| fi | |
| if [[ "$1" ]]; then | |
| sec="$1" | |
| fi | |
| fi | |
| for ((a = $sec; a > 0; a--)); do | |
| if [[ $a == $sec ]] ; then | |
| echo '-' | |
| fi | |
| printf \\e[K\\e[A | |
| date -u --date="@$a" '+%T' | |
| sleep 1 | |
| done |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
example:
usage:
user@pc:/$ timer 1m 30s && shutdown --reboot 0 00:01:30