I´m wondering if anyone could help me with a certain “problem”.
I´ve written a script for a countdown timer which after starting shows me the time left.
The most basic and important parts I found somewhere on the stackexchange (if I remember correctly); I just modified and expanded them according to my purposes.
Basically it works like this:
I enter the stop-time
the script shows how many days, hours, minutes and seconds are left
I enter the respective values (unaltered or altered, just to my liking)
As a result the remaining time counts down to zero, which is displayed in the last line of the output within the terminal.
That´s nice and I´m basically satisfied with the script so far.
Yet it would be an added benefit if there were the possibility of sending the last line of the output of the script (from the terminal that is) to a dedicated popup window (which i could then set to be displayed on every desktop).
That way I wouldn´t have to switch back to desktop 1 (in my case) from where I started the script to look for the time remaining.
In the past I´ve used the command notify-send -t 0 for e.g. weather messages.
But the content of the output was fixed (so no problem there) whereas in my countdown script the output changes every second.
Now I don´t really know if it´s at all possible to use notify-send for displaying changing content (and if so how to go about configuring it).
Does anybody know if there´s a solution to the issue in question
Many thanks in advance and many greetings.
Rosika
BTW:
The countdown script goes like this:
#!/bin/bash
echo "Zeitdauer ausrechnen:" # calculate time-span for countdown
echo
echo "Stopp-Zeit eingeben:" # enter stop time
echo "Beispiel: 12:33" # just an example
read Value1 # Stopp-Zeit
Value2=$(date +%R) # present time in hrs and mins
D1=$(date -d "$Value1" '+%s'); D2=$(date -d "$Value2" '+%s') # conversion of the times in secs since 1970−01−01 00:00:00 UTC
seconds=$(($D1-$D2)) # difference in secs
TZ=UTC printf "%d days %(%H hours %M minutes %S seconds)T\n" $((seconds/86400)) $seconds # result of calculation
sleep 1
echo "Stunden"
read hour
echo "Minuten"
read min
echo "Sekunden"
read sec
while [ $hour -ge 0 ]; do
while [ $min -ge 0 ]; do
while [ $sec -ge 0 ]; do
echo -ne "$hour:$min:$sec\033[0K\r" # that´s the (changing) part which should be displayed externally
let "sec=sec-1"
sleep 1
done
sec=59
let "min=min-1"
done
min=59
let "hour=hour-1"
done
I´ve been digging around for some solution for a while now and I´ve also read the Vincent Danen article but I seems despite the fact that zenity is a pretty versatile tool for many purposes it may have difficulties in displaying varying output.
I know you like bash scripts, but I always found them a bit cumbersome, so here’s a Perl script that should do what you want (Perl is installed by default on any Linux system):
#!/usr/bin/perl
sub s2t {
# turns a number in seconds into a string HH:MM:SS
my $s = shift;
my $m = int($s/60);
$s = $s % 60;
my $h = int($m/60);
$m = $m % 60;
return "$h:".($m < 10 ? '0' : '')."$m".($s < 10 ? ':0' : ':').$s
}
print "Enter alarm time (e.g. 13:44)\n\n";
my $t = <STDIN>;
chomp($t); # remove newline at the end
die "\nfaulty entry '$t'\n" unless $t =~ /(\d\d?)\D(\d\d?)/;
my ($h,$m) = ($1,$2);
my $msg = "** Time's up! It's NOW now. **";
print "\n\nDo you want to display an additional message at the end?
Default is:\n\n$msg
'NOW' is replaced by the time, the timer stopped.
Just hit return to use the default, otherwise enter some text.
\tUse NOW for the time the timer stopped
\tand THEN for when it started.\n\n";
my $custom_message = <STDIN>;
my ($sec,$current_minute, $current_hour) = ( localtime )[0,1,2];
my $then = $current_hour.':'.($current_minute < 10 ? '0' : '').$current_minute;
print "Timer started at $then\n\n";
$sec = 60*( (($current_minute <= $m) ?
($m - $current_minute) :
(60 + $m - $current_minute) ) +
60*(($current_hour <= $h) ?
($h - $current_hour) :
(24 + $h - $current_hour) )) - $sec;
$| = 1; # turn on autoflush
while($sec >= 0) {
print "\rTime to go until $t: ".s2t($sec)." ";
sleep(1);
$sec--;
}
$| = 0; # turn off autoflush
print "\n";
my $now = join(':',(localtime )[2,1]);
chomp $custom_message;
$custom_message and $msg = $custom_message;
$now =~ s/\D(\d)$/:0$1/;
$msg =~ s/NOW/$now/g;
$msg =~ s/THEN/$then/g;
$msg =~ s/([\n\s'\("\\])/\\$1/g;
exec "notify-send -t 0 $msg";
I had this (with exception of the notify-send) lying around, anyway.
Funny thing. This goes a bit more into the direction I would’ve gone to. If you really want a GUI, I would consider a scripting language like Bash underpowered for the task. It’d be easier to just use a proper general purpose programming language, where you just import a simple GUI library and write the app with that.
Sounds harder than using Bash, but if you go out of Bash’s comfortable zone, you will quickly get into very harsh and unforgiving territory. It’s honestly just better to use a “normal” general purpose programming language. Bash is so popular, simply because it’s available on all the popular Linux distributions. So, you can write one Bash script and run it anywhere. Bash is also almost always used for DevOps tasks and less for app-like tasks, like having an end-user timer.
Here is an example of a tiny GUI program written in Nim:
It has like 4 different ways of doing something in a GUI window. Yet, the whole program only took 128 lines for me:
You only need 1 timer thing. I doubt one would need more than 60 lines for that.
Again, seems complicated, but I’m pretty sure it’s easier than trying to do this in Bash.
Of the 66 lines, 19 are empty, and 13 are just text strings to write on the terminal.
If you want to get rid of the ticking down countdown, you lose another 11, which leaves you with 23 lines of functional code (and it could be still much shorter, especially if you don’t want the custom messages).
Of course, adding a small GUI would be easy with Tk, but it requires installation. I thought, something that would just run out of the box (like a bash script) would be easier.
As you can see I indeed got a notify-send popup, but only after the countdown finished.
So I guess it´s basically designed as a reminder as it pops up at a certain time.
That´s a fine thing indeed but I was looking for a small window which displays the time as it´s running down to zero.
So the countdown should be visible at any given time.
Of course the value changes every second.
I´m afraid that´s hardly possible to achieve. At least I couldn´t do it anyway.
But thanks so much Mina. Your help is certainly appreciated.
That’s quite pessimistic and untrue. Just look at your post history to see all the Linux stuff you have managed to achieve.
That’s some random guide.
Here’s the official and easy way to do it:
Installation using choosenim
choosenim is an installer for the Nim programming language. It allows you to easily switch between versions of Nim, whether that is the latest stable release or the latest development version.
To install the latest stable release of Nim using choosenim, just run the following in your terminal, then follow the onscreen instructions:
curl https://nim-lang.org/choosenim/init.sh -sSf | sh
Well, it’s just my favourite tool. However, what I explained earlier, can be achieved with any general purpose programming language.
That said, for the sake of simplicity and saving time, you can just copy the source for the mini program I had written.
Then, you just remove the unnecessary stuff and all you add is a timer. That’s it. So the only thing you have to do on your own is the timer and that’s really easy.
You also cannot make up excuses, because you already successfully(!) were able to get along with bash very often, which means you will get along with this one, too.
This should work. Try using the command manually for the current session. If that works, it should also work in the ~/.bashrc file. You could, however, also put it in /etc/bash.bashrc, which is the same thing, but is used by all users on the system.
I should have read carefully what you’ve written in the first place …
Always a good idea.
I thought that was what you wanted. However, notify-send creates a static window, so it’s definitely not the right tool. Of course, one could set it up the script so, that it would call notify-send every minute (or second) with the -t parameter set to 60,000 or 1,000 (milliseconds) respectively so that the notification vanishes after this time, but I guess, that would be really annoying.
Otherwise, a small GUI program, as @akito suggested, would do the job, but then desktop integration would be a hassle, as every desktop is different. A KDE-widget is an easy thing to do, but completely useless for people who use other DE.
rosika2@debian ~> export PATH=/home/rosika2/.nimble/bin:$PATH
rosika2@debian ~> nim -v
Nim Compiler Version 1.4.8 [Linux: amd64]
Compiled at 2021-05-25
Copyright (c) 2006-2021 by Andreas Rumpf
git hash: 44e653a9314e1b8503f0fa4a8a34c3380b26fff3
active boot switches: -d:release
So what you suggested, works. But only for the current terminal session of course.
I´ve now placed the export command in /etc/bash.bashrc, again logged out an in, and
the new PATH variable still isn´t saved.
Perhaps I should try ~/.profile likey they suggested. Still odd though…
Please don´t be so hard on yourself. I am very grateful for any kind of help and still want to learn something new.
So thanks anyway a lot for your help.
O.K., thanks for the confirmation.
Yes, I can imagine.
Thanks anyway for your thoughts and comments.
As always they´re highly appreciated.
Still, don’t bother with learning it. Despite being present on practically every Linux system, Perl is essentially dead. It’s far more powerful than other scripting languages (bash and sorts, even tcl) and way easier to learn and to maintain than Python in order to create meaningful projects, it has completely gone out of fashion and will probably soon completely vanish into oblivion.
It might be my favourite scripting language, but that’s because I learned it when it was still the glue that holds the internet together (15+ years ago).
this is something that bothers me a great deal, because I don´t understand it.
I´ve tried everything now:
I put export PATH=/home/rosika2/.nimble/bin:$PATH into the following files (only in one file at a time of course):
~/.bashrc # didn´t work
/etc/bash.bashrc # didn´t work
~/.profile # didn´t work.
Upon having a closer look at the ~/.profile file I realized the syntax might not be entirely correct.
The “$PATH” entry was displayed in red. That cannot be right.
So taking the already existing entries as an example I put the whole definition in inverted commas. Now the entry is displayed in yellow (which seems right I guess).
Here are the respective screenshots for reference: