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#151 by Rava » 15 Oct 2022, 05:07

Cave!
This might convern only a very small minority of you fine readers, but still I want to mention it.

There are several ways to use colours in the terminal / in the virtual console.
One is by using tput
Another is by using escape sequences - that has the advantage that you not need "echo -e" - it works with any

Code: Select all

echo "whatever"
but also with any

Code: Select all

cat <<EOF
this text will be printed on screen
yadda yadda
Lorem Ipsum
EOF
But now for the Cave and the

Code: Select all

echo "really long text that needs line break" | fold -sw $MYCOL
(aside from the need to first initialise MYCOL - see above where I presented you the sleep-timer script.)

When you use

Code: Select all

echo "…" | fold -sw $MYCOL
hack on a line with colours changing output, fold is unable to distinguish between normal text that will be printed and escape sequences that will change colours in this case, but will not print any single characters by itself.

Still fold counts these escape sequences as if they were normal text to be output and therefore the line is folded / word wrapped much earlier than should be.
I need screenshots to demonstrate since I cannot output the escape sequences in any [ code ] or [ quote ] block, so you cannot test it yourself - unless I would create a test script and upload it verbatim via intertubes.

This is the output using the echo / fold hack. The line should not have ended at "hierarchy":
Image
_________________________________________

And below is how it looks like now when the terminal is 80 characters. I used

Code: Select all

cat <<EOF
"the text"
EOF
instead of echo / fold - thus using a forced line break at column 80…
Image
Image I could not come up with a better solution on the spot that incorporates colours and is able to fold at any kind of current $COLUMNS the terminal might have…
Last edited by Rava on 11 Nov 2022, 04:14, edited 2 times in total.
Reason: typo, "ourself" should have read "yourself"
Cheers!
Yours Rava

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#152 by Rava » 28 Oct 2022, 00:13

Just in case you missed that thread:
[HOWTO] save log files when using no changes / always fresh approach in Port 3,4,5.
it contains the explanation and the code of two of my scripts - tail+tee_VLM_sda2.sh and cat-into-var_log_messages.sh
Rava wrote:
23 Oct 2022, 20:43
I hope these scripts are helpful to some of you.
:)
Cheers!
Yours Rava

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#153 by Rava » 29 Oct 2022, 20:37

Finally once more an update to my /usr/local/bin/aliasset (which sets aliases and functions)

Old version:

Code: Select all

# aliasset V3.6.41 (2022-01-01)
[…]
function lc () { /bin/ls -o  --time-style=long-iso "$@" |cut -c 14-93; }
function lcr () { /bin/ls -oatr  --time-style=long-iso "$@" |cut -c 14-93; }
function lcrt () { /bin/ls -oatr  --time-style=long-iso "$@" |tail -n 26|cut -c 14-93; }
Old version printed user name and was inflexible, always cutting off at $COLUMNS 80

New version omits user name (maybe I will create versions for omitting user names and printing user names) and reads COLUMNS via tput cols to do a flexible calculation:

Code: Select all

# aliasset V3.6.42 (2022-10-30)
[…]
function lc () { /bin/ls -o  --time-style=long-iso "$@" |cut -c 20-$(expr 19 + $(tput cols)); }
## ## was: cut -c 14-93; } -> fixed to cut of user name: cut -c 20-99; }
function lcr () { /bin/ls -oatr  --time-style=long-iso "$@" |cut -c 20-$(expr 19 + $(tput cols)); }
function lcrt () { /bin/ls -oatr  --time-style=long-iso "$@" |tail -n 26|cut -c 20-$(expr 19 + $(tput cols)); }
The variants for flexible calculation and printing user names would be using this cut instead at the end:

Code: Select all

| cut -c 14-$(expr 13 + $(tput cols))
Added in 13 minutes 41 seconds:
The difference: the new flexible calculations one on a full-screen terminal with COLUMNS = 146

Code: Select all

root@porteus:/Porteus_modules# tput cols
146
root@porteus:/Porteus_modules# lcrt 011-slapt-get-0.11.6-x86_64-2gv*
737280 2022-09-08 20:57 011-slapt-get-0.11.6-x86_64-2gv_2022.09.08.xzm
  2578 2022-10-07 04:10 011-slapt-get-0.11.6-x86_64-2gv.txt
737280 2022-10-07 23:18 011-slapt-get-0.11.6-x86_64-2gv_2022.10.07.xzm
733184 2022-10-09 21:41 011-slapt-get-0.11.6-x86_64-2gv_2022.10.09.xzm
737280 2022-10-11 16:28 011-slapt-get-0.11.6-x86_64-2gv_2022.10.11.xzm
733184 2022-10-22 07:32 011-slapt-get-0.11.6-x86_64-2gv_2022.10.21.xzm
749568 2022-10-29 22:02 011-slapt-get-0.11.6-x86_64-2gv_2022.10.30.xzm
    46 2022-10-29 22:03 011-slapt-get-0.11.6-x86_64-2gv-RECENT.xzm -> 011-slapt-get-0.11.6-x86_64-2gv_2022.10.30.xzm
and the manually simulated old fixed version also on the terminal with COLUMNS = 146

Code: Select all

root@porteus:/Porteus_modules# tput cols
146
root@porteus:/Porteus_modules# /bin/ls -oatr  --time-style=long-iso 011-slapt-get* |tail -n 26|cut -c 20-99
737280 2022-09-08 20:57 011-slapt-get-0.11.6-x86_64-2gv_2022.09.08.xzm
  2578 2022-10-07 04:10 011-slapt-get-0.11.6-x86_64-2gv.txt
737280 2022-10-07 23:18 011-slapt-get-0.11.6-x86_64-2gv_2022.10.07.xzm
733184 2022-10-09 21:41 011-slapt-get-0.11.6-x86_64-2gv_2022.10.09.xzm
737280 2022-10-11 16:28 011-slapt-get-0.11.6-x86_64-2gv_2022.10.11.xzm
733184 2022-10-22 07:32 011-slapt-get-0.11.6-x86_64-2gv_2022.10.21.xzm
749568 2022-10-29 22:02 011-slapt-get-0.11.6-x86_64-2gv_2022.10.30.xzm
    46 2022-10-29 22:03 011-slapt-get-0.11.6-x86_64-2gv-RECENT.xzm -> 011-slapt-
Cheers!
Yours Rava

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#154 by Rava » 30 Oct 2022, 04:32

I just realized while writing this post in Port 5.0 c86-64 at least vim is no longer existing, it was replaced by the minimalistic vi version of busybox v1.35.0…


Anyhow, I just realized, at least with the Port 5.0 standard setup, when editing a file via mcedit (the internal editor of mc, also available as stand-alone executable as - maybe you already guessed it - mcedit) it will display some files with syntax highlighting, e.g. .c or .sh files, which is very neat and the only terminal based editor I know that does. (Emacs sure will also be able doing so, but I was always the vi / vim kind of guy and not part of the emacs fangirls and fanboys - but nowadays that means nil to most Linux users; I presume most never heard of and never used emacs and / or vi (vim) - especially getting vi to save the file is more than a cryptical command you hardly will be able to find out for yourself. And that vi/vim usually blocks the Ctrl+C command and by doing so keeps on pestering a starting to despair user is not really helping. And so the first experience with vi/vim is often off-putting for people - try it out for yourself [ I dare you ] :D - the busybox version of vi also blocks Ctrl+C)

Code: Select all

guest@porteus:/$ vi -H 2>/dev/null|grep Signal\ catching
	Signal catching- ^C
The busybox version of vi mentions something in its vi -H ( List available features):

Code: Select all

guest@porteus:/$ vi -H 2>/dev/null|grep "colon mode command"
	Some colon mode commands with :
but that the so called colon mode acts as the main command pool for using vi (saving, quitting and all these essentials) is not explained or even hinted at… You know when you already know, but when you do not know prior you hardly could guess what that "colon mode commands" are about. Or what the most essential colon commands are - writing to a file (:w[enter]) or quitting (:q) or forcing write and quit even when some errors might occur (like writing is impossible since you have not write permission to that file or it's on a read-only-partition or you try to edit a file inside a squashfs container (:wq!)
Or, how to ender edit mode and how to exit edit mode, since in edit mode entering ":wq![enter]" would just insert this into the file

Code: Select all

old line 1
:wq!
old line 2
- to exit edit mode just press escape. At least that one command is intuitive. :)
────────────────────────────────────
Anyhow², ramble aside, what I dislike is when using mc / mcedit to view a file (either via viewing a file in mc (e.g. by pressing F3) or via using "mcedit -v filename.sh") there is no syntax highlighting.

Only when editing the file the code is displayed via syntax highlighting.

Does anyone know of a setup way of getting mc and mcedit to also syntax highlight files that only get displayed but not edited?
Cheers!
Yours Rava

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#155 by Rava » 02 Nov 2022, 09:11

Two posts that are more or less coding related.

The 1st is not sitting in here cause I found the code back in 2013 (!) and have no clue where I got it, therefore it's some Anons coding goodie so to speak but not mine:
cleanup_sessionstore.py for Palemoon or Firefox
As you can see by the file extension, it's a python script. :)
─────────────────────────────────────────────────────────
The 2nd I will quote. It's from here: blender module build script (Post by Rava #91285)
Rava wrote:
02 Nov 2022, 09:05
beny wrote:
02 Nov 2022, 08:58

Code: Select all

root@porteus:~#  `which python3`
Python 3.10.4 (main, Mar 23 2022, 23:05:40) [GCC 11.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
I presume that translate into something like this:

Code: Select all

root@porteus:/tmp# file `which python3`
/usr/bin/python3: symbolic link to python3.10.4
or this

Code: Select all

root@porteus:/tmp# file `which python3`
/usr/bin/python3: symbolic link to python3.10
Added in 3 minutes 10 seconds:
In case someone wonders why

Code: Select all

`which python3`
starts python, which just tells us which program with full path "python3" is:

Code: Select all

root@porteus:/tmp# which python3
/usr/bin/python3
therefore typing

Code: Select all

python3
or

Code: Select all

`which python3`
is equivalent here.
Added in 7 minutes 16 seconds:
In itself `which python3`is not of any real use.
But in connection of file it is:
my script "fw" does just that, aside from a bit of error handling it is practically mostly a

Code: Select all

root@porteus:/usr/local/bin# grep \`which fw
    file `which "$1"` 2>/dev/null
and why use it?
E.g. when you want to know if some program is a binary or a script. A script you can look into and probably even fix a bug or something that bothers you, while with a binary you cannot.
You could try getting the source code and look into that, but with a script the source code is already in your system and readable all the time.

Some examples:

Code: Select all

root@porteus:~# fw make-ffplay-script 
/usr/local/bin/make-ffplay-script: POSIX shell script, Unicode text, UTF-8 text executable
root@porteus:~# fw make-991-usr_local_bin.sh 
/usr/local/bin/make-991-usr_local_bin.sh: POSIX shell script, Unicode text, UTF-8 text executable, with escape sequences
root@porteus:~# fw python3.9
/usr/bin/python3.9: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, stripped
root@porteus:~# fw t
/usr/local/bin/t: Bourne-Again shell script, ASCII text executable
root@porteus:~# 
Cheers!
Yours Rava

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#156 by Rava » 04 Nov 2022, 07:46

When you are interested in coding small or medium applications using a script based approach that use a GUI, maybe you want to go beyond a mere bash script one day?

If so I recommend using python. It is especially designed to be as helpful for the programmer as can be, and it is by standard part of Porteus.

E.g. check these code examples out:
https://iot4beginners.com/look-and-feel ... ython-gui/

Some examples from that page (not put into code by be and not run on my own port system, therefore it uses the iot4beginners.com's standard GUI colours and not my XFCE dark scheme):
Image
or
Image
or
Image
:)

Added in 6 minutes 2 seconds:
Cave!
Porteus needs some additional libraries to make the tkinter-python work:

Code: Select all

guest@porteus:/welt/programmieren/python$ ./spin-box-widget-demo.py 
Traceback (most recent call last):
  File "/welt/programmieren/python/./spin-box-widget-demo.py", line 4, in <module>
    from tkinter import *
  File "/usr/lib64/python3.9/tkinter/__init__.py", line 37, in <module>
    import _tkinter # If this fails your Python may not be configured for Tk
ImportError: libtk8.6.so: cannot open shared object file: No such file or directory
(or library, at least it demands libtk8.6.so)

I have to figure out how much weight that puts on a standard Port to "configure Python for Tk" =@

Or alternatively I look into how Port's own python GUI scripts handle such matters and maybe find a different approach that work out of the box for our Port python.

In Arnie's voice "I'll be back…" :D
Cheers!
Yours Rava

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#157 by Rava » 05 Nov 2022, 06:42

Once more quoting myself since the special option "--" also comes in handy when coding.
Rava wrote:
05 Nov 2022, 06:24
[…]
FYI, many core terminal utilities know of the special "--" parameter as well, not only yt-dlp.

E.g. you have files called "-silly-filename" and "--silly-filename2"

Code: Select all

guest@porteus:~$ cd /tmp
guest@porteus:/tmp$ touch -- -silly-filename --silly-filename2
Now trying to use ls to look at details fails:

Code: Select all

guest@porteus:/tmp$ ls -o -silly-filename --silly-filename2
/bin/ls: invalid option -- 'y'
Try '/bin/ls --help' for more information.
but adding -- at the right place succeeds:

Code: Select all

guest@porteus:/tmp$ ls -o -- -silly-filename --silly-filename2
-rw-r--r-- 1 guest 0 2022-11-05 07:26 --silly-filename2
-rw-r--r-- 1 guest 0 2022-11-05 07:26 -silly-filename
I looked into man ls and found no mention on option "--"

In short, option "--" means "end of options, rest is other parameters or filenames given to the program, but no program specific options"

Therefore

Code: Select all

ls -o -- -silly-filename
reads option "-o" filename "-silly-filename"
while

Code: Select all

ls -- -o  -silly-filename
reads "no options", filenames "-o" and "-silly-filename" which will fail for filename "-o":

Code: Select all

guest@porteus:/tmp$ ls -- -o  -silly-filename
/bin/ls: cannot access '-o': No such file or directory
-silly-filename
HTH.
:)

Added in 11 minutes 51 seconds:
Cave!
having such filenames can be dangerous.


E.g. you have a file name starting with "-" and containing one lower or upper case "r" / "R" . Now you want to delete that file using rm - when given to rm be read as its option -r - meaning
man rm

Code: Select all

-r, -R, --recursive  remove directories and their contents recursively
So in this case when done maliciously correct you could be tricked into deleting directories and their contents recursively due to you having given rm the option of "-r" when you meant only to delete a file "-WHATEVER" since WHATEVER contains "R" - when all other parts of the file name would translate into valid options for rm, else rm only complains like ls did above. (/bin/ls: invalid option -- 'y')
Cheers!
Yours Rava

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#158 by Rava » 11 Nov 2022, 04:48

Rava wrote:
14 Oct 2022, 18:01
my below script "sleep-timer".
Since sleep-timer simply uses sleep for, maybe you guessed it, sleeping :D my script can also handle decimal part (as in: fractions of a minute).
E.g. start sleep-timer with

Code: Select all

sleep-timer 0.1m 10
tells sleep to sleep for 1/10th of a minute, and that's 6 seconds:

Code: Select all

guest@porteus:~$ sleep-timer 0.1m 10
sleep-timer v0.1
sleep time: 0.1m
pause time for daueralarm.sh: 10
11.11.2022 05:35:22 ____________________________________________________________
11.11.2022 05:35:28 ____________________________________________________________
daueralarm.sh V1.2 - Sounddatei ist /mnt/sda4/sound/notification_-15dB.mp3
Pausenlaenge: 10 Sekunden - Abbruch mit Strg+C
^C
More commonly used would be half a minute and not one-tenths of a minute.

Here the example of using 8 1/2 minutes :

Code: Select all

guest@porteus:/$ sleep-timer 8.5m 25
sleep-timer v0.1
sleep time: 8.5m
pause time for daueralarm.sh: 25
11.11.2022 05:10:53 ____________________________________________________________
11.11.2022 05:19:24 ____________________________________________________________
daueralarm.sh V1.2 - Sounddatei ist /mnt/sda4/sound/notification_-15dB.mp3
Pausenlaenge: 25 Sekunden - Abbruch mit Strg+C
^C
10:53 + 8:30 is indeed 19:23 and not 19:24 but this was a rare occasion when the slim execution delay of the script and its sub-routines (=called programs and functions) causes the mismatch in the expected time.

When initially executed near to the next full second (meaning it is treated as the current second, all fractional parts are ignored) the 2nd time the function tz() is called it can happen at that time due to the minimal delays by then the next full second already started. Thus we have the end report of "05:19:24" instead of the expected "05:19:23"

Added in 3 minutes 18 seconds:
If you want to see the code of sleep-timer click the ↑ link in the head of the initial [ quote ]
Cheers!
Yours Rava

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#159 by Rava » 16 Nov 2022, 20:04

Since one of the things that I do on a regular basis is playing a 1 hrs white noise sound file on loop I decided to not use my /usr/local/bin/ffplay-hide_banner-loop script with the file name again and again (and needing to either navigate via Thunar into the WhiteNoise/ folder or go via guest xfce4-terinal to the same WhiteNoise/ folder), thus I decided to create a script for doing just that.

The 1st version was in the folder the white noise sound files itself sits, but then I decided to move the script "Vintage 70s Hoover 1 hr.sh" itself into /usr/local/bin/ as well, thus the only change needed was to add the full path of the sound file, and now it can be executed from any guest terminal, regardless of the current directory the terminal itself is in.

It adds the minimum of error checking (only checks if the target file exists, if the file exists but is no multimedia file then ffplay will most likely report an error and abort.)

Since this script uses the true escape sequences that lets you use colours in the echo output even without using echo's -e parameter, I cannot post the [ code ] itself here, since the escape character cannot be handled, thus you get it as a mini module.

You will have to adjust the script, for the module I renamed "Vintage 70s Hoover 1 hr.sh" into Demo-ffplay-loop-sound.sh - but you have to manually adjust the path and the sound file name since I cannot guess what you want that to be.

You need to adjust

Code: Select all

myfile="/mnt/sda2/sound/MyLoopSound.webm"
to a valid path and file name that suits your need.
else you only get this:

Code: Select all

guest@porteus:~$ Demo-ffplay-loop-sound.sh
/usr/local/bin/Demo-ffplay-loop-sound.sh -- fatal error: target file
/mnt/sda2/sound/MyLoopSound.webm
not found… aborting.
guest@porteus:~$ 
While by default the file /usr/local/bin/Demo-ffplay-loop-sound.sh is owned by root group root it is not meant to be executed by root, so I added an extra check to prevent root from being the one running the script.

Trying to run the Demo script as user root you get this error only:
root@porteus:~# Demo-ffplay-loop-sound.sh
/usr/local/bin/Demo-ffplay-loop-sound.sh -- fatal error: Do not execute me as user root, or how ALSA puts it
ALSA: Couldn't open audio device: Connection refused
This could e.g. happen if you try to connect to a non-root PulseAudio as a root user, over the native protocol. Don't do that.

Here is the module:
https://filehorst.de/d/eFiAGHwd

The md5sum is like filehorst says it is: 212ec429aa7df9779800d4ca79dd1ff4

Since filehorst speaks German, a short HOWTO translation for downloading files:

To get it, first wait 15 seconds "Dein Download wird jetzt vorbereitet." (Your download is being prepared)
Then click "Jetzt zur Download-Seite" (Now to the download page)
Then click "Dein Download steht nun bereit. Klicke hier, um ihn zu starten." (Your download is ready. Click here to start downloading it)Downloads: 1 (4 kB) Zuletzt am: 16.11.2022 21:29 Uhr

Added in 1 day 10 hours 15 minutes 1 second:
Here an extract from my /usr/local/bin/echo.colors - more details about colours and other stuff (bold, flashing, right-aligned) since my above mini module only uses bold+red and bold+yellow.

I not use the true escape sequenzes here since you still cannot use these in a [ code ] segment, therefore the string variant is used.
Use these in a script e.g. like so:
(after first initializing the strings, of course)

Code: Select all

# initializing - demo - only bld red off
bld="\033[1m"	# bold
red="\033[31m"	# red
off="\033[m"	# off
echo -e $bld${red}This is a warning or error message in bold+red.$off
echo this is again normal text.
or like this if you prefer:

Code: Select all

echo -e "$bld${red}This is a warning or error message in bold+red.$off
this is again normal text."
See for yourself:
Image
Here now the more complete list of the codes as string variant

Code: Select all

# details also see man ls; man console_codes
bld="\033[1m"	# bold
und="\033[4m"	# underline
fla="\033[5m"	# flashing (Cave! flashing works on some terminals but not on all!)
red="\033[31m"	# red
gre="\033[32m"	# green
yel="\033[33m"	# yellow
blu="\033[34m"	# blue
mag="\033[35m"	# magenta
cya="\033[36m"	# cyan
#spc="\033[60G"	# aligned to the right (column 60)
off="\033[m"	# off
# MORE THAN ONE IN ONE GO, e.g. red and bold like so:
#	\033[01;31m
HTH.
Cheers!
Yours Rava

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#160 by Rava » 19 Nov 2022, 21:39

Today i give you the output of one of my help.* scripts - help.mpv

I use [ quote ] instead of [ code ] since quote let me use colors, and I use the colours to simulate what help.mpv looks like.
help.mpv V0.3
some mpv keyboard hints
. next frame
, previous frame
<- -> seek 5 seconds
shift <- (...) seek 1 second
e panzoom in
w panzoom out both only when video is not fullscreen

regular zoom: alt+ / alt-

[ and ] Decrease/increase current playback speed by 10%.
{ and } Halve/double current playback speed.
BACKSPACE Reset playback speed to normal.

my mpv (as in my scripts)
mpv --player-operation-mode=pseudo-gui --video-sync=display-resample --no-osd-bar --audio-file-auto=fuzzy

forcing aspect ratio
16:9
--video-aspect-override=1.777777

forcing aspect ratio 4:3
--video-aspect-override=1.333333
_________________________________
Sources: https://defkey.com/mpv-media-player-shortcuts
https://mpv.io/manual/master/
mpv --list-options |less
the original is black background and white text, therefore the bright yellow of the script name and version has very good visibility Unlike above with standard white background, then bright yellow is not that good of a visibility.. :)
Cheers!
Yours Rava

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#161 by Rava » 22 Dec 2022, 02:36

sorry for the late reply I oversaw your post when i added the mpv part. My bad. :)
Ed_P wrote:
02 Nov 2020, 06:28
I don't understand your echo code. Initially the bold and yellow colors are coded as: ${color} but then the off and yellow are specified without the brackets: $off$yel. Does this work? I thought the brackets were mandatory.
They are recommended.
You can leave them out when there is no ambiguity.
E.g. this

Code: Select all

echo -e "${bld}${yel}$MYNAME$off$yel V$VERSION${off}"
works and this

Code: Select all

echo -e "${bld}${yel}BLABLABLA$off$yel V$VERSION${off}"
also works but this

Code: Select all

echo -e "${bld}$yelBLABLABLA$off$yel V$VERSION${off}"
fails. Because there might be a $yel just by itself (aka a ${yel}) but there is no $yelBLABLABLA
I hope this explained things a bit better for you.

It is sure recommended not being such a lazy bone as I am at times. Always using the brackets like always ${off}${yel} gets you on the safe side of coding.
While leaving the brackets out when there is no ambiguity like in my [ code ] snippet above works for just that one case, and fails when the code only differs in a seemingly minor way like the explanation above tried to… well, explain it to you.
Therefore, being less lazy and use the brackets all the time gets you a more robust, less error prone code, especially when you tend to use Dr. Copy & Mr. Paste when working on adding code, since then errors like in the above example can easily happen.
Cheers!
Yours Rava

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#162 by Rava » 22 Dec 2022, 02:38

I might have explained creating an ext2 container file by scratch already somewhere above but since I posted a more elaborate answer to a question, I copy that answer into here as well for your convinience.

The original post's URL is this: container file system advice (Post by Rava #92251)
_______________________________________________________
rych wrote:
10 Jan 2018, 00:16
How do I go about creating my own container image file
I do it like so, for that purpose I create ext2 container files.

Code: Select all

dd if=/dev/zero of=YOURNAMEHERE.ext2 bs=1M count=60 # create 60 MB container
mkfs.ext2 -F YOURNAMEHERE.ext2 -L LABEL # format
tune2fs -r 50 YOURNAMEHERE.ext2          # tune
Replace YOURNAMEHERE with the name of your ext2 container.
Replace LABEL with the label your container should have. You could leave the label part out but I recommend using it, it doesn't slow the mkfs.ext2 command and it gives you a hint, maybe many many moons later for what a certain ext2 container was initially created for, e.g. when you left it lying around unused for a while.
The tune2fs reduces the often unneeded high standard reserved_blocks_counts to a mere 50.

And of course adjust the "60" in the dd count= part to whatever MB your container should have.
E.g. you want 1 GB, use this:

Code: Select all

dd if=/dev/zero of=lsfind.ext2 bs=1M count=1024
Cave! The tweak of reducing the standard reserved_blocks_counts to 50 is okay for a small container, like 20 MB or 80 MB, but it should definitely be higher for a much larger container file, e.g. a mere 50 standard reserved_blocks_counts is too few for an ext2 1 GB container file.

Please feel free to either have a quick glance at all the other options (and what the few options I use as quoted above are for) by doing these

Code: Select all

dd --help
mkfs.ext2   # there is no mkfs.ext2 --help nor is there a mkfs.ext2 -h
tune2fs # there is no tune2fs --help nor is there a tune2fs -h
though I personally recommend the man pages:

Code: Select all

man dd
man mkfs.ext2
man tune2fs 
(man mkfs.ext2 is actually man mke2fs)

When you use reserved-blocks-percentage instead of standard reserved_blocks_counts you can give that parameter to mkfs.ext2 directly and thus skip the invocation of tune2fs entirely. And yes, a half percent of reserved-blocks-percentage would work:

Code: Select all

root@porteus:/tmp# df -Tm .
Filesystem     Type 1M-blocks  Used Available Use% Mounted on
aufs           aufs       388    62       326  16% /
root@porteus:/tmp# dd if=/dev/zero of=TEST.ext2 bs=1M count=100 # create 100 MB container
100+0 records in
100+0 records out
104857600 bytes (105 MB, 100 MiB) copied, 0.0661513 s, 1.6 GB/s
root@porteus:/tmp# mkfs.ext2 -F TEST.ext2 -L 100MB_TEST -m 0.5
mke2fs 1.46.5 (30-Dec-2021)
Discarding device blocks: done                            
Creating filesystem with 102400 1k blocks and 25584 inodes
Filesystem UUID: 94c3f80f-bf87-4241-b7e4-cac88b65570a
Superblock backups stored on blocks: 
	8193, 24577, 40961, 57345, 73729

Allocating group tables: done                            
Writing inode tables: done                            
Writing superblocks and filesystem accounting information: done 

root@porteus:/tmp# fsck.ext2 TEST.ext2
e2fsck 1.46.5 (30-Dec-2021)
100MB_TEST: clean, 11/25584 files, 7985/102400 blocks
root@porteus:/tmp# df -Tm .
Filesystem     Type 1M-blocks  Used Available Use% Mounted on
aufs           aufs       388    63       326  17% /
root@porteus:/tmp# ls -o TEST.ext2
-rw-r--r-- 1 root 104857600 2022-12-22 03:16 TEST.ext2
root@porteus:/tmp# mkdir /100MB_TEST
root@porteus:/tmp# mount TEST.ext2 /100MB_TEST
root@porteus:/tmp# df -Tm /100MB_TEST
Filesystem     Type 1M-blocks  Used Available Use% Mounted on
/dev/loop20    ext2        93     1        92   1% /100MB_TEST
HTH! :)

Added in 22 hours 22 minutes 46 seconds:
Not only would a half percent work when invoking

Code: Select all

-m 0.5
but also less than that, e.g.

Code: Select all

-m 0.2
or even

Code: Select all

-m 0.1
But do not go too small, while your created container file gets a bit more usable space, its safety can be compromised when you reduce the reserved-blocks-percentage or reserved_blocks_count by too much.

For my lsfind suite I use 60 MB container files for years now, and these all have 50 reserved blocks which is much lower than what mkfs.ext2 initially assigns - and I never ran into any issues. But a 60 MB container file is not a very large one, and 50 reserved blocks is to be seen in relations to the 60 MB - therefore I would not use a mere 50 reserved blocks for a container file of 500 MB.
(Calculating this relatively, using 50 reserved blocks for a container file of 60 MB translates into 0.8333 reserved blocks / MB . Meaning a 500 MB container file would have the same relative amount of reserved blocks / MB when giving it 417 reserved blocks.
Or for a 1 GB container file (1024 MB) : assigning 853 reserved blocks)
Last edited by Rava on 23 Dec 2022, 00:58, edited 3 times in total.
Reason: Coloured the Cave! part to make it stand out more. // Added the missing --help for mkfs.ext2 and tune2fs
Cheers!
Yours Rava

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#163 by Rava » 23 Dec 2022, 00:35

Fore the ones interested, I posted a more elaborate thread about pimping an existing module via [HOWTO] ROOT=/path/ removepkg package here:
[HOWTO] ROOT=/path/ removepkg package
Cheers!
Yours Rava

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#164 by Rava » 31 Dec 2022, 14:07

Is is complicated to let you know if a script command succeeded or not?

Not really.
The main part is this code

Code: Select all

some_command && echo "some_command successful" || echo "some_command failed"
Let's presume you want to background download a bunch of videos from e.g. dailymotion.com. The (fake) demo URL being "ABC", and we presume the title of that video is "Series X S1E1", then the code would be like so:
yt-dlp https://www.dailymotion.com/video/ABC && notify-send -i info -t 0 "Downloading Series X S1E1" "finished" || notify-send -i error -t 0 "Downloading Series S1E1" "failed"
This is what the popups will look like when downloading "Series X S1E1" succeeds and when downloading "Series X S1E2" failed:
Image
Be aware that the code has no timeout for the popup(s), the popup stays forever on your desktop until you click on it.
I choose it like so since downloads can take some time and when I am AFK (away from keyboard), say, for a mere minute then I would miss the usual expired popup.
Look up notify-send --help if you want to change that behaviour:
guest@porteus:/$ notify-send --help
Usage:
notify-send [OPTION…] <SUMMARY> [BODY] - create a notification

Help Options:
-?, --help Show help options

Application Options:
-u, --urgency=LEVEL Specifies the urgency level (low, normal, critical).
-t, --expire-time=TIME Specifies the timeout in milliseconds at which to expire the notification.
-a, --app-name=APP_NAME Specifies the app name for the icon
-i, --icon=ICON[,ICON...] Specifies an icon filename or stock icon to display.
-c, --category=TYPE[,TYPE...] Specifies -t, --expire-time=TIME Specifies the timeout in milliseconds at which to expire the notification. the notification category.
-h, --hint=TYPE:NAME:VALUE Specifies basic extra data to pass. Valid types are int, double, string and byte.
-v, --version Version of the package.
(highlighting by me)

Added in 4 minutes 29 seconds:
Be aware that my use of the "-t" option as "-t 0" is not documented via notify-send --help.

Added in 23 minutes 57 seconds:
I forgot to mention:
The above code and all and any code that checks for the return value of $? of an executed command like so (presuming "error_message" is a function you defined at the start of your script that displays the given parameter as bold red text, and probably does some other stuff like removing temp files and temp folders.)

Code: Select all

mkdir /tmp/tmp$$
if [ $? -ne 0 ] ; then
     error_message "Could not create temp directory. Aborting…"
     exit 1
fi
# here comes the code when the script can continue.
or via && and/or || as seen above will fail when the script or program fails but still returns a "0" (a zero) then another script or program presumes (wrongly) that the script or program did succeed.

Especially when you did code a script by yourself you have to include all kinds of checks, you cannot presume a boring command like creating a directory or writing into a file is always successful. That could fail e.g:
● when the user has no write permission
● when the target partition is mounted read-only, or
● when there is no free space available on the target partition.
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#165 by Ed_P » 31 Dec 2022, 23:42

:shock: Thanks Rava. :good:
Ed

Post Reply