Skip to content

Instantly share code, notes, and snippets.

@IC-Tech
Created April 17, 2022 18:20
Show Gist options
  • Select an option

  • Save IC-Tech/97471606440284912dd007d9c61334b0 to your computer and use it in GitHub Desktop.

Select an option

Save IC-Tech/97471606440284912dd007d9c61334b0 to your computer and use it in GitHub Desktop.
simple sleep timer with bash
#!/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
@IC-Tech
Copy link
Author

IC-Tech commented Apr 17, 2022

example:

user@pc:/$ timer
user@pc:/$ timer 2
00:00:02
user@pc:/$ timer 3s
00:00:03
user@pc:/$ timer 2m 3s
00:02:03
user@pc:/$ timer 2m 3s 1m
00:03:03
user@pc:/$ timer 22s 3s 1m 1m 5h 0h1m2s
05:03:27

usage:

user@pc:/$ timer 1m 30s && shutdown --reboot 0
00:01:30

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