63 lines
1.8 KiB
Markdown
63 lines
1.8 KiB
Markdown
+++
|
|
title = "Tea Timer in Bash"
|
|
date = 2025-10-29
|
|
|
|
[taxonomies]
|
|
categories = ["Linux"]
|
|
|
|
[extra]
|
|
author = "Emil Miler"
|
|
+++
|
|
|
|
I grew tired of unlocking my phone, opening and starting a timer when brewing tea, whereas running a simple shell script is much more convenient, at least for me. Writing this script also helped me to refresh my dulled shell scripting skills.
|
|
|
|
<!-- more -->
|
|
|
|
Here is the full script which I named `tea` in my file system:
|
|
|
|
```sh
|
|
#!/bin/bash
|
|
|
|
NOTIFICATION_SOUND=~/.scripts/notification.mp3
|
|
DELAY="${1:-180}"
|
|
DELAY_REMAINING=$DELAY
|
|
|
|
echo "Timer set to $DELAY seconds"
|
|
echo "Remaining: $DELAY_REMAINING"
|
|
|
|
while [ "$DELAY_REMAINING" -ne 0 ]; do
|
|
sleep 1
|
|
((DELAY_REMAINING--))
|
|
echo -e "\e[1A\e[KRemaining: $DELAY_REMAINING"
|
|
done
|
|
|
|
notify-send 'Tea is Ready' "Timer was set to $DELAY seconds"
|
|
pw-play "$NOTIFICATION_SOUND" &
|
|
```
|
|
|
|
So how does it work? First, we set some variables:
|
|
|
|
```sh
|
|
NOTIFICATION_SOUND=~/.scripts/notification.mp3
|
|
DELAY="${1:-180}"
|
|
```
|
|
|
|
`NOTIFICATION_SOUND` contains the path for a sound which gets played after the timer runs out. `DELAY` sets the delay to `$1`, or to default of 180 seconds if the `$1` parameter is missing.
|
|
|
|
Next interesting part is the timing itself. It runs in a loop until `DELAY_REMAINING` reaches zero. The loop also contains a peculiar `echo`:
|
|
|
|
```sh
|
|
echo -e "\e[1A\e[KRemaining: $DELAY_REMAINING"
|
|
```
|
|
|
|
This basically replaces the last line in _stdout_, where `\e[1A` moves the cursor to the previous line and `\e[K` erases the line, therefore letting us overwrite the line with new content.
|
|
|
|
When the timer runs out, system notification is sent with `notify-send` and the predefined sound is played trough _pipewire_ using `pw-play`:
|
|
|
|
```sh
|
|
notify-send 'Tea is Ready' "Timer was set to $DELAY seconds"
|
|
pw-play "$NOTIFICATION_SOUND" &
|
|
```
|
|
|
|
The usage is simple: running `tea` with an optional parameter, such as `tea 120`.
|