diff options
| author | Hsieh Chin Fan <pham@topo.tw> | 2023-02-22 15:03:30 +0800 |
|---|---|---|
| committer | Hsieh Chin Fan <pham@topo.tw> | 2023-02-22 15:03:30 +0800 |
| commit | d0947320bc1b3a1223097749affe798ba225f979 (patch) | |
| tree | 7dad38f06475b1757b675f80762d2452e8809cb0 /bin/unix | |
| parent | c9fc50031b3f304c58524a3ee524598eed648560 (diff) | |
Update
Diffstat (limited to 'bin/unix')
| -rwxr-xr-x | bin/unix/countdown.sh | 19 | ||||
| -rwxr-xr-x | bin/unix/stopwatch.sh | 20 |
2 files changed, 39 insertions, 0 deletions
diff --git a/bin/unix/countdown.sh b/bin/unix/countdown.sh new file mode 100755 index 0000000..683949e --- /dev/null +++ b/bin/unix/countdown.sh | |||
| @@ -0,0 +1,19 @@ | |||
| 1 | #! /bin/bash | ||
| 2 | # Ref: https://superuser.com/questions/611538/611582#611582 | ||
| 3 | |||
| 4 | tput civis | ||
| 5 | read -p '? ' input | ||
| 6 | |||
| 7 | [[ "$input" =~ s && ! "$input" =~ sec ]] && input="$(sed s/s/sec/ <<<"$input")" | ||
| 8 | [[ "$input" =~ m && ! "$input" =~ min ]] && input="$(sed s/m/min/ <<<"$input")" | ||
| 9 | [[ "$input" =~ h && ! "$input" =~ hour ]] && input="$(sed s/h/hour/ <<<"$input")" | ||
| 10 | |||
| 11 | |||
| 12 | start=$(date +%s -d "$input") | ||
| 13 | while [ "$start" -ge $(date +%s) ]; do | ||
| 14 | ## Is this more than 24h away? | ||
| 15 | time="$(( $start - `date +%s` ))" | ||
| 16 | printf '%s\r' "$(date -u -d "@$time" +%H:%M:%S)" | ||
| 17 | sleep 0.3 | ||
| 18 | done | ||
| 19 | printf "\e[1;31m$(date -u -d "@$time" +%H:%M:%S)" | ||
diff --git a/bin/unix/stopwatch.sh b/bin/unix/stopwatch.sh new file mode 100755 index 0000000..70f6b0c --- /dev/null +++ b/bin/unix/stopwatch.sh | |||
| @@ -0,0 +1,20 @@ | |||
| 1 | #! /bin/bash | ||
| 2 | # Ref: https://superuser.com/questions/611538/611582#611582 | ||
| 3 | |||
| 4 | tput civis | ||
| 5 | read -p '? ' input | ||
| 6 | |||
| 7 | [[ "$input" =~ s && ! "$input" =~ sec ]] && input="$(sed s/s/sec/ <<<"$input")" | ||
| 8 | [[ "$input" =~ m && ! "$input" =~ min ]] && input="$(sed s/m/min/ <<<"$input")" | ||
| 9 | [[ "$input" =~ h && ! "$input" =~ hour ]] && input="$(sed s/h/hour/ <<<"$input")" | ||
| 10 | |||
| 11 | time=0 | ||
| 12 | start=$(date +%s) | ||
| 13 | given=$(date +%s -d "$input") | ||
| 14 | stop=$(( $given - $start )) | ||
| 15 | while [ $time -ne $stop ]; do | ||
| 16 | time="$(( $(date +%s) - $start ))" | ||
| 17 | printf '%s\r' "$(date -u -d "@$time" +%H:%M:%S)" | ||
| 18 | sleep 0.3 | ||
| 19 | done | ||
| 20 | printf "\e[1;31m$(date -u -d "@$time" +%H:%M:%S)" | ||