Copy all folders that start with a capital letter
What would the command be to copy all folders and their contents in the current directory that start with a capital letter to another folder?
I've looked, but haven't seen a clear example that answers this question.
For example, if the current directory contains the following folders:
Blue
greEn
Red
The folders that start with a capital would be copied to a folder called colors.
https://redd.it/16ya63s
@r_bash
What would the command be to copy all folders and their contents in the current directory that start with a capital letter to another folder?
I've looked, but haven't seen a clear example that answers this question.
For example, if the current directory contains the following folders:
Blue
greEn
Red
The folders that start with a capital would be copied to a folder called colors.
https://redd.it/16ya63s
@r_bash
Reddit
From the bash community on Reddit
Explore this post and more from the bash community
bash code to waste bandwidth
so i don't know if this belongs here, but i've been trying some code with gpt to try and waste bandwidth, the best we came up with was this:
while true; do
sudo arp-scan --localnet | grep -oE '(0-9{1,3}\.){3}0-9{1,3}' | xargs -I % sudo ping -f %
done
but it didn't quite do much. it slowed down the entire network a small bit but that's all. anyone got code that could help me waste bandwidth? in bash, so mostly simple like the one i put here
https://redd.it/16ydtpq
@r_bash
so i don't know if this belongs here, but i've been trying some code with gpt to try and waste bandwidth, the best we came up with was this:
while true; do
sudo arp-scan --localnet | grep -oE '(0-9{1,3}\.){3}0-9{1,3}' | xargs -I % sudo ping -f %
done
but it didn't quite do much. it slowed down the entire network a small bit but that's all. anyone got code that could help me waste bandwidth? in bash, so mostly simple like the one i put here
https://redd.it/16ydtpq
@r_bash
Reddit
From the bash community on Reddit
Explore this post and more from the bash community
sftp not working when triggered by cron
I have this noscript and it works fine when I run it by myself, but when it's triggered by cron everything works except copying files with sftp. It copies only a few random files from the list. Has anyone had such a problem? It worked fine with scp in the past but then scp started to break the connection when the files grew above 2GBs and I couldn't find the reason.
Thanks in advance
FILES=$(find /var/log/CPbackup/backups/ -name 'backup*' -mtime -1)
for file in $FILES
do
echo $file >> /var/log/CPbackup/backupcopy.log
echo "put $file /APPTEST-APP/" | sshpass -p 'password' sftp -vvv backup@172.1.1.1 2>&1 /var/log/CPbackup/backupcopy.log
echo "==========================================================" >> /var/log/CPbackup/backupcopy.log
done
​
https://redd.it/170ffpt
@r_bash
I have this noscript and it works fine when I run it by myself, but when it's triggered by cron everything works except copying files with sftp. It copies only a few random files from the list. Has anyone had such a problem? It worked fine with scp in the past but then scp started to break the connection when the files grew above 2GBs and I couldn't find the reason.
Thanks in advance
FILES=$(find /var/log/CPbackup/backups/ -name 'backup*' -mtime -1)
for file in $FILES
do
echo $file >> /var/log/CPbackup/backupcopy.log
echo "put $file /APPTEST-APP/" | sshpass -p 'password' sftp -vvv backup@172.1.1.1 2>&1 /var/log/CPbackup/backupcopy.log
echo "==========================================================" >> /var/log/CPbackup/backupcopy.log
done
​
https://redd.it/170ffpt
@r_bash
Reddit
From the bash community on Reddit
Explore this post and more from the bash community
Better control over the text in a terminal emulator's noscript bar
So a lot of us use some escape characters in
But if I run the command in a new terminal window, the noscript bar text is indeed 'sleep'
Other programs change the noscript bar even in my current window (for example, some ROS programs).
Anyway, does anyone know if there's a way to prevent programs from changing the noscript bar text? In another thread, someone said that these programs are changing the text through the same escape characters that are used by
If anyone has ideas here, or even just knows where to start looking, I'd appreciate it.
https://redd.it/1710fqs
@r_bash
So a lot of us use some escape characters in
PS1 to set the terminal emulator's noscript text whenever there's a new prompt. I've been customizing this and using the resulting text for various purposes. However, frustratingly, this is not the only thing that affects the text. When certain commands are run in the bash session, they also cause the noscript text to change. To be honest, I don't have a great understanding of what does or doesn't cause the text to change. For example, this does not:sleep 5But if I run the command in a new terminal window, the noscript bar text is indeed 'sleep'
kitty -- sleep 5Other programs change the noscript bar even in my current window (for example, some ROS programs).
Anyway, does anyone know if there's a way to prevent programs from changing the noscript bar text? In another thread, someone said that these programs are changing the text through the same escape characters that are used by
PS1. This led me to wonder if it's possible to change the escape characters for writing text to the noscript bar. It seems likely this would be possible, but I don't know if it's a change in bash or readline, or maybe a change in the source code of your terminal emulator (I'm using kitty, obviously).If anyone has ideas here, or even just knows where to start looking, I'd appreciate it.
https://redd.it/1710fqs
@r_bash
Reddit
From the bash community on Reddit
Explore this post and more from the bash community
I build a tool to supercharge bash noscripts running
https://github.com/docsion/rfsh
https://redd.it/1710pc8
@r_bash
https://github.com/docsion/rfsh
https://redd.it/1710pc8
@r_bash
GitHub
GitHub - docsion/rfsh: RFSH: Run shell noscripts in batch, concurrently, fully customized with variable .
RFSH: Run shell noscripts in batch, concurrently, fully customized with variable . - docsion/rfsh
Has anyone else overthought their command line and shell setups?
I've written some posts where I've put a lot of thoughts into individual things like which shell or prompts. I'm a quite a perfectionist, and even though I could just do fine with whatever shell and terminal an OS comes with, there are lots of features and nice stuff you can add, and they take time to learn and setup. If I commit to using one software whether its a shell, a CLI, or prompt, I want to be sure that its the one I actually want to use more than others and don't want to sink time into one just to find out I would prefer another.
Should I have a posix compatible shell like Bash or Zsh, or would I benefit from a non conforming one like Fish, at least as an interactive shell? Should I use a prompt with more features like Oh My Posh, or one that's easier to setup like Starship? Starship uses toml while Oh My Posh uses JSON or JSON-like formatting.
https://redd.it/171035o
@r_bash
I've written some posts where I've put a lot of thoughts into individual things like which shell or prompts. I'm a quite a perfectionist, and even though I could just do fine with whatever shell and terminal an OS comes with, there are lots of features and nice stuff you can add, and they take time to learn and setup. If I commit to using one software whether its a shell, a CLI, or prompt, I want to be sure that its the one I actually want to use more than others and don't want to sink time into one just to find out I would prefer another.
Should I have a posix compatible shell like Bash or Zsh, or would I benefit from a non conforming one like Fish, at least as an interactive shell? Should I use a prompt with more features like Oh My Posh, or one that's easier to setup like Starship? Starship uses toml while Oh My Posh uses JSON or JSON-like formatting.
https://redd.it/171035o
@r_bash
Reddit
From the bash community on Reddit
Explore this post and more from the bash community
Hi, I'm sharing ydf, a disruptive dotfiles manager+
Avoid repetitive work and errors, focus on what matter
Be ready to work in just a few minutes on your Fresh OS
Declare your working environment and Automate its configuration
New member on the team?, reproduce your colleague working environment and start working now
***https://github.com/yunielrc/ydf***
https://redd.it/171ehhd
@r_bash
Avoid repetitive work and errors, focus on what matter
Be ready to work in just a few minutes on your Fresh OS
Declare your working environment and Automate its configuration
New member on the team?, reproduce your colleague working environment and start working now
***https://github.com/yunielrc/ydf***
https://redd.it/171ehhd
@r_bash
GitHub
GitHub - yunielrc/ydf: A dotfiles manager+. Be ready to work in just a few minutes on your Fresh OS
A dotfiles manager+. Be ready to work in just a few minutes on your Fresh OS - yunielrc/ydf
AND OR comparison in IF
Hey everyone,
Breaking my head over this one and cannot seem to get it to work apart from a long || [ $var = 0\] || [$var =1\] .. etc string. I cannot get my comparison right! Is there anyone willing to give me a pointer?
I am trying to check of $var is either "index" OR 0,1,2,3,4,5,6,7,8,9,10.
Code:
#!/bin/bash
var="${1:-index}"
if [ $var = "index" \] || [ $var -ge 0 && $var -ls 11 \]; then <------- problem line
\# some code, var is either "index" or 1...10
else
\# some code, var is any other value
fi
https://redd.it/171krob
@r_bash
Hey everyone,
Breaking my head over this one and cannot seem to get it to work apart from a long || [ $var = 0\] || [$var =1\] .. etc string. I cannot get my comparison right! Is there anyone willing to give me a pointer?
I am trying to check of $var is either "index" OR 0,1,2,3,4,5,6,7,8,9,10.
Code:
#!/bin/bash
var="${1:-index}"
if [ $var = "index" \] || [ $var -ge 0 && $var -ls 11 \]; then <------- problem line
\# some code, var is either "index" or 1...10
else
\# some code, var is any other value
fi
https://redd.it/171krob
@r_bash
Reddit
From the bash community on Reddit
Explore this post and more from the bash community
Update noscript
Hi guys, one of my clients want a noscript to automatically update one executable, i've made basically everything when it comes to get the file and make the appropriate BUT the update check part xD
So, this is the noscript:
rm "/home/user/Appimages/App.AppImage"
curl "client website" --output "/home/user/Appimages/App.7z"
7z x "/home/user/Appimages/App.7z"
mv "/home/user/Appimages/App/App.AppImage" "/home/user/Appimages/App.AppImage"
rm "/home/user/Appimages/App.7z"
rm -r "/home/user/Appimages/App"
It works, but 2 things are missing, first i am using this command here:
testvariable=$(curl -s "$updatewebsite" | grep 'Last Update')
Basically it's returning me this line:
<h2>Last Update (v1.20.0)</h2>
I want grep to specifically get the version number, like "v.1.20.0", save to a file in the directory IF there is no version.txt file there, if there is a file, grab the number, check if it is higher and then execute the command above to update the appimage, can you guys help?
https://redd.it/171q8zd
@r_bash
Hi guys, one of my clients want a noscript to automatically update one executable, i've made basically everything when it comes to get the file and make the appropriate BUT the update check part xD
So, this is the noscript:
rm "/home/user/Appimages/App.AppImage"
curl "client website" --output "/home/user/Appimages/App.7z"
7z x "/home/user/Appimages/App.7z"
mv "/home/user/Appimages/App/App.AppImage" "/home/user/Appimages/App.AppImage"
rm "/home/user/Appimages/App.7z"
rm -r "/home/user/Appimages/App"
It works, but 2 things are missing, first i am using this command here:
testvariable=$(curl -s "$updatewebsite" | grep 'Last Update')
Basically it's returning me this line:
<h2>Last Update (v1.20.0)</h2>
I want grep to specifically get the version number, like "v.1.20.0", save to a file in the directory IF there is no version.txt file there, if there is a file, grab the number, check if it is higher and then execute the command above to update the appimage, can you guys help?
https://redd.it/171q8zd
@r_bash
Reddit
From the bash community on Reddit
Explore this post and more from the bash community
Escape for Conky noscript
I am writing up a small bash noscript so that I can do a quick-configure.
It's pretty much a noscript I can change and run the code to just auto inject. And it's only one file instead of two.
The issue is, I am adding it to a conf file using EOF, and things need escaped in order for it to inject properly.
I'm having issues escaping the line with
and
I've been going through Google reading on how to escape each letter, including dashes, which recommends to add two dashes in front
So the question is, how the hell do I escape that line properly so that it can be used inside
I also read about
https://redd.it/171ql74
@r_bash
I am writing up a small bash noscript so that I can do a quick-configure.
It's pretty much a noscript I can change and run the code to just auto inject. And it's only one file instead of two.
The issue is, I am adding it to a conf file using EOF, and things need escaped in order for it to inject properly.
conky.text = [[
\${color1}\${font ConkySymbols:size=20}t\${font} \${voffset -10}GNU/Linux» \$hr \${color}
\${color1}\${goto 35}OS : \${color}\${execi 86400 cat `ls -atr /etc/*-release | tail -2` | grep "PRETTY_NAME" | cut -d= -f2 | sed 's/"//g'}
\${color1}\${goto 35}Kernel : \${color}\$kernel on \$machine
I'm having issues escaping the line with
`ls -atr /etc/*-release | tail -2`
and
cut -d= -f2 | sed 's/"//g'
I've been going through Google reading on how to escape each letter, including dashes, which recommends to add two dashes in front
--, however, the noscript errors out with these:cat: '`ls': No such file or directory
cat: -atr: No such file or directory
tail: cannot open '-2`' for reading: No such file or directory
So the question is, how the hell do I escape that line properly so that it can be used inside
sudo tee "${config_folder}/${config_file}" >/dev/null <<EOF
Code Here
}
I also read about
<<'EOD' but came to read that it shows the text exactly as it is written, and I can't add variables inside. And I need the noscript to accept a few variables that will be added to the noscript when the noscript is ran.https://redd.it/171ql74
@r_bash
Reddit
From the bash community on Reddit
Explore this post and more from the bash community
A telegram bot to backup files and directories and send them as tar archieves to yourself
https://github.com/wolandark/TSBB
https://redd.it/171wt9h
@r_bash
https://github.com/wolandark/TSBB
https://redd.it/171wt9h
@r_bash
GitHub
GitHub - wolandark/TSBB: Telegram Server Backup Bot Written in BASH
Telegram Server Backup Bot Written in BASH. Contribute to wolandark/TSBB development by creating an account on GitHub.
If you wanted to explain to a new linux user why they need to learn bash, how would you do it?
how would you explain to a new linux user why they need to learn bash and the command line interface? what would you tell them to make them understand how important bash is to getting the most out of their linux distro?
what specific reason would you give them?
thank you
https://redd.it/1727jlo
@r_bash
how would you explain to a new linux user why they need to learn bash and the command line interface? what would you tell them to make them understand how important bash is to getting the most out of their linux distro?
what specific reason would you give them?
thank you
https://redd.it/1727jlo
@r_bash
Reddit
From the bash community on Reddit
Explore this post and more from the bash community
If there some general bash mechanism to determine if `read` / `mapfile -n` stopped due to hitting a delimiter or due to hitting an EOF?
**THE PROBLEM**
So, this is definitely a niche edge-case, but I'm working on a code that requires knowing if `mapfile -n $N A` returned because it hit a delimiter $N times or because it hit an EOF. Without knowing this it will result in reading partial lines a small but non-negligible percent of the time (something like "1 in 1000" to "1 in 10000" lines gets a partial read).
TL;DR: I have a way to figure this out by using `mapfile` (without the `-t`) so newlines are kept and then checking if the last char of the last element is a newline, then filtering out all the trailing newlines, but I want something more efficient. Any ideas?
***
**WHY I AM GETTING OCCASIONAL PARTIAL READS**
The "why" is complicated, but more or less comes down to because there are multiple independent processes doing i/o on the same file (note: file is on a tmpfs ramdisk):
* process 1 is appending data to the end of the file using write file denoscriptor `fd1`
* processes 2a, 2b, ... , 2n are sequentially (one ast a time) reading N lines at a time from the file using a shared read file denoscriptor `fd2`. after reading data they send process 3 how many lines they just read via a pipe.
* process 3 keeps track of how many lines all processes have read and, on occasion, deletes already-read data from the start of the file using `sed -i '1,#d` $file`
So at any given time, there might be 3 separate processes using the file to 1) add data to the end, 2) delete data from the start, and 3) read data from the middle.
With this setup, it is possible for the "read data from the middle process" to catch up with the "append data to the end" process. When this happens it technically hits what is the end of the file at that particular instant and returns a partial read. By the time the next read process starts reading the end of the file has been extended further out and it still has data to read. This happening is rare (1 every few thousand lines) but it definitely happens.
***
**CURRENT SOLUTION**
Checking how many elements `A` *almost* lets me figure this out, but unfortunately fails in the situation where the EOF was encountered in the middle of the last line mapfile was going to read anyways. In this situation, the last element in `A` is a partial read even though `A` has the correct number of elements.
I do have a seemingly-working workaround, but its a problem that seems like it should have a better and more efficient solution. My workaround basically involves replacing
mapfile -t A
some_func "${A[@]}"
with
mapfile A
[[ ${#A[@]} == 0 ]] || [[ "${A[-1]: -1}" == $'\n' ]] || {
read -r
A[$(( ${#A[@]} - 1 ))]+="$REPLY"
}
some_func "${A[@]%$'\n'}"
This makes `mapfile` keep the trailing newlines, checks if the last char in the last element of A is a newline (or if A is empty all altogether), and if this is not the case uses `read` to read the rest of the line and append it to the last element in `A`. When using `A` the trailing newlines get removed via `${A[@]%$'\n'}`.
That said, unnecessarily adding in and then having to remove the trailing newlines on *every* line read so that I can catch a 1 in ~5000 chance of a partial read is not exactly efficient, and does have a measurable impact on the code's execution speed (something like a 5%-20% slowdown, depending on specifics)
***
**WHAT IT IS FOR / WHY BOTHER**
The code this is part of is called `mySplit` and is hosted on github. [LINK TO CODE](https://github.com/jkool702/forkrun/blob/main/mySplit.bash). It is a work-in-progress rewrite of my `forkrun` utility that uses bash coprocs to parallelize for-loops in the same manner that `parallel -m` and `xargs -P <#>` do.
`xargs -P $(nproc) -d $'\n'` is the fastest existing method that I know of to parallelize shell loops (`parallel -m` is, by comparison, typically 2-3x slower). That said, if anyone knows of something faster let me know.
On problems where the efficiency of
**THE PROBLEM**
So, this is definitely a niche edge-case, but I'm working on a code that requires knowing if `mapfile -n $N A` returned because it hit a delimiter $N times or because it hit an EOF. Without knowing this it will result in reading partial lines a small but non-negligible percent of the time (something like "1 in 1000" to "1 in 10000" lines gets a partial read).
TL;DR: I have a way to figure this out by using `mapfile` (without the `-t`) so newlines are kept and then checking if the last char of the last element is a newline, then filtering out all the trailing newlines, but I want something more efficient. Any ideas?
***
**WHY I AM GETTING OCCASIONAL PARTIAL READS**
The "why" is complicated, but more or less comes down to because there are multiple independent processes doing i/o on the same file (note: file is on a tmpfs ramdisk):
* process 1 is appending data to the end of the file using write file denoscriptor `fd1`
* processes 2a, 2b, ... , 2n are sequentially (one ast a time) reading N lines at a time from the file using a shared read file denoscriptor `fd2`. after reading data they send process 3 how many lines they just read via a pipe.
* process 3 keeps track of how many lines all processes have read and, on occasion, deletes already-read data from the start of the file using `sed -i '1,#d` $file`
So at any given time, there might be 3 separate processes using the file to 1) add data to the end, 2) delete data from the start, and 3) read data from the middle.
With this setup, it is possible for the "read data from the middle process" to catch up with the "append data to the end" process. When this happens it technically hits what is the end of the file at that particular instant and returns a partial read. By the time the next read process starts reading the end of the file has been extended further out and it still has data to read. This happening is rare (1 every few thousand lines) but it definitely happens.
***
**CURRENT SOLUTION**
Checking how many elements `A` *almost* lets me figure this out, but unfortunately fails in the situation where the EOF was encountered in the middle of the last line mapfile was going to read anyways. In this situation, the last element in `A` is a partial read even though `A` has the correct number of elements.
I do have a seemingly-working workaround, but its a problem that seems like it should have a better and more efficient solution. My workaround basically involves replacing
mapfile -t A
some_func "${A[@]}"
with
mapfile A
[[ ${#A[@]} == 0 ]] || [[ "${A[-1]: -1}" == $'\n' ]] || {
read -r
A[$(( ${#A[@]} - 1 ))]+="$REPLY"
}
some_func "${A[@]%$'\n'}"
This makes `mapfile` keep the trailing newlines, checks if the last char in the last element of A is a newline (or if A is empty all altogether), and if this is not the case uses `read` to read the rest of the line and append it to the last element in `A`. When using `A` the trailing newlines get removed via `${A[@]%$'\n'}`.
That said, unnecessarily adding in and then having to remove the trailing newlines on *every* line read so that I can catch a 1 in ~5000 chance of a partial read is not exactly efficient, and does have a measurable impact on the code's execution speed (something like a 5%-20% slowdown, depending on specifics)
***
**WHAT IT IS FOR / WHY BOTHER**
The code this is part of is called `mySplit` and is hosted on github. [LINK TO CODE](https://github.com/jkool702/forkrun/blob/main/mySplit.bash). It is a work-in-progress rewrite of my `forkrun` utility that uses bash coprocs to parallelize for-loops in the same manner that `parallel -m` and `xargs -P <#>` do.
`xargs -P $(nproc) -d $'\n'` is the fastest existing method that I know of to parallelize shell loops (`parallel -m` is, by comparison, typically 2-3x slower). That said, if anyone knows of something faster let me know.
On problems where the efficiency of
GitHub
forkrun/mySplit.bash at main · jkool702/forkrun
runs multiple inputs through a noscript/function in parallel using bash coprocs - jkool702/forkrun
the parallelization framework matters (i.e., many very quick iterations....things like checksumming a few hundred thousand files that are all a few kb or less), `mySplit` is (in terms of "real" wall-clock time) unambiguously faster than `xargs` and blows away `parallel`.
In a [speedtest](https://github.com/jkool702/forkrun/blob/main/mySplit.speedtest.bash) comparing these 3 codes for computing 11 different checksums of just under 500,000 small files (everything under `/usr`) copied to a tmpfs ramdisk (so i/o wouldnt skew results), `xargs` took between 31% and 83% longer than `mySplit`, and on average took 62% longer. `parallel` took between 2.5x and 5.8x as long as `mySplit`, and on average took 4.4x as long.
Needless to say, getting a pure-bash^(*) function to parallelize loops faster than the fastest compiled C binaries (like `xargs`) required a LOT of time spent optimizing the process. As such, at this point, gaining potentially 5-20% speedup is a HUGE speedup.
^(*)well, almost pure bash. It depends on `cat`, `sed` and `grep`, but for each of these the busybox or GNU versions will work, and it is hard to imagine there being many machines out there with bash (and a recent version of bash...at minimum bash 4.0) that dont have at least busybox versions of `cat` `sed` and `grep`...
https://redd.it/1728cme
@r_bash
In a [speedtest](https://github.com/jkool702/forkrun/blob/main/mySplit.speedtest.bash) comparing these 3 codes for computing 11 different checksums of just under 500,000 small files (everything under `/usr`) copied to a tmpfs ramdisk (so i/o wouldnt skew results), `xargs` took between 31% and 83% longer than `mySplit`, and on average took 62% longer. `parallel` took between 2.5x and 5.8x as long as `mySplit`, and on average took 4.4x as long.
Needless to say, getting a pure-bash^(*) function to parallelize loops faster than the fastest compiled C binaries (like `xargs`) required a LOT of time spent optimizing the process. As such, at this point, gaining potentially 5-20% speedup is a HUGE speedup.
^(*)well, almost pure bash. It depends on `cat`, `sed` and `grep`, but for each of these the busybox or GNU versions will work, and it is hard to imagine there being many machines out there with bash (and a recent version of bash...at minimum bash 4.0) that dont have at least busybox versions of `cat` `sed` and `grep`...
https://redd.it/1728cme
@r_bash
GitHub
forkrun/mySplit.speedtest.bash at main · jkool702/forkrun
runs multiple inputs through a noscript/function in parallel using bash coprocs - jkool702/forkrun
Declare your working environment and automate its configuration on Linux with bash
I just discover ydf, a pure bash tool.
It's a tool that brings you a simple way to declare and install the tools you need along with its configurations.
You can create multiple selections of packages for your different needs, for example, you can create a packages selection for your laptop, desktop, servers, different operating systems, etc.
It looks really cool.
Github: https://github.com/yunielrc/ydf
https://redd.it/172ozt5
@r_bash
I just discover ydf, a pure bash tool.
It's a tool that brings you a simple way to declare and install the tools you need along with its configurations.
You can create multiple selections of packages for your different needs, for example, you can create a packages selection for your laptop, desktop, servers, different operating systems, etc.
It looks really cool.
Github: https://github.com/yunielrc/ydf
https://redd.it/172ozt5
@r_bash
GitHub
GitHub - yunielrc/ydf: A dotfiles manager+. Be ready to work in just a few minutes on your Fresh OS
A dotfiles manager+. Be ready to work in just a few minutes on your Fresh OS - yunielrc/ydf
bashunit 0.8
The new release for bashunit is ready!
You can now define mocks, spies, among many other features.
\- Check the improved Getting Started page
\- Check the full CHANGELOG for the new release 0.8
bashunit is a game changer for the bash ecosystem.
​
>The simplest testing library for bash noscripts - https://bashunit.typeddevs.com/
​
https://redd.it/17311bi
@r_bash
The new release for bashunit is ready!
You can now define mocks, spies, among many other features.
\- Check the improved Getting Started page
\- Check the full CHANGELOG for the new release 0.8
bashunit is a game changer for the bash ecosystem.
​
>The simplest testing library for bash noscripts - https://bashunit.typeddevs.com/
​
https://redd.it/17311bi
@r_bash
Typeddevs
Getting Started | bashunit
Test your bash noscripts in the fastest and simplest way, discover the most modern bash testing library.
From Complex to Simple
Hello, r/bash!
It's curious how things can get complicated in so many ways. I created a noscript that I use in my tmux.conf to retrieve the IPv4 address of my network interfaces and display it in my status bar, following a set of priorities. Depending on which interface is active, it prioritizes one over the other. Code of my first version:
#!/bin/bash
INET=''
if [[ -f /sys/class/net/tun0/operstate ]]; then
INET='tun0'
elif [[ $(< /sys/class/net/eth0/operstate) == "up" ]]; then
INET='eth0'
else
INET='wlan0'
fi
echo $(ip -4 addr show $INET | grep -oP "(?<=inet\s)\d+(\.\d+){3}")
This noscript worked 'fine', at least it served its purpose for several months. My problem was that sometimes the interface names didn't match, so I decided to make an array of names:
#!/bin/bash
INET=''
name=('tun0' 'eth0' 'wlan0')
if [[ -f /sys/class/net/${name[1]}/operstate ]]; then
INET=${name[1]}
elif [[ $(< /sys/class/net/${name[2]}/operstate) == "up" ]]; then
INET=${name[2]}
else
INET=${name[3]}
fi
echo $(ip -4 addr show $INET | grep -oP "(?<=inet\s)\d+(\.\d+){3}")
This works 'better' because I either just have to change the name within the array for the interface I want to prioritize or directly change the interface name in the network interface configuration.
I thought for a moment, why not simply use a regex to search for the interface name and then find the assigned IP of that interface, and this was the result:
#/bin/bash
inets=$(ip addr | awk '/^[0-9]+: (.*):/ {print $2}')
if [[ $inets =~ tun[0-9]+ && $(< /sys/class/net/${BASH_REMATCH[0]}/operstate) == "up" ]]; then
inet="${BASH_REMATCH[0]}"
elif [[ $inets =~ eth[0-9]+|enp.*[0-9]+ && $(< /sys/class/net/${BASH_REMATCH[0]}/operstate) == "up" ]]; then
inet="${BASH_REMATCH[0]}"
elif [[ $inets =~ wlan[0-9]+ && $(< /sys/class/net/${BASH_REMATCH[0]}/operstate) == "up" ]]; then
inet="${BASH_REMATCH[0]}"
else
echo [no inet or ip]
exit 1
fi
echo $(ip -4 addr show $inet | grep -oP "(?<=inet\s)\d+(\.\d+){3}")
This noscript once again works, and I don't have to change the interface name or do much, but I thought, 'This seems too complicated. Is there an easier way to do this?'
Reading a bit about how the `ip` command works, I realized that there's a `-o` flag I wasn't aware of, and this simplified the problem a lot. This way, I could parse the information with simple `awk` and regex. Here was the result:
#/bin/bash
ip -o -4 addr show | awk '/tun[0-9]+/ {print $4; exit}
/eth[0-9]+|enp.*[0-9]+/ {print $4; exit}
/wlan[0-9]+/ {print $4; exit}'
This works very well, and I'm quite happy with the final result. But it got me thinking, it all became complicated because I didn't know the `ip` command's arguments well. I wanted to share this experience with you in case it happens to any of you, so you can find something useful in it. But tell me, r/bash, has something similar ever happened to you? I mean, has a noscript ever become overly complicated, and then you found a much simpler way to do it?
If you think there's an even easier/better way to accomplish this task, I'd love to hear from you.
https://redd.it/173h2xz
@r_bash
Hello, r/bash!
It's curious how things can get complicated in so many ways. I created a noscript that I use in my tmux.conf to retrieve the IPv4 address of my network interfaces and display it in my status bar, following a set of priorities. Depending on which interface is active, it prioritizes one over the other. Code of my first version:
#!/bin/bash
INET=''
if [[ -f /sys/class/net/tun0/operstate ]]; then
INET='tun0'
elif [[ $(< /sys/class/net/eth0/operstate) == "up" ]]; then
INET='eth0'
else
INET='wlan0'
fi
echo $(ip -4 addr show $INET | grep -oP "(?<=inet\s)\d+(\.\d+){3}")
This noscript worked 'fine', at least it served its purpose for several months. My problem was that sometimes the interface names didn't match, so I decided to make an array of names:
#!/bin/bash
INET=''
name=('tun0' 'eth0' 'wlan0')
if [[ -f /sys/class/net/${name[1]}/operstate ]]; then
INET=${name[1]}
elif [[ $(< /sys/class/net/${name[2]}/operstate) == "up" ]]; then
INET=${name[2]}
else
INET=${name[3]}
fi
echo $(ip -4 addr show $INET | grep -oP "(?<=inet\s)\d+(\.\d+){3}")
This works 'better' because I either just have to change the name within the array for the interface I want to prioritize or directly change the interface name in the network interface configuration.
I thought for a moment, why not simply use a regex to search for the interface name and then find the assigned IP of that interface, and this was the result:
#/bin/bash
inets=$(ip addr | awk '/^[0-9]+: (.*):/ {print $2}')
if [[ $inets =~ tun[0-9]+ && $(< /sys/class/net/${BASH_REMATCH[0]}/operstate) == "up" ]]; then
inet="${BASH_REMATCH[0]}"
elif [[ $inets =~ eth[0-9]+|enp.*[0-9]+ && $(< /sys/class/net/${BASH_REMATCH[0]}/operstate) == "up" ]]; then
inet="${BASH_REMATCH[0]}"
elif [[ $inets =~ wlan[0-9]+ && $(< /sys/class/net/${BASH_REMATCH[0]}/operstate) == "up" ]]; then
inet="${BASH_REMATCH[0]}"
else
echo [no inet or ip]
exit 1
fi
echo $(ip -4 addr show $inet | grep -oP "(?<=inet\s)\d+(\.\d+){3}")
This noscript once again works, and I don't have to change the interface name or do much, but I thought, 'This seems too complicated. Is there an easier way to do this?'
Reading a bit about how the `ip` command works, I realized that there's a `-o` flag I wasn't aware of, and this simplified the problem a lot. This way, I could parse the information with simple `awk` and regex. Here was the result:
#/bin/bash
ip -o -4 addr show | awk '/tun[0-9]+/ {print $4; exit}
/eth[0-9]+|enp.*[0-9]+/ {print $4; exit}
/wlan[0-9]+/ {print $4; exit}'
This works very well, and I'm quite happy with the final result. But it got me thinking, it all became complicated because I didn't know the `ip` command's arguments well. I wanted to share this experience with you in case it happens to any of you, so you can find something useful in it. But tell me, r/bash, has something similar ever happened to you? I mean, has a noscript ever become overly complicated, and then you found a much simpler way to do it?
If you think there's an even easier/better way to accomplish this task, I'd love to hear from you.
https://redd.it/173h2xz
@r_bash
Reddit
From the bash community on Reddit
Explore this post and more from the bash community
would you evaluate this noscript? cp + chown
I made a noscript to migrate old user data to my new current user. As things turn out, I wrote this noscript to copy data from anywhere outside the loggedin user to the loggedin user and change the ownership of the files/directories to USER:USER.
However, as this is a pretty delecate process, I'm a little cautious to just use it. Would you mind to have a look at the noscript to evaluate it's validity? I provided comments for each step to clarify the reasoning behind it.
https://redd.it/173qk8b
@r_bash
I made a noscript to migrate old user data to my new current user. As things turn out, I wrote this noscript to copy data from anywhere outside the loggedin user to the loggedin user and change the ownership of the files/directories to USER:USER.
However, as this is a pretty delecate process, I'm a little cautious to just use it. Would you mind to have a look at the noscript to evaluate it's validity? I provided comments for each step to clarify the reasoning behind it.
#!/bin/bash
### arguments
[[ ${#@} != 2 ]] && exit 1 # 2 arguments are expected
### source
source=$(readlink -e "$1") # convert relative paths to absolute paths if
necessary; will filter non-existing files or paths
# errors
[[ "$source" = "" ]] && exit 2 # file or paht invalid
[[ "$source" = "/" ]] && exit 3 # copying / is not allowed
[[ "$source" = "$HOME"* ]] && exit 4 # copying from the current user dirs is not
allowed
[[ "$HOME" != "${HOME//"$source/"/}" ]] && exit 5 # copying from a path
containing the current user dirs as subdirectory is not allowed
### destination
destination="$(readlink -e "$2")" # convert relative paths to absolute paths if
necessary; will filter non-existing files or paths; however, in case of copying a
file that doesn't exist yet will also result in a empty string
if [[ -d "$source" || -d "$destination" ]]; then
add="/$(basename "$source")"
# if a dir is copied, get the dir name for the chown command
# or
# for copying a file: if only a valid path is provided as destination, get
the file name for the chown command
elif [[ -n "$(readlink -e "$(dirname "$2")")" ]]; then # for copying a file with
file name provided to the destination; check whether the path is valid
destination="$(readlink -e "$(dirname "$2")")/$(basename "$2")" # set the
absolute path and the file name as destination
fi
# errors
[[ -f "$(readlink -e "$destination$add")" ]] && exit 6 # case copying a file: if
file already exist (ok, this might be redundant because I use the -n flag for the
cp command)
[[ -d "$destination/$(basename "$source")" ]] && exit 7 # case copying a
directory: if the directory already exists at the destination
[[ -d "$source" && ! -d "$destination" ]] && exit 8 # if the destination path is
invalid
[[ "$destination" != "$HOME"* ]] && exit 9 # copying outside home of the current
user is not allowed
### operations
sudo cp -rpn "$source" "$destination"
echo "$source copied to $destination"
sudo chown -R $USER: "$destination$add"
echo "$USER owns $destination$add"
https://redd.it/173qk8b
@r_bash
Reddit
From the bash community on Reddit
Explore this post and more from the bash community
12 Super Cool Terminal Easter Eggs
https://medium.com/@tinplavec/12-super-cool-terminal-easter-eggs-edf6b48eb32c
I would like to share my article where I list cool 'Easter egg' programs you can try in your Bash shell. I know this article isn't directly related to noscripting, but it's still related to Bash and I hope you have fun trying out all these programs 😄.
And of course, comment if you know some other Easter eggs.
https://redd.it/173v4su
@r_bash
https://medium.com/@tinplavec/12-super-cool-terminal-easter-eggs-edf6b48eb32c
I would like to share my article where I list cool 'Easter egg' programs you can try in your Bash shell. I know this article isn't directly related to noscripting, but it's still related to Bash and I hope you have fun trying out all these programs 😄.
And of course, comment if you know some other Easter eggs.
https://redd.it/173v4su
@r_bash
Medium
12 Super Cool Terminal Easter Eggs You Need To Try
Terminal is quite a powerful tool. You will often see people bragging about their knowledge to use terminal commands. Indeed, using…
Am I the only one who thinks that bash is ugly?
Don't get me wrong, I love it, it's just...
​
https://preview.redd.it/m1xrt2qzd8tb1.png?width=886&format=png&auto=webp&s=2f8c77f3d700db91423654327a9d2c6df4ff3b6b
Even if it's clear and I consider it a good language everything feels so cluttered
https://redd.it/17414vj
@r_bash
Don't get me wrong, I love it, it's just...
​
https://preview.redd.it/m1xrt2qzd8tb1.png?width=886&format=png&auto=webp&s=2f8c77f3d700db91423654327a9d2c6df4ff3b6b
Even if it's clear and I consider it a good language everything feels so cluttered
https://redd.it/17414vj
@r_bash