r_bash – Telegram
Have you ever written a full on application in Bash? What was it?

I'm a very old hat programmer. C++ was newfangled stuff and nobody had ever spoken the word "Javanoscript" when I first learned how to code Hello World. Bsh/Bash was the first language I learned, and we called it "terminal programming" back then and not noscripting.


To this day its my go to if I need to write a linux-portable application that doesn't engage with the hardware enough to require C. I recently "finished" a program for controlling an entire network of remote Varnish server clusters, written in just under 2000 lines. It uses a pull-store-flag-edit-push-versioncontrol schema with 4 levels of granularity in managing .vcl files, and has remote tools built in for generating and pulling logs, modifying inline C include files, and controlling all the cache parameters. It even has a fancy toggling system that lets a non-VCL nerd enable and disable all the special modules, and its own Help menu.


I wrote this beast because I'm the only resident Varnish guru in our devteam, and I needed something simple that other administrators can use to control and maintain the system if I got hit by a bus. At its current line count, and with 28 menus I'm about 80% sure its the biggest Bash program I've written in my life. That got me wondering what kinds of things other people have written as their Magnum Opus.

https://redd.it/1dhsf7u
@r_bash
Site that returns protocol that can be used from the command line

Is there a site similar to ifconfig.me that you can curl so that it returns the protocol it was hit with? I.e. curl http://example.com should return http somewhere in the response and curl https://example.com should return https.

https://redd.it/1di8umt
@r_bash
Write to file keeps service restarting

I am trying to write a simple multi-line value to a file.

I've noticed as I watch the processes, and the logs, whenever I add that particular code and re-start my service, it loops over and over again, instead of running once and then waiting for the timer to re-activate it.

cat ${dir}/${file}.json << EOF
{
"id": "${item_id}",
}
EOF


I then execute

systemctl --user start my_service_name.service


And then loop begins. As soon as I remove that particular set of code and re-execute, then the noscript only runs once, and then waits 15 minutes for the .timer to call it again.

I've tried both cat and tee hoping one or the other would work.

https://redd.it/1did9c7
@r_bash
messed up configuration

Hi

i am running tumbleweed and messed up my bashrc (i think).

I followed this guide:

https://christitus.com/beautiful-bash/

i recognized afterwords that a comment says "this wont work on opensuse".



now, everytime i start my terminal, i get an "bash: /home/*user*/.bashrc: Permission denied"


is there a simple way to fix that? or do i have to reverse engineer the sh noscript?

https://redd.it/1djf1od
@r_bash
Anyone help me understand why this string fails regex validation?

This code outputs "bad" instead of "good" even though the regex seems to work fine when tested on regex101.com . Does anyone understand what is wrong?

#!/usr/bin/env bash

readonly serverVer="1.2.3.4"

if [[ "$serverVer" =~ ^(?:(\d+)\.)?(?:(\d+)\.)?(?:(\d+)\.)?(\*|\d+)$ ]]; then

echo good

fi

echo bad

https://redd.it/1djft6r
@r_bash
How would you learn bash noscripting today?

Through the perspective of real practise, after years of practical work, having a lot of experience, how wold you build your mastery of bash noscripting in these days?

*which books?
*video lessons?
*online courses?
*what kind of pet projects or practices?
*any other advices?

Thank you!

https://redd.it/1djhccz
@r_bash
How to extract certain text from local html files in ubuntu

I have several HTML files saved locally. How can I extract numbers that start with "-" from all of them and save each number in a separate line in a new file? For example, the numbers are formatted like this: - 74345. I want each of these numbers to be placed on a new line in the output file.

https://redd.it/1djqcjs
@r_bash
CLI lightweight 3D printer progress viewer noscript
https://redd.it/1dk0sgj
@r_bash
source file counter variable

My post keeps getting removed for my code.

My source file has 4 line is such as

img_1=file1

img_2=file2

I'm trying to write a noscript with a counter to "ls -lh $img_1".... be easier to explain if I could post my code

https://redd.it/1dl8zkx
@r_bash
Need Help Sorting Files by Hashing in Bash Script

I've been trying to sort files in a folder by comparing them to a source directory using BLAKE2 hashing on my unraid server. The noscript should move matching files from the destination directory to a new folder. However, it keeps saying "Destination file not found" even though the files exist.

### Here’s the noscript:

```bash
#!/bin/bash

# Directories
source_dir="/path/to/source_directory"
destination_dir="/path/to/destination_directory"
move_to_dir="/path/to/move_to_directory"

# Log file
log_file="/path/to/logs/move_files.log"

# Function to calculate BLAKE2 hash
calculate_hash() {
/usr/bin/python3 -c 'import hashlib, sys; h = hashlib.blake2b(); h.update(sys.stdin.buffer.read()); print(h.hexdigest())'
}

# Ensure destination directory exists
mkdir -p "$move_to_dir"

# Iterate through files in source directory and subdirectories
find "$source_dir" -type f -print0 | while IFS= read -r -d '' source_file; do
# Print source file for debugging
echo "Source File: $source_file"

# Calculate hash of the file in the source directory
source_hash=$(calculate_hash < "$source_file")

# Calculate relative path for destination file
relative_path="${source_file#$source_dir}"
destination_file="$destination_dir/$relative_path"

# Print destination file for debugging
echo "Destination File: $destination_file"

# Check if destination file exists
if [ -f "$destination_file" ]; then
# Print hash calculation details for debugging
echo "Calculating hashes..."
destination_hash=$(calculate_hash < "$destination_file")

# Log hashes for debugging
echo "$(date +"%Y-%m-%d %H:%M:%S") - Source Hash: $source_hash, Destination Hash: $destination_hash" >> "$log_file"

# Compare hashes
if [ "$source_hash" == "$destination_hash" ]; then
# Move the file to the new directory
mv "$destination_file" "$move_to_dir/"

# Log the move
echo "$(date +"%Y-%m-%d %H:%M:%S") - Moved: $destination_file" >> "$log_file"
fi
else
echo "Destination file not found: $destination_file"
fi
done

echo "Comparison and move process completed."

https://redd.it/1dm775e
@r_bash
learning file permissions, what is the "owner" "group" and "other"?

hello i'm trying to learn and understand file permissions in bash, and to what i understand there are 3 "categories" in bash?

owner, group and other?

what do these things mean? what does owner mean? is that strictly the user that made the file or can the owner of a file give ownership of that file to another user?

what are groups?

and what are "other"? what does that mean?

thank you

https://redd.it/1dmfkes
@r_bash
What's the most elegant way to achieve this?

So I have a wine program I'd like to run and also a wine prefix I'd like to run that program in. Both have long paths.

Should I alias them both in .bash_aliases, then call them within a noscript and call it a day? Preferably something I could also bind to a key easily.

Sorry if this question is dumb.



https://redd.it/1dmzfe9
@r_bash
bashbro - New Software Release (rework of bashttpd)

Newly released bashbro - it's Bash-based web file browser that allows you to remotely browse, stream, view documents and save files via your web browser. Super easy to use, try it!!

https://github.com/victrixsoft/bashbro/

https://redd.it/1dndldo
@r_bash
Counterintuitive word splitting

I've recently already made a post about word splitting, however, this seems to be another unrelated issue that I again can't seem to find any answers. Consider this setup:

$ #!/bin/bash
$ # version 5.2.26
$ IFS=" :" # space (ifs-whitespace), colon (ifs-non-whitespace)
$ A=" ::word:: " # spaces, colon, "word", colon, spaces
$ printf "'%s'\n" $A
''
''
'word'
''

As you can see, printf got 4 arguments, as opposed to 3, what I would've expected. First, I though my previous post might be related, however, adding another instance of `$A` to the end makes it 8 arguments, exactly double, so it's not related to stripping trailing "null arguments".

Why does this happen? Is there a sentence in the man page that explains this behavior (I couldn't parse it from the section about word splitting :'D)

Edit: I tested the following bourne-like shells:

bash
bash -o posix
dash
ksh
mksh
yash
yash -o posix
posh (policy-compliant ordinary shell)
pbosh (schilytools)
mrsh (by Simon Ser)

ALL of them do it exactly the same, except mrsh (it's doing what I expected). However, mrsh is quite niche and rather a hobby project by someone, so I wouldn't take that as any authority.

https://redd.it/1dnqswy
@r_bash
Question about stream redirection / file denoscriptors

**TL;DR - In bash, what is the significance of the `-` character in the following expression?: `${@}"; echo "${?}" 1>&3-;`**

Problem denoscription:

While trying to find a way to capture stderr, stdout, and return code to separate variables, I came across a solution [on this stackoverflow post](https://stackoverflow.com/questions/11027679/capture-stdout-and-stderr-into-different-variables).. I am mostly looking at the section labeled "**6. Preserving the exit status with sanitization – unbreakable (rewritten)**" which has this:

{
IFS=$'\n' read -r -d '' CAPTURED_STDOUT;
IFS=$'\n' read -r -d '' CAPTURED_STDERR;
(IFS=$'\n' read -r -d '' _ERRNO_; exit ${_ERRNO_});
} < <((printf '\0%s\0%d\0' "$(((({ some_command; echo "${?}" 1>&3-; } | tr -d '\0' 1>&4-) 4>&2- 2>&1- | tr -d '\0' 1>&4-) 3>&1- | exit "$(cat)") 4>&1-)" "${?}" 1>&2) 2>&1)

It seems to work ok. although I am making my own alterations. I've read through the post a couple times and mostly understand what's going on (short version is some trickery using redirection to different denoscriptors and reformatting output with `NUL` / `\0` so that `read` can pull it into the appropriate variables).

I get that e.g. `1>&3-; ` is redirecting from file denoscriptor 1 to file denoscriptor 3, `1>&4-` is redirecting from file denoscriptor 1 to file denoscriptor 4, and so on. But I've never seen stream redirection examples with a trailing hyphen before and I don't really understand the significance of having a `-` following `1>&3` etc. I have been hitting ddg and searx for the last 30 minutes and still coming up empty-handed.

Any idea what am I missing? Is there any functional difference between using `1>&3-; ` vs `1>&3; ` or is it just a coding style thing?

https://redd.it/1dnxehx
@r_bash
Differences between (MacOS) 3.2.57 and 5.x?

Hi, folks. I'm sure this has been asked before. I've been doing searches but keep bumping up against posts about ZSH or how to upgrade with Brew.

Unfortunately, I'm in a bit of a tight spot. I have not found an answer to what I need and am hoping someone can point me in the right direction.

I wrote a BASH noscript that is fairly sophisticated. Nothing too crazy though. Lots of functions, a few run-of-the-mill commands like find, sort, uniq, awk. Keywords like 'local' and 'read.'

It works on my laptop (Windows running BASH 5.2.21 under Cygwin - I'm not allowed to run WSL) and runs perfectly on a Linux host. Idk the BASH version on the Linux side (and logging into it is a PITA which is why I'm not checking) but it's a modern Linux so probably 5.x. I handed the noscript to a coworker who ran my noscript on his MacOS laptop and found it didn't work. 🤦

Sigh. So, now I need to try to figure out what BASH feature I'm using that's not compatible with 3.x. I can't tell all my coworkers to upgrade BASH just so my noscript will work. I don't have time to make my noscript compatible with ZSH. I'm probably the only one in the dept NOT running MacOS. I'm starting to remember why 🤣😬

If anybody has ideas of where I can look for guidance on what features to avoid when making a BASH noscript work on MacOS, I'd appreciate it. Maybe 4.0 and 5.0 release notes on what features were introduced?

Is variable expansion ${} incompatible or running a subprocess with $() instead of backticks?

I wish I could share the noscript but I would be violating rules doing that.

Thanks in advance

https://redd.it/1doc89p
@r_bash
RAG in bash for MongoDB Atlas

Hi everyone, I made a small bash noscript (based on a JS noscript) that allows you do turn data/insights into AI and then query it directly in the terminal.

https://github.com/farspeak/farspeak-cli

https://preview.redd.it/pojyoheqlr8d1.png?width=1600&format=png&auto=webp&s=961b13e6f43af6c23a5fe326590968b683b2b1d5

Please let me know what you think


https://redd.it/1dodphp
@r_bash
Does anyone know of a good way to read raw hexadecimal / uint data using only bash builtins?

Im trying to figure out a way to convert integers to/from their raw hex/uint form.

Bash stores integers as ascii, meaning that each byte provides 10 numbers and N bytes of data allows you to represent numbers up to of `10^N - 1`. With hex/uint, all possible bit combinations represent integers, meaning each byte provides 256 numbers and N bytes of data allows you to represent numbers up to `256^N - 1`.

In practice, this means that (on average) it takes ~60% less space to store a given integer (since they are being stored `log(256)/log(10) = ~2.4` times more efficiently).

Ive figured out a pure-bash way to convert integers (between 0 and `2^64 - 1` to their raw hex/uint values:

shopt -s extglob
shopt -s patsub_replacement

dec2uint () {
local a b nn;
for nn in "$@"; do
printf -v a '%x' "$nn";
printf -v b '\\x%s' ${a//@([0-9a-f])@([0-9a-f])/& };
printf "$b";
done
}

We can check that this does infact work by determining the number associated with some hex string, feeding that number to `dec2uint` and piping the output to xxd (or hexdump), which should show the hex we started with

# echo $(( 16#1234567890abcdef ))
1311768467294899695

# dec2uint 1311768467294899695 | xxd
00000000: 1234 5678 90ab cdef .4Vx....

In this case, the number that usually takes 19 bytes to represent instead takes only 8 bytes.

# printf 1311768467294899695 | wc -c
19

# dec2uint 1311768467294899695 | wc -c
8

***

At any rate, Im am trying to figure out how to do the reverse operation, speciffically the functionality that is provided by xxd (or by hexdump) in the above example, efficiently using only bash builtins...If I can figure this out then it is easy to convert back to the number using printf.

Anyone know of a way to get bash to read raw hex/uint data?

https://redd.it/1dor7ss
@r_bash