r_bash | Unsorted

Telegram-канал r_bash - r_bash

36

Credits: @r_channels & @reddit2telegram

Subscribe to a channel

r_bash

A script to install llama-cpp-python with CUDA enabled

I made an auto-install script for myself that I thought some people might find useful or interesting. I have seen posts online where some have claimed to be unable to figure out a way to install llama-cpp-python so for those people maybe this can help and for everyone else this is just a plain fast way to do this.

1. Optionally installs Conda (which I personally recommend doing)
2. Installs the latest version of llama-cpp-python with CUDA enabled from the official llama-cpp-python GitHub repo.

GitHub - llama-installer.sh

FYI, if you choose to install Conda it links to this script: GitHub - install_conda.sh

Cheers guys and have a great day.

-J

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

Читать полностью…

r_bash

How to use "cut" correctly

Hi there,


I have a file with urls in it, followed by a little description.

`www.lol.com`///description

I want to use dmenu to open it in a brower, at the moment, I'm using this line :

cat ~/world/Documents/bookmarks | ~/.config/sway/scripts/mydmenu -p "Bookmarks:" | xargs -0 -I {} firefox "{}"

But sometimes it fails because xargs takes the full line with the spaces and the description. How would you do to still print the whole line in dmenu, but only takes the first part (url) into args ?

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

Читать полностью…

r_bash

i want to put raw code into a variable by utilizing heredoc, but it seems that the outer syntax is interpreting things

what i'm trying to do is make a script that would put some boilerplate code into files, so i need raw unexecuted code in a variable.

the smallest example of my problem can be shown with this code:

DEFAULT_PROGRAM=$(cat <<'EOF'
\)
EOF
)
echo $DEFAULT_PROGRAM


regardless of which of the 4 combinations of fixes i apply here (having quotes around EOF or not, and having the inner parenthesis escaped or not), it seems to never output just the raw parenthesis. Either it outputs the escaping character too \), or it errors out by saying:
EOF: command not found
syntax error near unexpected token `)'
`)'


as i understand it, it's the outer syntax $(cat ... ) that breaks it.

is there an elegant solution to this so that i don't have to resort to using echo with lots of character escaping?

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

Читать полностью…

r_bash

exitcode from a pipe inside an if statement

Backstory, SomeCommand produces 15-20 lines of output that the user needs to read. Sometimes it fails, most of the time in a known way.
My approach has been
if [[ `SomeCommand |tee /dev/stderr |grep -c Known Error; Xcode=${PIPESTATUS[0\]}` -gt 0 \]\]; then
echo Known error $Xcode
else
echo Unknown error $Xcode
exit
fi

/dev/stderr goes to the console so the user can see the output
grep finds the known error string & handles it correctly
but...

$Xcode is always 0 :(
If $Xcode is >0 and it's not the known error, the script should terminate.

Have been using true, false & echo as SomeCommand for testing, maybe this is an issue.

It's not the |tee part
if [[ `false |grep -c Known Error; Xcode=${PIPESTATUS[0\]}` -gt 0 \]\]; then echo found $Xcode; else echo not found $Xcode; fi
not found 0

It's something to do with the if [[ `...` \]\] bit
false |tee /dev/stderr |grep -c Known Error; Xcode=${PIPESTATUS[0\]}; echo $Xcode
0
1


If it's changed to if [...\], then it's always 1

if [ `echo "Known Error" |tee /dev/stderr |grep -c "Known Error"; Xcode=${PIPESTATUS[0\]}` -gt 0 \]; then echo found $Xcode; else echo not found $Xcode; fi
Known Error
found 1


if [ `echo "Unknown Error" |tee /dev/stderr |grep -c "Known Error"; Xcode=${PIPESTATUS[0\]}` -gt 0 \]; then echo found $Xcode; else echo not found $Xcode; fi
Unknown Error
not found 1


Someone please put me out of my misery.

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

Читать полностью…

r_bash

In theory, could all quoting be achieved with just the backlash character? Or are there instances where single quotes are required

In other words, are single quotes supported by necessity or pure convenience?

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

Читать полностью…

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&amp;format=png&amp;auto=webp&amp;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&amp;format=png&amp;auto=webp&amp;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&amp;format=png&amp;auto=webp&amp;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&amp;format=png&amp;auto=webp&amp;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

Читать полностью…

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

Читать полностью…

r_bash

FuzPad 1.0 is now released
https://github.com/JianZcar/FuzPad

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

Читать полностью…

r_bash

Help me improve my MySQL backup script

So I've got a working backup script for backing up MySQL databases on different database servers. The script 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 >

Читать полностью…

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

Читать полностью…

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

Читать полностью…

r_bash

convert nested css to standard syntax css

Hi im new in this sub and i don't have much experience with bash in general, i was trying to create a script that allows me to convert nested css that has multi-selector in to standard css, currently i got my script working for nested css, but it seem i can't find a way to eleaborate multi selector (es. #foo, .bar).
Also tryed to ask gpt but it just added some comments and made me feel more miserable.
Can someone give me a hand?

#!/bin/bash

# Ensure the script receives correct arguments
if [ "$#" -ne 2 ]; then
    echo "Usage: $0 input_file output_file"
    exit 1
fi

input_file=$1
output_file=$2

# Check if the input file exists and is readable
if [ ! -f "$input_file" ]; then
    echo "Error: Input file '$input_file' does not exist or is not a file."
    exit 1
fi

# Create or clear the output file
>"$output_file"

# Initialize variables
current_selector=""
current_styles=""
parent_selector_stack=()
multi_selector_members=()
is_multi_selector=false
media_query=false

# Function to flush a selector and its styles
flush_selector() {
    trimmed_current_styles=$(echo "$current_styles" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')
    trimmed_current_selector=$(echo "$current_selector" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')
    check_style=$(echo "$current_styles" | tr -d '[:space:]\\n')

    # Only flush if the selector and styles are not empty
    if [ -n "$trimmed_current_selector" ] && [ -n "$check_style" ]; then
        {
            echo "$trimmed_current_selector {"
            echo -e "$trimmed_current_styles"
            echo "}"
        } >>"$output_file"
    fi
    current_styles=""
}

# Handle multi-selectors (combine selectors with commas and ensure proper space)
handle_multi_selectors_with_children() {
    local base_selectors=("$@")
    local child_selector="$current_selector"
    local combined_selectors=""

    # Loop through each base selector and append the child selector
    for base_selector in "${base_selectors[@]}"; do
        if [ -n "$child_selector" ]; then
            combined_selectors+="$base_selector $child_selector, "
        else
            combined_selectors+="$base_selector, "
        fi
    done

    # Remove trailing comma and space
    combined_selectors=$(echo "$combined_selectors" | sed 's/, $//')

    echo "$combined_selectors"
}

# Read the input file line by line
while IFS= read -r line || [ -n "$line" ]; do
    # Remove leading and trailing whitespace
    line=$(echo "$line" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')

    if [[ "$line" =~ ^@media ]]; then
        # If it's a media query, start a new media query block
        flush_selector
        media_query=true
        current_selector=$(echo "$line" | sed 's/{//g')
        parent_selector_stack=("$current_selector")
        continue
    elif [[ "$line" =~ ^[.#\[][^,{]*\{$ ]]; then
        # Handle new selector block or nested multi-selector
        if [ "$is_multi_selector" = true ]; then
            # If multi-selector handling was active, expand them with the current child selector
            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

        # Flush the previous selector before processing the new one
        flush_selector

        # Extract the current selector and add it to the multi-selector array if needed
        new_selector=$(echo "$line" | sed 's/{//g' | tr -d '\n')

        if [ "$is_multi_selector" = true ]; then
           

Читать полностью…

r_bash

YAML manipulating with basic tools, without yq

The problem. I have a YAML file with this:

network:
version: 2
renderer: networkd
ethernets:
wifis:
wlx44334c47dec3:
dhcp4: true
dhcp6: true

As you can see, there is an empty section ethernets, but we could also have wifis section empty. This is invalid structure and I need to remove those empty sections:

This result:

network:
version: 2
renderer: networkd
wifis:
wlx44334c47dec3:
dhcp4: true
dhcp6: true

can be achieved easily with:

yq -y 'del(.network.ethernets | select(length == 0)) | del(.network.wifis | select(length == 0))'

But I want to achieve the same with sed / awk / regex. Any idea how?

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

Читать полностью…

r_bash

Simple script to integrate chat-gpt into bash

Will try this post again now...

I love messing around and modifying bash, my latest project has been to integrate chat-gpt into the command line. wanted to just type a normal command and have it reply whenever i needed/wanted it too. It became a little more complicated as i went on and had to add various functions such as save session memory for it to be effective-also added a simple menu. Works ok on my system but just after a little feedback please **you will need your own openai API key**

https://github.com/Fenlanddavid/arch-ai

Ps- did attach image but does not seem to be displaying.....

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

Читать полностью…

r_bash

HELP!

I'm trying to make a simple OS that uses BASH and coreutils as a base.
I searched and asked to chatgpt how to compile it to a unknown os and basically everything went wrong.

btw, i'm on windows 11 with nasm,gcc, mingw, msys2 and Arch WSL.
Can someone help me?

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

Читать полностью…

r_bash

Collections of very useful Bash Functions

I use Bash a lot working with applications, systems, containers or networks, mgmt & integration.

I've found and frequently use a few really useful Bash Github repositories with collections of Bash "Functions" that you can use in your own Bash scripts.  

I've learned  a lot from them and have to say my Bash scripts now have capabilities I'd probably never been smart enough to create myself. In your own script(s) you just "source" the file you create or download from the following URLs: 

I am sharing this info in case someone else finds them useful.

# Collections of Functions for Bash

GUI'sEasyBashGUI: **https://github.com/BashGui/easybashgui/blob/master/docs/install.md**

>Simplified way to code bash made GUI frontend dialogs!

Script-Dialog: **https://github.com/lunarcloud/script-dialog?tab=readme-ov-file**

>Create bash scripts that utilize the best dialog system that is available. Intended for Linux,
but has been tested on macOS and Windows, and should work on other unix-like OSs.

If it's launched from a GUI (like a .desktop shortcut or the dolphin file manager)
\- it will prefer kdialog in Qt-based desktops and zenity in other environments.

If neither of those are available
\- then relaunch-if-not-visible will relaunch the app in a terminal so that a terminal UI can be used.

If it's launched in a terminal
\- It will use whiptail or dialog

If neither of those are available, then it will fallback to basic terminal input/output with tools like read and echo

Collections of General Bash FunctionsBashMatic: **https://github.com/kigster/bashmatic**

>Bashmatic is a BASH framework, meaning its a collection of BASH functions (almost 900 of them) that, we hope, make BASH programming easier, more enjoyable, and more importantly, fun \- due to the library’s focus on providing the developer with a constant feedback about what is happening, while a script that uses Bashmatic’s helpers is running.

Bash-Concurrent: **https://github.com/themattrix/bash-concurrent**

>A Bash function to run tasks in parallel and display pretty output as they complete.

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

Читать полностью…

r_bash

What is the purpose of /dev/tty ?

Please hear me out. So, reading about special devices like tty, tty0, pst1...pstn I understand in loose terms that terminal emulators (like the ones you bring up with ctrl+t ) are special devices under /dev/pts/<some_number> . Now, tty0 appears to be a terminal associated to kernel itself (I still don't know what that means). But tty? I only know that it points to the current terminal being used but I don't know exactly what to make of that and how it pertains to the following humble little snippet I wrote:

#!/bin/bash

while read -r filename
do
echo "Current fie: ${filename}"

read -p "Delete ${filename} ? " response < /dev/tty

if [[ $response = 'y' || $response = 'Y' ]]
then
echo "response was yes"
echo "Deleting ${filename}"
tar vf pdf_files.tar --delete "${filename}"
echo
else
echo "skipping"
fi
done < <(tar tf pdf_files.tar)

You'll notice that in the line that contains the read -p command I had to redirect input from tty. I had chatGPT suggest that to me after many failed attempts at getting my little script to run correctly because I didn't understand why $response variable would be automatically set to something and the script wouldn't even wait at the prompt for me to enter something. I had my eyes OPENED today -- and in a frustrating way -- as to how many little tricks and things one must take into account when learning bash scripting.

So, going back to the script, why did I even need to do that or more importantly, WHEN do I need to do that kind of trick again?

p.s. I've been learning from time to time bash scripting for like the past 3 o 4 months and I know I have to learn a lot more, but Jesus, the journey feels never-ending.

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

Читать полностью…

r_bash

New to Bash Scripting and Sysadmin? Check Out My Tool: Linux Console Manager (Open Source - All Feedback Appreciated)

Hi r/bash community!

I'm a new Redditor, and I wanted to share a simple bash script I've been working on called Linux Console Manager. I'm actually from Korea and haven't used Reddit much before, but I'm excited to share this with you all!

As someone who occasionally does Linux system administration, I found myself constantly typing the same commands over and over. So, I created this little tool to streamline those common tasks and make my life a bit easier. I thought it might be helpful for others too, especially those who are newer to Linux or just want a quicker way to manage their systems from the terminal.

Key Features:

System Monitoring: Tired of typing top, free -m, df -h, and ifconfig separately? Linux Console Manager gives you a quick, consolidated overview of essential system metrics like CPU usage, memory consumption, disk space, and network stats in one place!
Service Control: Managing system services like Apache, Nginx, or MySQL can be a bit tedious with systemctl. This script lets you easily start, stop, restart, and check the status of your services through a simple menu. No more struggling to remember those commands!
Process Management: Quickly identify and manage running processes. Need to find that resource-hogging process? Linux Console Manager helps you easily find and even kill processes directly from the script.
Network Utilities: Basic network troubleshooting tools like ping and traceroute are built right in, making it convenient for quick network checks.
\[Add other key features of your script here - Be specific! For example:\]
User Management: Easily add, delete, or modify user accounts with simple menu options.
Log Viewing: Quickly access and view common system logs like /var/log/syslog or /var/log/auth.log.
Package Management (Debian/Ubuntu based): Simple interface for updating packages or searching for new ones using apt.

The script is completely open source and available on GitHub: https://github.com/forsys02/linux\_console\_manager

https://preview.redd.it/1avm3lnnsune1.png?width=458&amp;format=png&amp;auto=webp&amp;s=25a753e1bb074cbc0dbd4e8f4a93c8caa952de5f

I would love for you to try it out and give me your feedback! I'm really open to suggestions for improvement and new features. Specifically, I'm curious about:

Is the menu structure intuitive and easy to navigate?

Are there any features you think are missing or could be more useful?
Do you find it helpful in your daily system administration tasks?
I'd especially appreciate feedback from both experienced sysadmins and those who are newer to Linux. Let me know what you think!

This is a project I'm working on in my spare time, and I hope it can be helpful to others in the community.

Thanks for checking it out! Happy scripting! 😊



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

Читать полностью…

r_bash

> bib (a Bible reference tool for CLI)

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

Читать полностью…

r_bash

HELP Please. The while loop is running before SSH has ended completely.

https://preview.redd.it/6yv2drjx0ine1.png?width=687&amp;format=png&amp;auto=webp&amp;s=afaef25c281cf083cbc5685e7552203ef6375e58

https://preview.redd.it/f622kvze1ine1.png?width=596&amp;format=png&amp;auto=webp&amp;s=943b60670c3d4ce0e63d5e143673302c5db97f08

So I wrote this code to automate ssh and storing passwords in OverTheWire challenge.
Problem : When I press Enter nothing happens.
What I think the problem is : The while loop starts running before the SSH ends completely. Even GPT did not help.
Can someone please tell me wat the issue is, and how to fix it?

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

Читать полностью…

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

Читать полностью…

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

Читать полностью…

r_bash

${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

Читать полностью…

r_bash

Help me improve my MySQL backup script

So I've got a working backup script for backing up MySQL databases on different database servers. The script 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 >

Читать полностью…

r_bash

Why is "set -e" not the default or not left in scripts after creation?

"set -e" as most of you know will exit a script on error. It (and other set commands) is often used in a script 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 script is in production and there is an error, you would want the script 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

Читать полностью…

r_bash

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

Читать полностью…

r_bash

want to print only the real time

time ./prog

real 0m0.004s
user 0m0.001s
sys 0m0.003s

but i only want to print the first line

real 0m0.004s or 0m0.004s

is there any way ?```

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

Читать полностью…

r_bash

Shunpo: A Bash Tool for Quick Directory Navigation

Hi fellow redditors. I recently wrote a minimalistic navigation tool for bash. A few of my colleagues liked it, so I thought I'd make the repository public and share it. Here's a video demo-ing it . I hope it helps speed up your navigation!

Link to the repo:
https://github.com/egurapha/Shunpo

Link to a youtube video:
https://www.youtube.com/watch?v=TN66A3MPo50

https://i.redd.it/o2t12nrceefe1.gif

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

Читать полностью…

r_bash

Chatgpt in bash

Hi, first post here so please go easy..... !

I love modifying bash, and have recently thought about having Chatgpt work directly from bash by just typing a simple command whenever needed/wanted. As a result i have created the following, it seems to work fine and have added a menu to it for added functionality.

Feedback would be most welcome please - https://github.com/Fenlanddavid/AI-command/blob/main/gpt.sh

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

Читать полностью…

r_bash

Importance of checking IFS

I just wanted to spread a word about importance of explicitly defining and assigning values to IFS.

After years of scripting in bash in Ubuntu i never thought of non standard IFS values in other linux based operating systems.

Few minutes ago figured out why some of my scripts weren’t working properly in openwrt. IFS in openwrt contains only /n newline character vs tab space and newline.

Can be checked by looking into environment via set (printenv is not installed by default) or simply by echoing IFS and piping into cat: echo “$IFS” | cat -A

Hope this will save someone down the road from wasting hours on debugging.

My scripts weren’t working simply copied to openwrt as they were working on Ubuntu and didnt show any issues at first glance. I want to pinpoint here that i didnt write in openwrt environment or else i would have checked IFS. From now on i will make a habit to assign it right after the shebang.

Thanks.

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

Читать полностью…
Subscribe to a channel