r_bash – Telegram
multi_selector_members+=("$new_selector")
            parent_selector_stack+=("&referencetomultiselect&")
        else
            # If there's a current selector, combine it with the new selector
            if [ -n "$current_selector" ]; then
                current_selector="$current_selector $new_selector"
            else
                current_selector="$new_selector"
            fi
            parent_selector_stack+=("$current_selector")
        fi

    elif [[ "$line" =~ ^[.#\[][^,{]*,$ ]]; then
        # Handle multi-selectors (those ending with a comma)
        is_multi_selector=true
        new_selector=$(echo "$line" | sed 's/,//g' | tr -d '\n')
        multi_selector_members+=("$new_selector")

    elif [[ "$line" =~ ^\}$ ]]; then
        # End of the current block
        flush_selector

        # Handle multi-selector members if they exist
        if [ "$is_multi_selector" = true ]; then
            expanded_selectors=$(handle_multi_selectors_with_children "${multi_selector_members[@]}")
            current_selector="$expanded_selectors"
            flush_selector
            multi_selector_members=() # Clear the multi-selector list
            is_multi_selector=false
        fi

        # Reset media query flag if we're closing a media query
        if [ "$media_query" = true ]; then
            media_query=false
        fi

        # Loop through parent_selector_stack and find references to multi-selector
        final_selectors=()
        for selector in "${parent_selector_stack[@]}"; do
            if [[ "$selector" == "&referencetomultiselect&" ]]; then
                # Replace reference with expanded multi-selectors
                final_selectors+=($(handle_multi_selectors_with_children "${multi_selector_members[@]}"))
            else
                final_selectors+=("$selector")
            fi
        done

        # Add the final selectors back to the parent stack
        parent_selector_stack=("${final_selectors[@]}")

        # Pop the last selector from the stack (we're closing the current block)
        if [ ${#parent_selector_stack[@]} -gt 0 ]; then
            unset 'parent_selector_stack[-1]'
        fi

        # Reset the current selector to the top of the stack
        if [ ${#parent_selector_stack[@]} -gt 0 ]; then
            current_selector="${parent_selector_stack[-1]}"
        else
            current_selector=""
        fi

    else
        # Treat the line as a style and append it
        current_styles="$current_styles\n $line"
    fi
done <"$input_file"

# Flush any remaining selectors
flush_selector

echo "CSS processing complete. Output written to $output_file"






https://redd.it/1ic6azt
@r_bash
Get stderr and stdout separated?

How would I populate e with the stderr stream?

r="0"; e=""; m="$(eval "$logic")" || r="1" && returnCode="1"

I need to "return" it with the function, hence I cannot use a function substitution forward of 2> >()

I just want to avoid writing to a temp file for this.


https://redd.it/1iclsku
@r_bash
Why is "set -e" not the default or not left in noscripts after creation?

"set -e" as most of you know will exit a noscript on error. It (and other set commands) is often used in a noscript during development when errors are expected and you want immediate halt.

But why is this behavior not the default anyway? Surely in vast majority of cases when a noscript is in production and there is an error, you would want the noscript to halt rather than attempt to execute the rest of it (much of which will have dependency and less likely to just be an independent process)?

https://redd.it/1icwuqh
@r_bash
Does anyone have any good recommendations for learning awk? I've tried a few books and YT videos but I'm finding my eyes glazing over. I worked with LLMs to make it do some advanced editing and I'm super excited to learn how to actually drive it myself, but I haven't found anything that gels with me



https://redd.it/1id47fq
@r_bash
Help me improve my MySQL backup noscript

So I've got a working backup noscript for backing up MySQL databases on different database servers. The noscript is run every hour via cron job on an Apache server and subseqently backed up via FTP to a local NAS. I know it's not pretty, but as long as it works...

'''

#!/bin/bash

backupdir=/backup
timestamp=$(date +%Y-%m-%dT%H:%M)
user=dbuser
backup
retentiontime=10

mkdir -p "$backup
dir/$timestamp"

mysqldump --defaults-file=/files/.my.cnf --opt --no-tablespaces --user=$user --host='dbserver1.com' database1 | gzip -9 > ${backupdir}/$timestamp/database1-$timestamp.sql.gz
mysqldump --defaults-file=/files/.my.cnf --opt --no-tablespaces --user=$user --host='
dbserver2.com' database2 | gzip -9 > ${backupdir}/$timestamp/database2-$timestamp.sql.gz
mysqldump --defaults-file=/files/.my.cnf --opt --no-tablespaces --user=$user --host='dbserver1.com' database3 | gzip -9 > ${backupdir}/$timestamp/database3-$timestamp.sql.gz
mysqldump --defaults-file=/files/.my.cnf --opt --no-tablespaces --user=$user --host='
dbserver3.com' database4 | gzip -9 > ${backupdir}/$timestamp/database4-$timestamp.sql.gz
mysqldump --defaults-file=/files/.my.cnf --opt --no-tablespaces --user=$user --host='dbserver1.com' database5 | gzip -9 > ${backupdir}/$timestamp/database5-$timestamp.sql.gz
mysqldump --defaults-file=/files/.my.cnf --opt --no-tablespaces --user=$user --host='
dbserver1.com' database6 | gzip -9 > ${backupdir}/$timestamp/database6-$timestamp.sql.gz
mysqldump --defaults-file=/files/.my.cnf --opt --no-tablespaces --user=$user --host='dbserver1.com' database7 | gzip -9 > ${backupdir}/$timestamp/database7-$timestamp.sql.gz
mysqldump --defaults-file=/files/.my.cnf --opt --no-tablespaces --user=$user --host='
dbserver1.com' database8 | gzip -9 > ${backupdir}/$timestamp/database8-$timestamp.sql.gz
mysqldump --defaults-file=/files/.my.cnf --opt --no-tablespaces --user=$user --host='dbserver1.com' database9 | gzip -9 > ${backupdir}/$timestamp/database9-$timestamp.sql.gz
mysqldump --defaults-file=/files/.my.cnf --opt --no-tablespaces --user=$user --host='
dbserver1.com' database10 | gzip -9 > ${backupdir}/$timestamp/database10-$timestamp.sql.gz
mysqldump --defaults-file=/files/.my.cnf --opt --no-tablespaces --user=$user --host='dbserver1.com' database11 | gzip -9 > ${backupdir}/$timestamp/database11-$timestamp.sql.gz
mysqldump --defaults-file=/files/.my.cnf --opt --no-tablespaces --user=$user --host='
dbserver1.com' database12 | gzip -9 > ${backupdir}/$timestamp/database12-$timestamp.sql.gz
mysqldump --defaults-file=/files/.my.cnf --opt --no-tablespaces --user=$user --host='dbserver1.com' database13 | gzip -9 > ${backupdir}/$timestamp/database13-$timestamp.sql.gz
mysqldump --defaults-file=/files/.my.cnf --opt --no-tablespaces --user=$user --host='
dbserver1.com' database14 | gzip -9 > ${backupdir}/$timestamp/database14-$timestamp.sql.gz
mysqldump --defaults-file=/files/.my.cnf --opt --no-tablespaces --user=$user --host='dbserver1.com' database15 | gzip -9 > ${backupdir}/$timestamp/database15-$timestamp.sql.gz
mysqldump --defaults-file=/files/.my.cnf --opt --no-tablespaces --user=$user --host='
dbserver4.com' database16 | gzip -9 > ${backupdir}/$timestamp/database16-$timestamp.sql.gz
mysqldump --defaults-file=/files/.my.cnf --opt --no-tablespaces --user=$user --host='dbserver1.com' database17 | gzip -9 > ${backupdir}/$timestamp/database17-$timestamp.sql.gz
mysqldump --defaults-file=/files/.my.cnf --opt --no-tablespaces --user=$user --host='
dbserver1.com' database18 | gzip -9 > ${backupdir}/$timestamp/database18-$timestamp.sql.gz
mysqldump --defaults-file=/files/.my.cnf --opt --no-tablespaces --user=$user --host='dbserver1.com' database19 | gzip -9 > ${backupdir}/$timestamp/database19-$timestamp.sql.gz
mysqldump --defaults-file=/files/.my.cnf --opt --no-tablespaces --user=$user --host='
dbserver2.com' database20 | gzip -9 >
Help me improve my MySQL backup noscript

So I've got a working backup noscript for backing up MySQL databases on different database servers. The noscript is run every hour via cron job on an Apache server and subseqently backed up via FTP to a local NAS. I know it's not pretty, but as long as it works...

'''

#!/bin/bash

backup_dir=/backup
timestamp=$(date +%Y-%m-%dT%H:%M)
user=dbuser
backup_retention_time=10

mkdir -p "$backup_dir/$timestamp"

mysqldump --defaults-file=/files/.my.cnf --opt --no-tablespaces --user=$user --host='dbserver1.com' database_1 | gzip -9 > ${backup_dir}/$timestamp/database_1-$timestamp.sql.gz
mysqldump --defaults-file=/files/.my.cnf --opt --no-tablespaces --user=$user --host='dbserver2.com' database_2 | gzip -9 > ${backup_dir}/$timestamp/database_2-$timestamp.sql.gz
mysqldump --defaults-file=/files/.my.cnf --opt --no-tablespaces --user=$user --host='dbserver1.com' database_3 | gzip -9 > ${backup_dir}/$timestamp/database_3-$timestamp.sql.gz
mysqldump --defaults-file=/files/.my.cnf --opt --no-tablespaces --user=$user --host='dbserver3.com' database_4 | gzip -9 > ${backup_dir}/$timestamp/database_4-$timestamp.sql.gz
mysqldump --defaults-file=/files/.my.cnf --opt --no-tablespaces --user=$user --host='dbserver1.com' database_5 | gzip -9 > ${backup_dir}/$timestamp/database_5-$timestamp.sql.gz
mysqldump --defaults-file=/files/.my.cnf --opt --no-tablespaces --user=$user --host='dbserver1.com' database_6 | gzip -9 > ${backup_dir}/$timestamp/database_6-$timestamp.sql.gz
mysqldump --defaults-file=/files/.my.cnf --opt --no-tablespaces --user=$user --host='dbserver1.com' database_7 | gzip -9 > ${backup_dir}/$timestamp/database_7-$timestamp.sql.gz
mysqldump --defaults-file=/files/.my.cnf --opt --no-tablespaces --user=$user --host='dbserver1.com' database_8 | gzip -9 > ${backup_dir}/$timestamp/database_8-$timestamp.sql.gz
mysqldump --defaults-file=/files/.my.cnf --opt --no-tablespaces --user=$user --host='dbserver1.com' database_9 | gzip -9 > ${backup_dir}/$timestamp/database_9-$timestamp.sql.gz
mysqldump --defaults-file=/files/.my.cnf --opt --no-tablespaces --user=$user --host='dbserver1.com' database_10 | gzip -9 > ${backup_dir}/$timestamp/database_10-$timestamp.sql.gz
mysqldump --defaults-file=/files/.my.cnf --opt --no-tablespaces --user=$user --host='dbserver1.com' database_11 | gzip -9 > ${backup_dir}/$timestamp/database_11-$timestamp.sql.gz
mysqldump --defaults-file=/files/.my.cnf --opt --no-tablespaces --user=$user --host='dbserver1.com' database_12 | gzip -9 > ${backup_dir}/$timestamp/database_12-$timestamp.sql.gz
mysqldump --defaults-file=/files/.my.cnf --opt --no-tablespaces --user=$user --host='dbserver1.com' database_13 | gzip -9 > ${backup_dir}/$timestamp/database_13-$timestamp.sql.gz
mysqldump --defaults-file=/files/.my.cnf --opt --no-tablespaces --user=$user --host='dbserver1.com' database_14 | gzip -9 > ${backup_dir}/$timestamp/database_14-$timestamp.sql.gz
mysqldump --defaults-file=/files/.my.cnf --opt --no-tablespaces --user=$user --host='dbserver1.com' database_15 | gzip -9 > ${backup_dir}/$timestamp/database_15-$timestamp.sql.gz
mysqldump --defaults-file=/files/.my.cnf --opt --no-tablespaces --user=$user --host='dbserver4.com' database_16 | gzip -9 > ${backup_dir}/$timestamp/database_16-$timestamp.sql.gz
mysqldump --defaults-file=/files/.my.cnf --opt --no-tablespaces --user=$user --host='dbserver1.com' database_17 | gzip -9 > ${backup_dir}/$timestamp/database_17-$timestamp.sql.gz
mysqldump --defaults-file=/files/.my.cnf --opt --no-tablespaces --user=$user --host='dbserver1.com' database_18 | gzip -9 > ${backup_dir}/$timestamp/database_18-$timestamp.sql.gz
mysqldump --defaults-file=/files/.my.cnf --opt --no-tablespaces --user=$user --host='dbserver1.com' database_19 | gzip -9 > ${backup_dir}/$timestamp/database_19-$timestamp.sql.gz
mysqldump --defaults-file=/files/.my.cnf --opt --no-tablespaces --user=$user --host='dbserver2.com' database_20 | gzip -9 >
${backup_dir}/$timestamp/database_20-$timestamp.sql.gz
mysqldump --defaults-file=/files/.my.cnf --opt --no-tablespaces --user=$user --host='dbserver1.com' database_21 | gzip -9 > ${backup_dir}/$timestamp/database_21-$timestamp.sql.gz
mysqldump --defaults-file=/files/.my.cnf --opt --no-tablespaces --user=$user --host='dbserver1.com' database_22 | gzip -9 > ${backup_dir}/$timestamp/database_22-$timestamp.sql.gz
mysqldump --defaults-file=/files/.my.cnf --opt --no-tablespaces --user=$user --host='dbserver1.com' database_23 | gzip -9 > ${backup_dir}/$timestamp/database_23-$timestamp.sql.gz
mysqldump --defaults-file=/files/.my.cnf --opt --no-tablespaces --user=$user --host='dbserver1.com' database_24 | gzip -9 > ${backup_dir}/$timestamp/database_24-$timestamp.sql.gz
mysqldump --defaults-file=/files/.my.cnf --opt --no-tablespaces --user=$user --host='dbserver1.com' database_25 | gzip -9 > ${backup_dir}/$timestamp/database_25-$timestamp.sql.gz

find $backup_dir -depth -type d -mtime +$backup_retention_time -exec rm -r {} \;

'''

My main goal is to implement a rolling backup/retention strategy, i.e. I want to keep

* 24 hourly backups
* 7 daily backups
* 12 monthly backups

Any help is greatly appreciated!

EDIT: changed the timestamp from %Y-%m-%dT%H:%M to %Y-%m-%dT%H-%M for better compatibility.

https://redd.it/1icu3vc
@r_bash
How to configure terminal to show the entire previous command, without truncating it with an ellipsis?

Using iterm2 on MacOS with ZSH and powerlevel10k and Oh-My-Zsh. Nothing unusual.

When I paste a long `curl` command (with a request body that has a few dozen lines or more) into the terminal and execute it, I want to see the entire command when I press the Up arrow key to reload the last command from my history.

But what actually happens is only the last 30 or so lines of the command are shown when I press the Up arrow key, truncating all the lines above with an ellipsis (...).

I want to configure my terminal to actually display the *whole* entire command when I press Up.

I assume this is a config issue somewhere either in my `\~/.zshrc` file or the `\~/.p10k.zsh` file, but have no clue if that's correct.

https://redd.it/1ic5jhd
@r_bash
jq throwing parse errors

I have the following in a file called test.txt:

```
[
[
"a",
"b"
],
[
"c",
"d"
]
]
```

I inserted it into a shell variable like this:

```
$ test_records=$(cat test.txt)
```

When I echo test_records, I get this:

```
$ echo $test_records
[ [ "a", "b" ], [ "c", "d" ] ]
```

When I iterate through, I get the following:

```
$ for record in $test_records; do echo $record; done
[
[
"a",
"b"
],
[
"c",
"d"
]
]
```

Note the opening and closing brackets which I think are related to the issue. Anyway, when I try to pipe the result of the echo to jq, I get the following:

```
$ for record in $test_records; do echo $record | jq '.[0]'; done
jq: parse error: Unfinished JSON term at EOF at line 2, column 0
jq: parse error: Unfinished JSON term at EOF at line 2, column 0
jq: error (at <stdin>:1): Cannot index string with number
jq: parse error: Expected value before ',' at line 1, column 4
jq: error (at <stdin>:1): Cannot index string with number
jq: parse error: Unmatched ']' at line 1, column 1
jq: parse error: Unfinished JSON term at EOF at line 2, column 0
jq: error (at <stdin>:1): Cannot index string with number
jq: parse error: Expected value before ',' at line 1, column 4
jq: error (at <stdin>:1): Cannot index string with number
jq: parse error: Unmatched ']' at line 1, column 1
jq: parse error: Unmatched ']' at line 1, column 1
```

As I said, I think this is because of the opening and closing brackets. If so, why are they there? If not, what's the issue with the filter string?

Thanks,
Rob

https://redd.it/1idrbfy
@r_bash
Can I get a variable, at all times, to keep the output of the last command?

To capture the output of a command, I do

2>&1|tee capture.log

, but this is tedious and I find myself always needing it.

Is it possible to do some magic in the background, so that the output of the last command is always captured in an environment variable?

I don't want to prefix the command with something like "capture" and I don't want to suffix it, with "2>&1";)


I just want the variable, at all times, to keep the output of the last command.

https://redd.it/1ie270l
@r_bash
New to grep - why are these commands returning different results?

I'm working through the Sander van Vugt RHCSA video course, and on one of the labs you're asked to "use grep to show the names of all the files in /etc that have lines that contain the text 'root' as a word."

The solution he suggest is to change your directory to /etc, and run grep 'root\\b' * 2> /dev/null which returns something like this:

https://preview.redd.it/8v6irsjkq8ge1.png?width=834&format=png&auto=webp&s=2fc253b31c6eafb2a9b53351a5150bd084763a4b

However, if I run grep 'root\\b' *2 /etc 2> /dev/null from my home directory, I get this:

https://preview.redd.it/ahneo97fr8ge1.png?width=522&format=png&auto=webp&s=a58b0001a3d8d6a3e094bd3f14ae28817c42d6bb

If I remove the 2> /dev/null to see my STDERR, I see these errors:

https://preview.redd.it/da5nd9dtq8ge1.png?width=358&format=png&auto=webp&s=3088ab8cc2515c994b7f035dba125899874dc87d

And I'm not clear on why that's the case. In the other examples he's provided, we use grep to search files in other directories. I'm sure I'm missing a flag somewhere.

I had looked into using --directories=recurse which does return results, but they different than what I see if I run grep 'root\\b' * 2> /dev/null directory from /etc:

https://preview.redd.it/pb4dz2m5s8ge1.png?width=848&format=png&auto=webp&s=1f9df5f5b743dfe188a11d43c65dc2ad236d7f60

I know I'm misunderstanding something, but I'm not sure what and I don't know if I understand grep well enough to ask the right question. In the end, what I'd like to know is if I can use grep to look into a different directory from my home directory, and what I need to be doing differently if that is an option.

https://redd.it/1ie4t0x
@r_bash
Is this the right way of processing an array with elements containing white spaces?

The following function takes a list of arguments and searches for elements in the form "--key=value" and prints them in the form "--key value", so for instance "aaa --option=bbb ccc" gets converted into "aaa --option bbb ccc".

expand_keyval_args() {
local result=()
for arg in "$@"; do
if [[ "$arg" == --*=* ]]; then
key="${arg%%=*}"
value="${arg#*=}"
printf "%s %q " "${key}" "${value}"
else
printf "%q " "${arg}"
fi
done
}


The way I deal with values containing white spaces (or really any character that should be escaped) is by using "%q" in printf, which means I can then do the following if I want to process an array:

local args=( ... )
local out="$(expand_keyval_args "${args[@]}")"
eval "args=(${out})"


Is it the best way of doing this or is there a better way (that doesn't involve the "eval")?

https://redd.it/1iekm3s
@r_bash
Check for any one of the background commands run by a loop exits with success

I have a loop that runs bluetooth command in the background (tries to connect to bluetooth devices with a timeout of X seconds).

If any one of those commands run by the loop exits (which can only mean either timeout of X seconds has passed after attempting to connect devices or a device has connected, which is usually immediately), then exit the noscript, else do something.

connect_trusted() {
local device
for device in $(bluetoothctl devices Trusted | cut -f 2 -d ' '); do
# this command runs in background, exiting immediately with success on
# connection or failure after timeout of 5 seconds has passed
bluetoothctl -t 5 connect "$device" &
done
}

# if even just 1 device was connected, exit noscript immediately since no more action is needed
if connect_trusted; then
exit 0
# else, launch bluetooth menu after 5 seconds have passed (implied when bluetooth command exits with failure)
else
do_something
fi

------------

How to check that "any one of the `bluetoothctl -t 5 connect "$device" &` commands" exited with success to then exit the noscript, else `do_something`?

https://redd.it/1ifdcpg
@r_bash
Anyone ever try to use Tmux + friends to build a TUI app?

Anyone ever try to use Tmux as the basis for a TUI for a bash app? Perhaps combined with `dialog`/`whiptail`, `fzf`, `bat`, `watch`, etc. It could even include some tmux plugins.

TUI apps similar to `lazygit`, `lazydocker` and wtfutil could possibly be quickly written as a bash noscript inside of a tmux layout.


Possible skeleton (untested):

#!/bin/bash

# app_name - denoscription of app_name
# usage:
# app_name <options>

set -euo pipefail

_dispatch() {
case "$1" in
"_start_tui")
shift
_start "$@"
;;
"_pane0_1")
shift
_loop _pane0_1
;;
"_pane0_2")
shift
_loop _pane0_2
;;
*)
_start_tmux "$@"
;;
esac
}

_loop() {
while sleep 5; do "$@" || true; done
}

_start_tmux() {
# enable tmux to run inside of tmux
unset TMUX TMUX_PANE TMUX_PLUGIN_MANAGER_PATH tmux_version
export TMUX_SOCKET="$(mktemp -u)"
# re-run self with $1=_layout
exec tmux \
-S "$TMUX_SOCKET" \
-p ~/.config/app_name \
-f ~/.config/app_name/tmux.conf \
-c "'$0' _start_tui $(printf '%q ' "$@")"
}

_start_tui() {
# TODO: unbind the prefix key, to disable the default keybinds.
# TODO: capture ctrl-c/INT to kill tmux (not individual pane noscripts)

_layout "$@" &
_loop _pane0_0
}

_layout() {
# TODO: layout panes. examples:
tmux split-window -h -t 0.0 "$0" _pane0_1
tmux split-window -v -t 0.1 "$0" _pane0_2
# TODO: settings
# TODO: app key bindings
# TODO: process command line options
}

# definitions of panes

_pane0_0() {
# noscript for window 0 pane 0

date
}

_pane0_1() {
# noscript for window 0 pane 1

top
}

_pane0_2() {
# TODO: noscript for window 0 pane 2
}

_dispatch "$@"



https://redd.it/1ies4ig
@r_bash
Can't seem to get the correct pid when using this bash noscript I'm working on. Any help?

When using this noscript: https://pastecode.io/s/py42w4xn (via usernoscripts on unraid)

The pid in the logs is not the same as the one that's showing when i run a ps aux | grep "[s]leep 10"

It always seems to be off one. What am I doing wrong here?

The goal is to basically reset a timer every time there's a update from inotifywait and then at the end perform a command.

Thanks!

https://redd.it/1ielvng
@r_bash
Total Newbie at Bash Scripting

I'm trying to create a noscript to download and datestamp YouTube videos. I can download the videos, but it comes down with the name given by it's creator. I want to append the upload date to the front of the filename. Any help is appreciated. My noscript so far:

read -p "Enter YouTube URL: " yt_url

echo "YouTube URL = ${yt_url}"



read -p "Enter upload date: " upload_date

echo "Upload Date = ${upload_date}"



file=$(yt-dlp --get-filename -o "%(noscript)s.mp4" $yt_url)

echo "File = ${file}"



yt-dlp -f mp4 "$yt_url"



https://redd.it/1iedal0
@r_bash
Custom bash noscript dependency graph

Hi all!
Some time ago I started to write a little bash noscript to check some kubernetes stuffs I need to check. By the time this noscript has become so huge with a lot of functions and variables. Sometimes I need to edit some things but I’m starting to get lost in the functions. Is there any automated way to create a graph that contains all the functions and them dependencies?

Thank you!

https://redd.it/1ig9d5f
@r_bash
How would you efficiently process every line in a file? while read is 70x slower than Python

I have written a lot of shell noscripts over the years and in most cases for parsing and analyzing text I just pipe things around to grep, sed, cut, tr, awk and friends. The processing speeds are really fast in those cases.

I ended up writing a pretty substantial shell noscript and now after seeding its data source with around 1,000 items I'm noticing things are slow enough that I'm thinking about rewriting it in Python but I figured I'd post this to see if anyone has any ideas on how to improve it. Using Bash 4+ features is fine.

I've isolated the slowness down to Bash looping over each line of output.

The amount of processing I'm doing on this text isn't a ton but it doesn't lend itself well to just piping data between a few tools. It requires custom programming.

That means my program ends up with code like this:

while read -r matched_line; do
# This is where all of my processing occurs.
echo "${line}"
done <<< "${matches}"

And in this case `${matches}` are lines returned by grep. You can also loop over the output of a program too such as `done < <(grep ...)`. On a few hundred lines of input this takes 2 full seconds to process on my machine. Even if you do nothing except echo the line, it takes that amount of time. My custom logic to do the processing isn't a lot (milliseconds).

I also tried reading it into an array with `readarray -t matched_lines` and then doing a `for matched_line in "${matched_lines[@]}"`. The speed is about the same as while read.

Alternatively if I take the same matches content and use Python using code like this:

with open(filename) as file:
for line in file:
print(line)

This finishes in 30ms. It's around 70x faster than Bash to process each line with only 1,000 lines.

Any thoughts? I don't mind Python but I already wrote the tool in Bash.



https://redd.it/1ifvg1m
@r_bash
Url-encode get string with multiple arguments?

I have one string that's like
action=query&format=json&list=allpages&aplimit=max&apfilterredir=nonredirects&apprefix=Wp/akz&apcontinue=Wp/akz/Bréhéville
If I put it into the url without encoding, it breaks because it contains special characters. If I put the whole thing into --data-urlencode it encodes the &s and treats it all as one argument.
Soo, what do I do?

https://redd.it/1iftomw
@r_bash
can you explain what this does?

echo '[q]sa[ln0=aln256%Pln256/snlbx]sb5567320342535949633984860024054390510049758475925810612727383477870370412074937779308150930912981042snlbxq'|dc



(It is in a single line)

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