Ravas coding goodies

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

Ravas coding goodies

Post#211 by Rava » 01 Nov 2023, 22:10

Ed_P wrote:
01 Nov 2023, 21:49
I can assure you I don't see 860 segments listed in your
[…]
with or without the "-------- --------- -------- ----" line.
Let us look at what you quoted, the command line itself will suffice:
Rava wrote:
01 Nov 2023, 19:05
root@rava:/Porteus_modules# hdparm --fibmap warzone2100-4.1.3-x86_64-1ponce.txz |tail
(Red and bold highlighting by me; leaving out the [ code ] bbcode or else bold and coloured highlighting would not work)

I presume you do not know what the program "tail" does?
guest@rava:~$ tail --help|head -n 2
Usage: tail [OPTION]... [FILE]...
Print the last 10 lines of each FILE to standard output.
guest@rava:~$
(Red and bold highlighting by me)

When you use command | tail you will always only see the last 10 lines of what command is writing to standard output.
So of course you do not see 860 segments listed, since this huge listing is irrelevant in what I was writing about, only the last 10 lines of the output did the trick.
Cheers!
Yours Rava

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

Ravas coding goodies

Post#212 by Rava » 03 Nov 2023, 04:40

The command tput can be used for many awesome things.
E.g. determine what $COLUMNS your current terminal has to properly word-wrap a long line of output text:
Without fold and tput

Code: Select all

#!/bin/sh
cat <<EOF
Lorem ipsum dolor sit amet, consectetur adipisici elit, sed eiusmod tempor incidunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquid ex ea commodi consequat. Quis aute iure reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint obcaecat cupiditat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
EOF
result:

Code: Select all

guest@rava:/tmp$ ./lore.sh 
Lorem ipsum dolor sit amet, consectetur adipisici elit, sed eiusmod tempor incidunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquid ex ea commodi consequat. Quis aute iure reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint obcaecat cupiditat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
guest@rava:/tmp$ 
With fold and tput

Code: Select all

#!/bin/sh
cat <<EOF|fold -sw $(tput cols)
Lorem ipsum dolor sit amet, consectetur adipisici elit, sed eiusmod tempor incidunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquid ex ea commodi consequat. Quis aute iure reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint obcaecat cupiditat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
EOF
result:

Code: Select all

guest@rava:/tmp$ ./lorefold.sh
Lorem ipsum dolor sit amet, consectetur adipisici elit, sed eiusmod tempor 
incidunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis 
nostrud exercitation ullamco laboris nisi ut aliquid ex ea commodi consequat. 
Quis aute iure reprehenderit in voluptate velit esse cillum dolore eu fugiat 
nulla pariatur. Excepteur sint obcaecat cupiditat non proident, sunt in culpa 
qui officia deserunt mollit anim id est laborum.
guest@rava:/tmp$
Much better.

But to be more precise, on a 80 characters $COLUMNS terminal it does look like so when executing "./lore.sh"

Code: Select all

guest@rava:/tmp$ ./lore.sh 
Lorem ipsum dolor sit amet, consectetur adipisici elit, sed eiusmod tempor incid
unt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exer
citation ullamco laboris nisi ut aliquid ex ea commodi consequat. Quis aute iure
 reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. E
xcepteur sint obcaecat cupiditat non proident, sunt in culpa qui officia deseru
nt mollit anim id est laborum.
guest@rava:/tmp$
Not really pretty.

Conclusion:
OUTPUT TO STANDARD |fold -sw $(tput cols) is nice.

But I already mentioned that somewhere above, and tput and/or printf can do much more.

Like colours. :D


Try

Code: Select all

hostname
vs

Code: Select all

tput setaf 1; hostname; tput sgr0
Now you might ask yourself "tput setaf 1" is nice and all, and "tput setaf 1" seems to translate into "red", but what with the other colours?

Fret not, unix.stackexchange.com comes to the rescue.
This very post https://unix.stackexchange.com/question ... olor-codes to be precise.

Have some code snippets from some answers on there:

Code: Select all

#!/bin/bash
color(){
    for c; do
        printf '\e[48;5;%dm%03d' $c $c
    done
    printf '\e[0m \n'
}

IFS=$' \t\n'
color {0..15}
for ((i=0;i<6;i++)); do
    color $(seq $((i*36+16)) $((i*36+51)))
done
color {232..255}
Image
(screenshot from the https://unix.stackexchange.com/questions/269077/ article, it looks slightly different when executed in my xfce4-terminal (mainly because I use 80 characters per line), but basically the output is the same; the "015" in the first line is not visible to me, though: in my terminal that is white text on white background :wall: )

To print all 256 colours in your terminal, try the following rainbow-y one-liner:

Code: Select all

for c in {0..255}; do tput setaf $c; tput setaf $c | cat -v; echo =$c; done
or

Code: Select all

for c in {0..255}; do tput setaf $c; tput setaf $c | cat -v; echo =$c; done| column
:)

I hope you can use this to make your scripts more easily readable compared to an all-one-text-coloured-only output.

Added in 6 hours 58 minutes 31 seconds:
Silly me forgot to add the basic 8 colours table from the very same article as quoted and linked above, so here you go:

Code: Select all

Color       #define       Value       RGB
black     COLOR_BLACK       0     0, 0, 0
red       COLOR_RED         1     max,0,0
green     COLOR_GREEN       2     0,max,0
yellow    COLOR_YELLOW      3     max,max,0
blue      COLOR_BLUE        4     0,0,max
magenta   COLOR_MAGENTA     5     max,0,max
cyan      COLOR_CYAN        6     0,max,max
white     COLOR_WHITE       7     max,max,max
As you might have seen, the attribute "bold" is missing in that article. Another article explains why:
https://unix.stackexchange.com/question ... codes-bold
Bold is a character-related video attribute, usually thicker lines, but which in some devices is imitated using bright colors (a change in intensity).

RGB already gives the color intensity, so at most you'll find some terminals doing something like changing the levels a little — but nothing that you can count on in different terminals.
Cheers!
Yours Rava

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

Ravas coding goodies

Post#213 by Rava » 03 Nov 2023, 11:48

And more or less unrelated to the colours in terminal, I learned about the DEBUG trap - as part of how to colour the shell prompt, never seen that outside of debugging:

https://unix.stackexchange.com/question ... he-command

For completeness, here's how to set a prompt color and the command line input color in bash. Put these lines in your .bashrc.

PS1='\[\e[1;34m\]\w\$ \[\e[0;32m\]'
trap 'printf \\e[0m' DEBUG

The DEBUG trap, as suggested by Chris Down, is used to reset the text attributes. It's meant for debugging, but it's also useful to execute code before executing the command typed by the user.

In the prompt setting:
  • \[…\] is used around escape sequences to tell bash that they don't use up any screen real estate (without them, the screen would become garbled when bash needs to redraw the prompt).
  • \e[…m where … is a number, or more generally a sequence of numbers separated by semicolons, is an escape sequence sent to the terminal to affect the color and other attributes of the following text. For example, 1;34 sets bold blue; 0;32 resets all attributes then switches the color to green. Adjust to taste.
  • \w\$ print the current directory and $ or #. You can of course change this to whatever you like, and insert text attribute changes in between if you like.
  • There's a final attribute change at the end, which sets the color of the text input.
I have to experiment with that and think about all this, more complex than I anticipated. As you can see: you can know about bash and shell and script-coding and all that jazz for a while, and even after years you learn something new and it blows you away (in a good way) like it had blown you away when you started the shell-journey as noob decades ago. :good:
Cheers!
Yours Rava

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

Ravas coding goodies

Post#214 by Ed_P » 03 Nov 2023, 14:06

Rava wrote:
01 Nov 2023, 22:10
I presume you do not know what the program "tail" does?
I do but I wasn't aware it defaulted to 10 lines. I always use it as tail -n 1 or tail -n 2. :)
Ed

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

Ravas coding goodies

Post#215 by Rava » 03 Nov 2023, 14:09

Ed_P wrote:
03 Nov 2023, 14:06
I do but I wasn't aware it defaulted to 10 lines.
Okay then, so I presume you alter your perception of your
I can assure you I don't see 860 segments listed
line.
Cheers!
Yours Rava

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

Ravas coding goodies

Post#216 by Ed_P » 03 Nov 2023, 14:21

Rava wrote:
03 Nov 2023, 11:38
Have some code snippets from some answers on there:
This works also.

Code: Select all

#!/bin/bash
# https://techstop.github.io/bash-script-colors/
# Ansi color code variables
red="\e[0;91m"
blue="\e[0;94m"
expand_bg="\e[K"
blue_bg="\e[0;104m${expand_bg}"
red_bg="\e[0;101m${expand_bg}"
green_bg="\e[0;102m${expand_bg}"
green="\e[0;92m"
white="\e[0;97m"
bold="\e[1m"
uline="\e[4m"
reset="\e[0m"

# horizontally expanded backgrounds
echo -e "${blue_bg}${reset}"
echo -e "${red_bg}${reset}"
echo -e "${green_bg}${reset}"

echo ""

# colored text
echo -e "${red}Hello World!${reset}"
echo -e "${blue}Hello World!${reset}"
echo -e "${green}Hello World!${reset}"
echo -e "${white}Hello World!${reset}"

echo ""

# bold colored text
echo -e "${red}${bold}Hello World!${reset}"
echo -e "${blue}${bold}Hello World!${reset}"
echo -e "${green}${bold}Hello World!${reset}"
echo -e "${white}${bold}Hello World!${reset}"

echo ""

# underlined colored text
echo -e "${red}${uline}Hello World!${reset}"
echo -e "${blue}${uline}Hello World!${reset}"
echo -e "${green}${uline}Hello World!${reset}"
echo -e "${white}${uline}Hello World!${reset}"

echo ""

# ansi across multiple lines
echo -e "${green}This is a sentence across"
echo "three lines to show how an ansi color"
echo -e "works across multiple lines with echo.${reset}"

echo ""

# combining ansi in one line
echo -e "${red}This sentence ${green}displays ${blue}ansi code used in ${white}${bold}combination.${reset}"
Added in 3 minutes 15 seconds:
Rava wrote:
03 Nov 2023, 14:09
Okay then, so I presume you alter your perception of your
"I can assure you I don't see 860 segments listed"
line.
No, I still only see 10 lines. ;)
Ed

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

Ravas coding goodies

Post#217 by Rava » 03 Nov 2023, 15:57

Ed_P wrote:
03 Nov 2023, 14:24
This works also.
There us an even better method than this, because this method only works for echo -e and not for cat <<EOF

You can use true escape sequences but these cannot be put into a [ code ] snippet.
You can use these true escape sequences with echo without -e
You can also use them with cat, e.g. displaying a local document a la

Code: Select all

#!/bin/sh
cat <<EOF|fold -sw $(tput cols)
Lorem ipsum dolor sit amet...
EOF
(e.g. a help text of your script) switching colours and bold on and off to yours hearts content.

Added in 3 minutes 35 seconds:
Example:
Image

Added in 3 minutes 54 seconds:
Also called ANSI escape code
ANSI escape sequences are a standard for in-band signaling to control cursor location, color, font styling, and other options on video text terminals and terminal emulators. Certain sequences of bytes, most starting with an ASCII escape character and a bracket character, are embedded into text. The terminal interprets these sequences as commands, rather than text to display verbatim.
Example from that wikipedia page:
Image
Output of the system-monitor htop, an ncurses-application (which uses SGR and other ANSI/ISO control sequences).
Cheers!
Yours Rava

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

Ravas coding goodies

Post#218 by Ed_P » 03 Nov 2023, 21:07

Lots of old code.

Code: Select all

function Title() {
  echo -en "\033]0;$1\a";
}
Title "Flash Games"

# Colors                          # ignisdownloader.sh
WHITE="\e[1;37m"                  # https://techstop.github.io/bash-script-colors/
CYAN="\e[1;96m"                   # Regular   - \e[0;**m
PURPLE="\e[1;35m"                 # Bold      - \e[1;**m
BLUE="\e[1;94m"                   # Underline - \e[4;**m
YELLOW="\e[1;93m"                 # High Intensity - \e[0;9*m
GREEN="\e[1;32m"
RED="\e[1;31m"
BLACK="\e[1;30m"
BOLD="\e[1m"
CLR="\e[0m"

# Color definitions               # ups.sh
txtbld=$(tput bold)               # Bold
txtred=${txtbld}$(tput setaf 1)   # Bold Red
rst=$(tput sgr0)                  # Reset
function redpswd() {
  echo -E "$1" $txtred  
}
function Bold() {
  echo -e $txtbld"$1"$rst "$2"; 
}

if [ `whoami` != "root" ]; then
   redpswd "Enter root's password"
   su -c "sh $0 $1 $2"
   exit
fi
echo $rst


if [ `whoami` != "root" ]; then
   echo "Enter root's password" $(tput bold)$(tput setaf 2)  # 1-red, 2-green, 6-cyan, 7-white
   su -c "sh $0 $1 $2"; exit
fi
echo $(tput sgr0) 



# colors: https://techstop.github.io/bash-script-colors/  &  https://ibb.co/pRzvrqV 



Very old code but it works: Just made this script: UPS (Post by normalGuy #49193)
Ed

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

Ravas coding goodies

Post#219 by Rava » 05 Nov 2023, 17:59

Finally I added functionality to one of my scripts:
make-ffplay-script is now V3.5.1

What did I add? When a parameter is given, that is taken as a sleep parameter and the script sleeps for the given time before it starts playing the multimedia files; the reason for that is so that one can switch to a different desktop while having the terminal that started the ffplay script can stay at the original desktop.

Cave! I was too lazy to think of any sanity check, and also too lazy to implement a -h / --help option explaining the sleep functionality.

Have the code:

Code: Select all

#!/bin/sh
MYVERSION=V3.51 # please add V at the beginning - newest info at bottom!
# V3.4 - $1 is the relative folder with the files, will be stripped of any "/" - e.g. Filme or Filme/ works but not /mnt/sda1/video/Filme nor /mnt/sda1/video/Filme/
# for now supported file endings are
# avi 	flv	mp3	mp4	mpg	mkv	ogv	TS	ts	webm	divx 	VOB	vob 	ogg 	aac
# V3.4: now added "chmod -v a+x "$PARAMETER".sh - will create error message on NTFS or VFAT fs, necessary for Linux fs like ext2,ext3 etcetera.
# V3.5: now uses ffplay-hide_banner script instead of using ffplay itself. ffplay-hide_banner distinguishes between video and media-file that is sound only. In the latter case the following is added: -showmode waves -x 500 -y 150 
# V3.46 changed into using script ffplay-hide_banner-fs (start in -fs aka full-screen mode for videos - nothing changed for audio-only-files) instead of script ffplay-hide_banner-
# V3.47 back to direct call of ffplay via function
# V3.48 using "double quotes" because of consecutive whitespaces in filename bug
# V3.49 added .ogg file format
# V3.50 added .aac file format
# V3.51 added sleep paramerter; for now no sanity check!
echo make-ffplay-script $MYVERSION

if [ $# -ne 1 ]; then
	echo 'No parameter given. Need one parameter - the relative Folder e.g. "folder/".
Abort.'
	exit 2
fi

PARAMETER=$(echo "$1"|sed 's|/||g')
if [ -d "$PARAMETER" ]; then
	echo working on "$PARAMETER"… writing into "$PARAMETER".sh
	echo '#!/bin/sh
echo "$0"' >"$PARAMETER".sh
echo '# made by make-ffplay-script '$MYVERSION '- blame Rava' >>"$PARAMETER".sh
echo 'if [ $# -eq 1 ]; then
	# XXX for now NO SANITY CHECK ! XXX
	echo sleep $1
	sleep $1
fi

function myffplay {
ffprobe -hide_banner 2>&1 "$*" |grep Stream|grep "Video:" >/dev/null
if [ $? -eq 0 ]; then
	# return zero -> video file: default
	ffplay -autoexit -hide_banner -fs -i "$*"
else
	# return non-zero -> audio only - adjusting…
	ffplay -autoexit -hide_banner -showmode waves -x 500 -y 150 -i "$*"
fi
}' >>"$PARAMETER".sh
	find "$PARAMETER" |grep -E "\.avi$|\.flv$|\.mp[34g]$|\.mkv$|\.ogv$|\.[tT][sS]$|\.webm$|\.divx$|\.[vV][oO][bB]$|\.ogg$|\.aac$" |sort | while read line ; do
{
 echo myffplay \""${line}\"" >>"$PARAMETER".sh
} ; done
echo >>"$PARAMETER".sh
	chmod -v a+x "$PARAMETER".sh
else
	echo Given parameter "$PARAMETER" is not a directory. Abort.
	exit 1
fi
Cheers!
Yours Rava

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

Ravas coding goodies

Post#220 by Rava » 19 Nov 2023, 19:44

Today I found this:

https://www.pement.org/sed/sed1line.txt
Useful one-line scripts for sed (Unix stream editor) Dec. 29, 2005 - version 5.5
Compiled by Eric Pement
Hope some find it useful.
Cheers!
Yours Rava

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

Ravas coding goodies

Post#221 by Ed_P » 19 Nov 2023, 21:14

Rava wrote:
19 Nov 2023, 19:44
Hope some find it useful.
:shock: Very useful. :happy62: Thank you Rava. :beer:
Ed

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

Ravas coding goodies

Post#222 by Rava » 19 Nov 2023, 21:42

^
You are welcome.

And a bit of POSIX / Linux humour: found this gem in man find (1):
A `%' at the end of
the format argument causes undefined behaviour since there is no
following character. In some locales, it may hide your door
keys, while in others it may remove the final page from the
novel you are reading.
(highlighting by me)
:D
Cheers!
Yours Rava

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

Ravas coding goodies

Post#223 by Rava » 30 Nov 2023, 04:27

Lazy me just quoting myself once more. :D
Rava wrote:
30 Nov 2023, 04:25
Vic wrote:
29 Nov 2023, 23:17
I could not figure out how to make file=%f; work in the script
This is a syntax the .desktop files use.

Scripts use parameters instead.

Have some initial help on that.

https://linuxhandbook.com/bash-arguments/
Bash Beginner Tutorials
Passing Arguments to Bash Scripts

In the third part of the Bash Beginner Series, you'll learn to pass arguments to a bash shell script. You'll also learn about special bash shell variables.

Arguments can be useful, especially with Bash!

So far, you have learned how to use variables to make your bash scripts dynamic and generic, so it is responsive to various data and different user input.

In this tutorial, you will learn how you can pass variables to a bash scripts from the command line.
[…]
https://www.baeldung.com/linux/pass-com ... ash-script
1. Overview

Linux has a rich and powerful set of ways to supply parameters to bash scripts.

In this tutorial, we’ll look at a few ways of doing this.
2. Argument List

Arguments can be passed to a bash script during the time of its execution, as a list, separated by space following the script filename. This comes in handy when a script has to perform different functions depending on the values of the input.

For instance, let’s pass a couple of parameters to our script start.sh:

sh start.sh development 100
[…]
https://www.baeldung.com/linux/use-comm ... ash-script
1. Introduction

We’ve previously examined how to pass command-line arguments to a bash script. In this tutorial, we’ll take a look at how we can use these arguments inside the bash script.
[…]
And much more via this search:
https://duckduckgo.com/?q=linux+shell+s ... ter&ia=web
Cheers!
Yours Rava

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

Ravas coding goodies

Post#224 by Rava » 01 Jan 2024, 19:42

Once more lazy me just quotes myself. :)
Rava wrote:
01 Jan 2024, 19:39
porteux wrote:
01 Jan 2024, 13:34
It's still unclear what exactly you're trying to achieve.

If I do this it just works, regardless if bash.sh is already present or not:

Code: Select all

mousepad ~/bash.sh
What might be an issue is that by default a file in a POSIX filesystem will not be executable.
Either do the needed change via your DE's file browser, or via the terminal. I will do all in the terminal, file creation and file mode change.
Example: using the shell to create a sample ~/bash.sh

Code: Select all

guest@rava:~$ echo '#!/bin/sh
> echo "Hello world!"' >~/bash.sh
guest@rava:~$ cat ~/bash.sh
#!/bin/sh
echo "Hello world!"
guest@rava:~$ ./bash.sh
bash: ./bash.sh: Permission denied
Why "Permission denied" - because ~/bash.sh is not executable:

Code: Select all

guest@rava:~$ ls -l ~/bash.sh
-rw-r--r-- 1 guest users 30 2024-01-01 20:21 /home/guest/bash.sh
Using chmod to change that:

Code: Select all

guest@rava:~$ chmod u+x ~/bash.sh
guest@rava:~$ ls -l ~/bash.sh
-rwxr--r-- 1 guest users 30 2024-01-01 20:21 /home/guest/bash.sh
guest@rava:~$ ~/bash.sh
Hello world!
guest@rava:~$ ./bash.sh
Hello world!
guest@rava:~$ 
:magic:

Added in 4 minutes 14 seconds:
Cave! the users specified by chmod might be confusing.
u=User (the owner of the file)
o=NOT owner but "Others"
g=Group
a=All

See either chmod --help or man chmod for more details.

Added in 10 minutes 59 seconds:
One other small detail: you can still execute a script even when it is not executable like so:

Code: Select all

guest@rava:~$ chmod a-x bash.sh 
guest@rava:~$ ls -l bash.sh 
-rw-r--r-- 1 guest users 30 2024-01-01 20:21 bash.sh
guest@rava:~$ . bash.sh 
Hello world!
but there is a huge CAVE! when doing it like so: when there is one or more explicit exit commands in the script (e.g. to signal an error occurred so that the exit status is non zero, e.g. via

Code: Select all

exit 1
the starting bash itself will exit since you not started a new one for that script, but you executed the script in your current bash. So when that script encounters any exit command the bash itself will quit instead. That is often not what you wanted especially when the script would explain the issue prior the exit command. Today's CPUs and GPUs are too powerful and quick for you to even see any characters of the error message.
Cheers!
Yours Rava

donald
Full of knowledge
Full of knowledge
Posts: 2073
Joined: 17 Jun 2013, 13:17
Distribution: Porteus 3.2.2 XFCE 32bit
Location: Germany

Ravas coding goodies

Post#225 by donald » 02 Jan 2024, 06:47

Use chmod like a Pro.. :)
Look at the Image.
Image
Whatever you want to set, simply add up the numbers
and use chmod <number> <file>

Example:
chmod 755 = -rwxr-xr-x
chmod 644 = -rw-r--r--
chmod 777 = -rwxrwxrwx

and so on.

Post Reply