Ravas coding goodies

For discussions about programming and projects not necessarily associated with Porteus.
User avatar
Rava
Contributor
Contributor
Posts: 5401
Joined: 11 Jan 2011, 02:46
Distribution: XFCE 5.01 x86_64 + 4.0 i586
Location: Forests of Germany

Ravas coding goodies

Post#91 by Rava » 03 Feb 2022, 16:15

Ed_P wrote:
03 Feb 2022, 01:31
It would also be good if it did the repeat sound after a period of time the user could input so I may be playing with it in the future.
That is how I use it all the time.

E.g. wait 6 minutes for the tea to be ready:

Code: Select all

sleep 6m;alarm.sh 5
See?
Cheers!
Yours Rava

User avatar
Ed_P
Contributor
Contributor
Posts: 8341
Joined: 06 Feb 2013, 22:12
Distribution: Cinnamon 5.01 ISO
Location: Western NY, USA

Ravas coding goodies

Post#92 by Ed_P » 03 Feb 2022, 17:54

I'm thinking more:

Code: Select all

alarm.sh 5;sleep 20m;alarm.sh 5;sleep 20m;alarm.sh 5
where I am outside when the disk copy finishes and miss the 1st alarm but I like your example also. :)
Ed

User avatar
Rava
Contributor
Contributor
Posts: 5401
Joined: 11 Jan 2011, 02:46
Distribution: XFCE 5.01 x86_64 + 4.0 i586
Location: Forests of Germany

Ravas coding goodies

Post#93 by Rava » 03 Feb 2022, 21:07

Ed_P wrote:
03 Feb 2022, 17:54
I'm thinking more:

Code: Select all

alarm.sh 5;sleep 20m;alarm.sh 5;sleep 20m;alarm.sh 5
where I am outside when the disk copy finishes and miss the 1st alarm
Since alarm is a loop it will sound its file every 5 seconds when told to, so if you are anywhere near the speakers it can be hardly missed (unless the speakers are off or set to a too low volume)

What you meant is probably this:

Code: Select all

sleep 20m;beep;sleep 20m;beep
or maybe this

Code: Select all

sleep 20m;beep;sleep 5;beep;sleep 20m;beep;sleep 5;beep
I wonder how a 2nd parameter can be incorporated so that it makes sense.

The idea is that alarm.sh just acts like any regular real-world-alarm, beeping until it gets human interaction to tell it to be silent…
So, having a parameter telling it e.g. "do the loop / the sound only n times, then sleep for X minutes" makes not much sense to me.

The idea of alarm.sh is to be annoying and to alert you that whatever you told it to monitor is finished. In in that regard in my book it is a good idea to have a running-forever loop until you stop it manually.
Cheers!
Yours Rava

User avatar
Ed_P
Contributor
Contributor
Posts: 8341
Joined: 06 Feb 2013, 22:12
Distribution: Cinnamon 5.01 ISO
Location: Western NY, USA

Ravas coding goodies

Post#94 by Ed_P » 04 Feb 2022, 06:38

Rava wrote:
03 Feb 2022, 21:07
What you meant is probably this:

Code: Select all

sleep 20m;beep;sleep 20m;beep
Yes, kinda. I do like alarm.sh's 2 beeps verses a single one so your second example is more to my liking. Alarm.sh is indeed more like an alarm clock and repeats constantly. My mind is more around an alert type of sound to indicate that a process has finished and a reminder if the alert is missed. Like when you tell someone to do something and then check back every so often to check that it was done.
Ed

User avatar
Rava
Contributor
Contributor
Posts: 5401
Joined: 11 Jan 2011, 02:46
Distribution: XFCE 5.01 x86_64 + 4.0 i586
Location: Forests of Germany

Ravas coding goodies

Post#95 by Rava » 04 Feb 2022, 10:01

Ed_P wrote:
04 Feb 2022, 06:38
Alarm.sh is indeed more like an alarm clock and repeats constantly. My mind is more around an alert type of sound to indicate that a process has finished and a reminder if the alert is missed.
But what is the issue when alarm.sh just repeats constantly?
Usually you set an alarm for something you do not want to miss (like some operations be finished you want to check on the results [e.g. if compiling a software was successful], or when finished one task you start another similar task, but do not want both to run at the same time because of too much usage of harddisk or CPU.
In all these cases you want the alarm to alert you as soon as it happened (or as soon as you are back at the machine)
But feel free to use my alarm.sh as a skeleton to code a more complex script out of it.

But when you want a simple reminder you can use notify-send instead:
as guest

Code: Select all

notify-send -i info -t 0 "Encoding" "finished"
as root

Code: Select all

su guest -c 'notify-send -i info -t 0 "Encoding" "finished"'
Hard to miss a popup on the screen that will not vanish unless you click it.
Cheers!
Yours Rava

User avatar
Ed_P
Contributor
Contributor
Posts: 8341
Joined: 06 Feb 2013, 22:12
Distribution: Cinnamon 5.01 ISO
Location: Western NY, USA

Ravas coding goodies

Post#96 by Ed_P » 04 Feb 2022, 17:10

Rava wrote:
04 Feb 2022, 10:01
But when you want a simple reminder you can use notify-send instead:
as guest

Code: Select all

notify-send -i info -t 0 "Encoding" "finished"
as root

Code: Select all

su guest -c 'notify-send -i info -t 0 "Encoding" "finished"'
Hard to miss a popup on the screen that will not vanish unless you click it.
:o You are good Rava. :D I like this idea. :good: Maybe a beep and a notify script would be best. :lol:
Rava wrote:
04 Feb 2022, 10:01
But what is the issue when alarm.sh just repeats constantly?
When the oven is set to a temperature and I am watching something on tv it beeps, one time, to alert me it has reached the set temp. Not constantly like an alarm clock. If I'm watching a football game and my team is moving, I may ignore the beep, or not hear it if I'm yelling at the tv, in which case a reminder beep would be nice. Same thing with a disk to disk copy or a backup or a download. Important to know when they end but not as important as getting up for breakfast. :lol:
Ed

User avatar
Ed_P
Contributor
Contributor
Posts: 8341
Joined: 06 Feb 2013, 22:12
Distribution: Cinnamon 5.01 ISO
Location: Western NY, USA

Ravas coding goodies

Post#97 by Ed_P » 08 Feb 2022, 16:58

A quick question, where/how do you set a variable in a script to be available to other scripts? Is there a command that displays all the variables currently in use? I'm referring to variables like $PATH and $PWD. Thanks. :)
Ed

User avatar
Rava
Contributor
Contributor
Posts: 5401
Joined: 11 Jan 2011, 02:46
Distribution: XFCE 5.01 x86_64 + 4.0 i586
Location: Forests of Germany

Ravas coding goodies

Post#98 by Rava » 08 Feb 2022, 17:07

Ed_P wrote:
08 Feb 2022, 16:58
A quick question, where/how do you set a variable in a script to be available to other scripts? Is there a command that displays all the variables currently in use? I'm referring to variables like $PATH and $PWD. Thanks. :)
You can check if a variable is set.

If you want to include it in other scripts you have to export it.

man bash

Code: Select all

   -x
   Mark names for export to subsequent commands via the environment.
But that not works in a loop. Whatever kind of loop you have, if you need to export data to another script you have to use some kind of file, exporting a variable not works, unfortunately.

That is one of the big downsides of sh or bash programming, in my book.
Cheers!
Yours Rava

User avatar
Ed_P
Contributor
Contributor
Posts: 8341
Joined: 06 Feb 2013, 22:12
Distribution: Cinnamon 5.01 ISO
Location: Western NY, USA

Ravas coding goodies

Post#99 by Ed_P » 08 Feb 2022, 18:53

How is pwd set up to be available between scripts? Where is it stored?

Code: Select all

guest@porteus:~$ pwd
/home/guest
man bash and -x not as much help as https://www.geeksforgeeks.org/export-co ... -examples/

Added in 12 minutes 23 seconds:
guest@porteus:~$ declare
and
guest@porteus:~$ export

yield interesting results.
Ed

User avatar
Rava
Contributor
Contributor
Posts: 5401
Joined: 11 Jan 2011, 02:46
Distribution: XFCE 5.01 x86_64 + 4.0 i586
Location: Forests of Germany

Ravas coding goodies

Post#100 by Rava » 08 Feb 2022, 20:14

Ed_P wrote:
08 Feb 2022, 19:06
How is pwd set up to be available between scripts? Where is it stored?
Try

Code: Select all

$PWD
:D
Ed_P wrote:
08 Feb 2022, 19:06
man bash and -x not as much help as https://www.geeksforgeeks.org/export-co ... -examples/
Depends, man pages are known to be so minimal at times that they can be called cryptic.
And reading good free online bash coding books or specific articles is always enlightening.

But I dislike it when they use screen shot images instead of a text based box for the output of a text terminal. When you call yourself a geek that should be no issue coding it as text, using css for colouring maybe. (Just my 2 cents)
Reason why: because you can highlight and copy text when the example terminal output is text, but not from an image.
Ed_P wrote:
08 Feb 2022, 19:06
guest@porteus:~$ declare
and
guest@porteus:~$ export
yield interesting results.
Indeed they do, especially when you declare a variable as integer you can calculate with it, like i++ in a loop. :)
e.g. here:

Code: Select all

declare -i waitfor_sleep=$1 
as part of my waitfor.sh script.

Code: Select all

root@porteus:/usr/local/bin# waitfor.sh 
waitfor.sh V0.9
Waiting $2 seconds for process with the ID of $1 to exit;
This script will exit with exitcode 0;
Other errors are code 2.
waitfor.sh ERROR -- You must specify exact two parameters as shown above.
Unfortunately, due to a system changes years ago in Port you always have to be root for it to work, in older Port versions (really old, like 1.x or 2.x) you can run it as guest when the process you are monitoring is also run as guest.

I use it when I have a long running process and want to be altered when it's finished, like so.
Presuming PID 1000 is a long ffmpeg encoding:

Code: Select all

root@porteus:/usr/local/bin# waitfor.sh 1000 5;alarm.sh 5
waitfor.sh V0.9
[pid] = 1000    [sleep] = 5 seconds
-
The "-" character in the last line is the indicator that waitfor.sh is running, it changes like so:

Code: Select all

while kill -0 $searchpid &>/dev/null ; do
	if [ $waitfor = "|" ]; then
	    waitfor="/"
	elif [ $waitfor = "/" ]; then
	    waitfor="-"
	elif [ $waitfor = "-" ]; then
	    waitfor='\'
	elif [ $waitfor = '\' ]; then
	    waitfor="|"
	fi
	echo -ne '\b'$waitfor	
	sleep $waitfor_sleep
done
It should say

Code: Select all

Other errors are exitcode 2.
instead of

Code: Select all

Other errors are code 2.
though, but too lazy to change that even when I could bump it to version 1.0 for that. :)
Cheers!
Yours Rava

User avatar
Ed_P
Contributor
Contributor
Posts: 8341
Joined: 06 Feb 2013, 22:12
Distribution: Cinnamon 5.01 ISO
Location: Western NY, USA

Ravas coding goodies

Post#101 by Ed_P » 08 Feb 2022, 21:19

Ok, so how do I get "DRV" to be sharable to other scripts while Porteus is running.

Code: Select all

#!/bin/bash

# https://forum.porteus.org/viewtopic.php?p=86943#p86943

#set -x;
# http://forum.porteus.org/viewtopic.php?f=53&t=3801&start=30#p28472
BOOT=`grep -A1 "Booting" /var/log/porteus-livedbg|tail -n1|sed 's^//^/^g'`
SYSTM=
DRV=${BOOT:5:4}
if [ "$DRV" == "nvme" ]; then
   DRV=${BOOT:5:9}
fi
if [ "$DRV" == "isol" ]; then
   ISOBOOT=`grep -A1 "//porteus" /var/log/porteus-livedbg|tail -n2|sed 's^//^/^g'`
   SYSTM="${ISOBOOT:9:11}/"
   DRV=${ISOBOOT:5:4}
   if [ "$DRV" == "nvme" ]; then
      SYSTM="${ISOBOOT:14:11}/"
      DRV=${ISOBOOT:5:9}
   fi
fi 

echo
echo "BOOT: "$BOOT
echo "ISOBOOT: "$ISOBOOT
echo "SYSTM: "$SYSTM
echo "DRV: "$DRV
echo 
echo Finished!
sleep 8
Your waitfor indicator is interesting. :good: :D
Last edited by Ed_P on 18 Jan 2024, 20:10, edited 2 times in total.
Ed

User avatar
Rava
Contributor
Contributor
Posts: 5401
Joined: 11 Jan 2011, 02:46
Distribution: XFCE 5.01 x86_64 + 4.0 i586
Location: Forests of Germany

Ravas coding goodies

Post#102 by Rava » 08 Feb 2022, 22:34

Ed_P wrote:
08 Feb 2022, 21:19
Ok, so how do I get "DRV" to sharable to other scripts while Porteus is running.
Have you tried exporting it via /etc/rc.d/rc.local ?

Code: Select all

# this being in /etc/rc.d/rc.local
# needs reboot to work
BOOT=`grep -A1 "Booting" /var/log/porteus-livedbg|tail -n1|sed 's^//^/^g'`
DRV=${BOOT:5:4}
export DRV
Added in 1 minute 4 seconds:
Ed_P wrote:
08 Feb 2022, 21:19
Your waitfor indicator is interesting. :good: :D
It is indeed, and the waitfor.sh script is helpful, especially combined with alarm.sh at times.
Cheers!
Yours Rava

User avatar
Ed_P
Contributor
Contributor
Posts: 8341
Joined: 06 Feb 2013, 22:12
Distribution: Cinnamon 5.01 ISO
Location: Western NY, USA

Ravas coding goodies

Post#103 by Ed_P » 08 Feb 2022, 23:21

Thank you Rava. I will try that. :)

A negative thought about waitfor.sh, it's using cpu cycles that might help the main task. :(
Ed

User avatar
Rava
Contributor
Contributor
Posts: 5401
Joined: 11 Jan 2011, 02:46
Distribution: XFCE 5.01 x86_64 + 4.0 i586
Location: Forests of Germany

Ravas coding goodies

Post#104 by Rava » 09 Feb 2022, 00:09

Ed_P wrote:
08 Feb 2022, 23:21
A negative thought about waitfor.sh, it's using cpu cycles that might help the main task. :(
Hardly, it does one check every given seconds, and you can tell it to check only every 5 or only every 100 seconds, depending how much you care about a quick info that the other task is finished.

Still the best is using a one-liner and adding

Code: Select all

ffmpeg WHATEVER […];alarm.sh 5
But when you forgot that and the ffmpeg is already running too long for you to start anew, then a solution like waitfor.sh with a long wait period is the 2nd best you can do when you want to be alerted of the CPU using process being finished.

And besides, usually there are other background processes that use more CPU cycles than waitfor.sh, especially when you tell it to only check every 10 or 30 seconds.

Also, I coded waitfor.sh so that the minimum wait time is 2 seconds:

Code: Select all

guest@porteus:~$ waitfor.sh 0 1
waitfor.sh V0.9
Waiting $2 seconds for process with the ID of $1 to exit;
This script will exit with exitcode 0;
Other errors are code 2.
The wait for time is too low, I set it at a reasonable minimum of 2 seconds.
[pid] = 0    [sleep] = 2 seconds
-
See?
One could tweak waitfor.sh for the minimum wait time to be higher, e.g. 5 or 10 seconds.
Cheers!
Yours Rava

User avatar
Ed_P
Contributor
Contributor
Posts: 8341
Joined: 06 Feb 2013, 22:12
Distribution: Cinnamon 5.01 ISO
Location: Western NY, USA

Ravas coding goodies

Post#105 by Ed_P » 09 Feb 2022, 16:53

Rava wrote:
08 Feb 2022, 22:35
exporting it via /etc/rc.d/rc.local
Actually I editted the script to include:

Code: Select all

export DRV
exit
before the echos and added the line:

Code: Select all

/home/guest/DRV.sh
to the /etc/rc.d/rc.local/ file and rebooted but echo $DRV is empty. :( There's some other approaches I will try. :)
Ed

Post Reply