Let's presume you have a long info text one of your scripts should print to the terminal, and you want it nicely word wrapped and not broken by the end of $COLUMNS. This is part of my below script "sleep-timer".
Look at the lines with either
that catches $COLUMNS or at the lines with
echo " some text
" at the beginning and
at the end that does the word wrapping.
"sleep-timer" was first done via a one liner, until I got sick of that one-liner - because of how I needed editing that one-liner to alter the sleep pause accordingly to the momentarily needs (e.g. 25 minutes for rice cooking, 10 minutes for pasta and such); now in "sleep-timer" that's simply the 1st parameter.
The one liners have been e.g.
when tz is a global function on my systems and daueralarm.sh is a script that sits in all my /usr/local/bin .
sleep-timer's sole dependency is another script of mine, daueralarm.sh - I already posted that here (using a more English only name since Dauer is German for "permanent" or "certain time that passes") as "sleepalarm.sh" but in my systems the script is called "daueralarm.sh"
If you want to use sleep-timer either tweak the code into it reading "sleepalarm.sh" instead of "daueralarm.sh" or create a symlink named "daueralarm.sh" that points to "sleepalarm.sh".
You first have to make "sleepalarm.sh", look above for its code, and you also need an appropriate sound file that gets your attention as a kind of alarm clock sound while not being overt annoying.
Code: Select all
#!/bin/sh
MYNAME=sleep-timer
MYVERSION=0.1 # blame Rava
#dependencies: daueralarm.sh
#function(s): tz
tz ()
{
echo $(date +%d.%m.%Y\ %H:%M:%S) ____________________________________________________________
}
MYCOL=$(tput cols)
if [ $# -ne 2 ];then
echo $MYNAME v$MYVERSION - I need exactly two parameters:
echo "● Parameter One: The time you want me to sleep. It could be '90' - that is 90 seconds, or '4m' - that is 4 minutes etcetera. When the called command 'sleep' prints an error correct that error and try again."| fold -sw $MYCOL
echo "● Parameter Two: The pause for daueralarm.sh in seconds - e.g. 5 or 30. When daueralarm.sh prints an error correct that and try again. In that case you would not get reminded of the sleep time being over."| fold -sw $MYCOL
exit 1
else
echo $MYNAME v$MYVERSION
echo sleep time: $1
echo pause time for daueralarm.sh: $2
tz;sleep $1;tz;daueralarm.sh $2
fi
What I could have added it the check if the dependency "daueralarm.sh" is met, but I was too lazy doing so. It says so in its code, that's enough for now.
This is what it looks like in action:
Code: Select all
guest@porteus:/$ sleep-timer 5m 25
sleep-timer v0.1
sleep time: 5m
pause time for daueralarm.sh: 25
14.10.2022 17:44:16 ____________________________________________________________
14.10.2022 17:49:16 ____________________________________________________________
daueralarm.sh V1.2 - Sounddatei ist /sound/notification_-15dB.mp3
Pausenlaenge: 25 Sekunden - Abbruch mit Strg+C
^C
guest@porteus:/$
My daueralarm.sh speaks German, if you want to understand it run your own version of sleepalarm.sh - or ask DeepL via
https://www.deepl.com/translator
Of course sleep-timer could be coded a bit differently:
When only one parameter is given assume that's the sleep time. And presume the user is okay with a longer pause for daueralarm.sh - 25 seconds.
When 2 parameters are given - same as above.
When none or more than 2 are given: error, only explaining that the 2nd parameter is optional, and the 1st is obligatory.