# Data processing commands requiring initialized environment
echo "Processing data..."
```
```link :go_to_menu
file: menu.md
```
In this example, the `data_processing` block relies on `initialize_environment`. When selecting `data_processing`, MDE first executes `initialize_environment` to ensure proper setup before proceeding. The `link` block type enables navigation to `menu.md`, offering a structured and interconnected document system. These attributes make MDE an effective tool for managing complex script sequences and various applications. The automated execution feature via command-line arguments further enhances MDE's role in batch processing and workflow automation.
https://redd.it/18r1ygi
@r_bash
Use MarkdownExec to interactively select and execute fenced code blocks in markdown files.
The ["MarkdownExec (MDE)"](https://github.com/fareedst/markdownexec) application is a tool for executing `bash` code blocks extracted from Markdown (MD) documents. MDE operates in a Ruby and Ubuntu environment, employing Bash for script execution.
**Platform Specifications:**
- **Base Platform:** Ruby for Ubuntu systems.
- **Shell Integration:** Incorporates Bash for executing scripts.
- **Configuration and Metadata Management:** Utilizes YAML for managing configuration and metadata.
- **User Interface:** Boasts a terminal interface with ANSI colors for enhanced readability and engagement.
**Core Functionalities:**
1. **LLM Output Integration:** MDE adeptly reads MD files from LLMs, focusing on identifying and processing `bash` fenced code blocks.
2. **Document Processing and Menu Interface:** Transforms MD text into an accessible format. It distinguishes fenced code blocks, converting them into interactive menu items akin to hyperlinks for straightforward navigation.
3. **Interactive User Experience:** Offers keyboard navigation within the menu, enabling users to execute desired blocks by selecting relevant menu items.
4. **Script Execution and Output Display:** Executes chosen scripts and presents outputs, utilizing ANSI colors for distinction and emphasis. The menu dynamically updates to reflect changes post-execution.
5. **Application Use Cases:** Suited for executing automated scripts from LLM recommendations, serving as an interactive educational platform, and assisting developers in rapid prototyping.
6. **Automated Execution via Command Line Arguments:**
- MDE supports automated operation by specifying the document and block names in command-line arguments.
- Designated blocks are executed in order, encompassing navigation and execution within new documents accessed via links or imports.
- When block names are specified, MDE automatically concludes operations post-execution, optimizing batch processes and automation.
**Extended Functionalities:**
1. **Block Naming and Dependencies:**
- Fenced code blocks are identified by type (`bash`) and unique names for effortless referencing.
- MDE accommodates dependencies among code blocks, facilitating execution of prerequisite scripts before the target script.
2. **Code Block Reusability and Document Navigation:**
- **@import Directive:** MDE features an "@import" directive to boost code reusability, allowing the insertion of blocks from other documents at the directive's location, fostering modular coding.
- **Link Block Type:** MDE integrates a "link" block type for seamless document navigation. Execution of this block shifts focus to the specified file, as shown below:
```link :gotomenu
file: menu.md
```
**Customization and Configuration:**
- MDE allows extensive customization, including numerous options for matching source document text, formatting, and coloring output, and personalizing the MDE interface.
- Users can configure MDE settings via configuration files, environment variables, program arguments, and within markdown documents.
**Configuration Sources:**
1. **Environment Variables:** MDE reads the current environment, including configuration in the current and child shells and the current command.
2. **Configuration Files:** MDE accommodates configurations in all shells and supports a dedicated `.mde.yml` file in the current folder, or a specified YAML file.
3. **Program Arguments:** Users can set options directly through command arguments.
4. **Opts Fenced Code Blocks:** MDE recognizes configuration in `opts` blocks, applying settings when the document is loaded or blocks are executed.
**Example Markdown Document:**
These blocks illustrate the use of named and dependent `bash` code blocks and the `link` block type.
```bash :initializeenvironment
# Initial environment setup commands
echo "Initializing environment..."
bash :dataprocessing +initializeenvironment Читать полностью…
The order of the $PATHs matters! Depends on what? And how can I change it?
We are two Debian users, both with the same \~/.profile and \~/.bashrc files with defaults (no changes in them). The only additional line is this in the \~/.bashrc file:
​
export PATH=$PATH:$(xdg-user-dir USER)/.local/bin
By performing the command echo $PATH I get this:
​
/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:/home/ivan/.local/bin
and him have this:
​
/home/sz/.local/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:/home/sz/.local/bin:/home/sz/.local/bin
​
The result is that my binary symlinked in \~/.local/bin is working, not for him.
If him changes the line in its \~/.profile file from PATH="$HOME/.local/bin:$PATH" to PATH="$PATH:$HOME/.local/bin" the result is similar to the mine:
​
/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:/home/sz/.local/bin
​
and the symlink is working as expected.
​
Why all this happens? Why him have to edit the \~/.profile file to reach my results?
https://redd.it/18q8r4q
@r_bash
Bash, printf formatting: accented characters ruin the formatting
Let's take the following code. The first command is echo, just to set the stantards, and the following commands use printf to format the text, as if there was a box around the text:
#!/bin/bash
text=Security
echo " ......................................................"
printf "%5s %-50s %-5s\n" "." "" "."
printf "%5s %-50s %-5s\n" "." "${text}" "."
printf "%5s %-50s %-5s\n" "." "" "."
This is the output:
......................................................
. .
. Security .
. .
Now let's change the text to Portuguese: "Segurança" instead of "Security", mind the "ç":
#!/bin/bash
text=Segurança
echo " ......................................................"
printf "%5s %-50s %-5s\n" "." "" "."
printf "%5s %-50s %-5s\n" "." "${text}" "."
printf "%5s %-50s %-5s\n" "." "" "."
This is the new output:
......................................................
. .
. Segurança .
. .
The dot at the end of the line of "Segurança" is out of place.
The same behavior can be seen with box drawing characters: take a look at what I had to do to achieve the same formatting (%7s instead of %5s), all the definitions in printf had to be changed:
#!/bin/bash
text=Security
echo " ┌────────────────────────────────────────────────────┐"
printf "%7s %-50s %-5s\n" "│" "" "│"
printf "%7s %-50s %-5s\n" "│" "${text}" "│"
printf "%7s %-50s %-5s\n" "│" "" "│"
To get:
┌────────────────────────────────────────────────────┐
│ │
│ Security │
│ │
I was OK with that, with weird trial and error values to draw the box, but when it comes to text, that can contain accented characters or not, in lots of lines, coming from variables, it is an issue.
Thank you in advance!
https://redd.it/18ph0a5
@r_bash
Tree command include contents of a zip file
Is it possible to create a tree of all files in a folder including the contents of zips? Thanks in advance
https://redd.it/18pav4d
@r_bash
Debugging Bash like a Sire - And how to get a StackTrace from a bash script
https://blog.brujordet.no/post/bash/debugging_bash_like_a_sire/
https://redd.it/18p2zr0
@r_bash
Failing assert_success even though script runs successfully and produces correct output
This is probably a very trivial thing, but I just don't know what is happening. I am new to bash (but not completely new to programming), and I am trying to solve a bunch of exercises on [exercism.org](https://exercism.org).
I was solving [this](https://exercism.org/tracks/bash/exercises/two-fer) problem, and for some reason, my script fails the bats test. I am asked to output "One for you, one for me." when script is run with no args. And output the same thing except I'll replace "you" with whatever name I got as an arg. Below is the error message that I get.
```
1..5
not ok 1 no name given
# (from function `assert_success' in file bats-extra.bash, line 409,
# in test file two_fer.bats, line 24)
# `assert_success' failed
#
# -- command failed --
# status : 1
# output : One for you, one for me.
# --
#
ok 2 a name given # skip
ok 3 another name given # skip
ok 4 handle arg with spaces # skip
ok 5 handle arg with glob char # skip
```
And this is my script:
```
#! /bin/bash
[[ -z $1 ]] && echo "One for you, one for me."
[[ -n $1 ]] && echo "One for $1, one for me."
```
https://redd.it/18ooh6i
@r_bash
interrupt running bash script while keys are pressed
hi, bash scripting noob here looking for help finding a graceful way to interrupt a running script which uses ydotool to simulate keypresses.
I've written a script that looks something like this:
#!/bin/bash
while true; do
YDOTOOLSOCKET="$HOME/.ydotoolsocket" ydotool key 30:1
sleep 30
YDOTOOLSOCKET="$HOME/.ydotoolsocket" ydotool key 30:0
sleep 1
done
the idea being that ydotool holds a key down, releases, then loops back to keep doing that indefinitely
while I can ctrl-c to stop the script, doing that does not seem to release the keypress, so even after the script stops running that key isn't usable unless I sudo pkill ydotoold.
does anyone know of a way to include a keybind in the bash script to interrupt the loop and release all pressed keys or pkill ydotoold directly?
sorry I'm definitely overlooking something, I bet there's a really simple way to do this that I'm missing!!
as an aside, the YDOTOOL_SOCKET bit is a result of the ydotoold daemon being started with the command sudo -b ydotoold --socket-path="$HOME/.ydotool_socket" --socket-own="$(id -u):$(id -g)" to allow the user to run the script without root permissions. does anyone happen to know how this actually works? does the socket path need to be set before every ydotool command or can I just put it once in the script and then ydotool will figure it out?
thanks!
https://redd.it/18oe2gd
@r_bash
Special characters in password
Hi there,
I have a situation where I an running bash script in data pipeline orchestrator. Script retrieves password from orchestrator's env variable and passes it to docker run command. I am not setting the original password, it comes from external source and I do not have any influence on that. The problem is the password can contain any of special characters ' " $ or any other, for ex. #U{7TK('\\'')}4WQ@5@$8_\^uvdrt$%\^(.
So, I can not use '$password' since if password has ' in it it will be broken. I could use "$password" but then if I have $ in password it will be broken again as $ will be treated as beginning of variable.
I have tried escaping special chars with function, but nothing really worked.
The idea is to have:
function escape_chars () {
some function
}
docker run - p $(escape_chars "$password_comming_from_external_source") .....
Any ideas with this?
Thanks a lot!
https://redd.it/18o84nm
@r_bash
Was planning to use the output of a command in a bash script, but I don't know how to deal with the command behavior
I'm fiddling with motd, to be able to display some information at login.
I created this script:
#!/bin/bash
echo "OS: $(lsbrelease -s -d)"
echo "sendmail: $(sendmail -V)"
Fantasizing about this result:
OS: Ubuntu 22.04.3 LTS
sendmail: sSMTP 2.64 (Not sendmail at all)
But got this instead:
OS: Ubuntu 22.04.3 LTS
sSMTP 2.64 (Not sendmail at all)
sendmail:
Then I tried to assign the result of "sendmail -V" to a variable and get it printed:
#!/bin/bash
echo "OS: $(lsbrelease -s -d)"
sendm=$(sendmail -V)
echo "sendmail: ${sendm}"
But it didn't work:
OS: Ubuntu 22.04.3 LTS
sSMTP 2.64 (Not sendmail at all)
sendmail:
Apparently "sendmail -V" is related only to sSMTP.
My actual point here is to learn what is going on, and if it's possible to achieve what I want with this specific kind of output. I kind of see what is going on, I mean, that the output is different than what I see in other commands I've dealt with before, but have no idea how to begin to understand it or to talk about it. I don't really care about displaying the version of sSMTP, it's just overall curiosity now.
UPDATE: $(sendmail -V 2>&1) did the trick, it was going to stderr and I just wouldn't find out by myself. Thank you!
https://redd.it/18n7343
@r_bash
Some useful bash projects for linux servers as a helpdesk tier 2?
What I've made
- log vomitter (thing that vomits logs so that I can use logrotate and verify-I know that's stupid).
- glassfish/payara domain restart command.
That's all that I've needed and I've made them. Is there anything else that I can make? That will be useful to me a helpdesk tier 2?
https://redd.it/18mvtld
@r_bash
why is my script not working?
The script reads domain names from a text file, performs a DNS query using "dig +short NS" for each domain, and checks if "ns14.net" is present in the results. The issue is that, despite correct manual queries returning "ns14.net," the script consistently outputs "Nein" (No) for all domains, even if the correct answer would be Yes.
​
Can someone help?
#!/bin/bash
if $# -ne 1 ; then
echo "Verwendung: $0 inputdomains.txt"
exit 1
fi
inputdomains="$1"
desktoppath="/mnt/c/Users/stiglmmi/Desktop"
outputcsv="$desktoppath/Ergebniss.csv"
echo "Domain;IntX?;dig-Ergebnis" > "$outputcsv"
while IFS= read -r domain; do
result=$(dig +short NS "$domain")
# Debug-Ausgabe
echo "hdaten für $domain"
echo "dig-Ergebnis: $result"
# Überprüfen, ob "ns14.net" in den Ergebnissen enthalten ist
if [ "$result" == *"ns14.net"* ]; then
nx="Ja"
else
nx="Nein"
fi
# Ergebnisse in der Ausgabedatei in separaten Spalten anzeigen
echo "$domain;$nx;\"$result\"" >> "$outputcsv"
# Debug-Ausgabe
echo "Ja/Nein: $nx"
done < "$inputdomains"
echo "Skript abgeschlossen. Ergebnisse wurden auf dem Desktop in 'Ergebniss.csv' gespeichert."
​
https://redd.it/18msvkm
@r_bash
Problem with understaning bash while trying to follow the manual on installing Dolphin AI locally
Hello.
I'm running Ubuntu through WSL, and I have to say that I'm completely new to Unix in general. The problem is: I'm currently following a step-by-step manual on how to install Dolphin from here:
https://erichartford.com/dolphin-25-mixtral-8x7b
Specifically, this part:
> git clone https://github.com/ggerganov/llama.cpp.git
> cd llama.cpp
> make -j
> cd models
> # download whichever version you want
> wget https://huggingface.co/TheBloke/dolphin-2.5-mixtral-8x7b-GGUF/resolve/main/dolphin-2.5-mixtral-8x7b.Q5KM.gguf
> cd ..
> ./server -m models/dolphin-2.5-mixtral-8x7b.Q5KM.gguf -c 16384
Everything went smoothly until the last command. The response I get to it is:
> -bash: ./server: No such file or directory
And yeah… when I run ls, I can actually see that there's no such directory. Since the manual is pretty much step-by-step, I'm kind of stumped. What did I do wrong and how can I fix it? Any help, please?
https://redd.it/18m3vf6
@r_bash
red syntax error in vim
Hi,
Working my way through TLCL by William Shotts and just curious about why this line in VIM has a red syntax error. It seem to work.
https://imgur.com/a/4fs4Sj8
if (( ((INT % 2)) == 0)); then
Also, why is there a space between (( (( but not once between 0)), seems inconsistent.
Thanks
https://redd.it/18kwk3s
@r_bash
script to add numbers stored in environment variables
Hello, I have a task assignment as follows:
Write a shell script that adds the two numbers stored in the environment variables `WATER`
and `STIR` and prints the result.
* `WATER` is in base water
* `STIR` is in base stir.
* The result should be in base bestchol.
the script must only contain two lines including shebang, and use no operators such as &&, ||, ;, sed or bc.
the script i came up with, is as follows:
`#!/bin/bash`
`printf "%x" "$((0x$WATER + 0x$STIR))"`
assuming that the variables WATER and STIR are set, i understand that i first need to convert the variables from base water and stir respectively, to decimal and add these conversions.
I then converted the result from decimal to base bestechol by mapping the decimal values to corresponding values in bestechol. i am stumped here... while i did ask someone for help, and got the following result:
echo $(printf %o $(($((5#$(echo $WATER | tr 'water' '01234'))) + $((5#$(echo $STIR | tr 'stir.' '01234'))))) | tr '01234567' 'behlnort')
i have no idea how the mapping was done to `behlnort.` Additionally, testing this against the given test cases works for one testcase and none of the others.
**edit during typing:**
i just realised while asking that the mapping was arbitrary and mapping to `behlnort` was arbitrary and i could just use `bestchol.` i am so excited to solve it.
https://redd.it/18jxx4s
@r_bash
Use Markdown_Exec to interactively select and execute fenced code blocks in markdown files.
The ["Markdown_Exec (MDE)"](https://github.com/fareedst/markdown_exec) application is a tool for executing `bash` code blocks extracted from Markdown (MD) documents. MDE operates in a Ruby and Ubuntu environment, employing Bash for script execution.
**Platform Specifications:**
- **Base Platform:** Ruby for Ubuntu systems.
- **Shell Integration:** Incorporates Bash for executing scripts.
- **Configuration and Metadata Management:** Utilizes YAML for managing configuration and metadata.
- **User Interface:** Boasts a terminal interface with ANSI colors for enhanced readability and engagement.
**Core Functionalities:**
1. **LLM Output Integration:** MDE adeptly reads MD files from LLMs, focusing on identifying and processing `bash` fenced code blocks.
2. **Document Processing and Menu Interface:** Transforms MD text into an accessible format. It distinguishes fenced code blocks, converting them into interactive menu items akin to hyperlinks for straightforward navigation.
3. **Interactive User Experience:** Offers keyboard navigation within the menu, enabling users to execute desired blocks by selecting relevant menu items.
4. **Script Execution and Output Display:** Executes chosen scripts and presents outputs, utilizing ANSI colors for distinction and emphasis. The menu dynamically updates to reflect changes post-execution.
5. **Application Use Cases:** Suited for executing automated scripts from LLM recommendations, serving as an interactive educational platform, and assisting developers in rapid prototyping.
6. **Automated Execution via Command Line Arguments:**
- MDE supports automated operation by specifying the document and block names in command-line arguments.
- Designated blocks are executed in order, encompassing navigation and execution within new documents accessed via links or imports.
- When block names are specified, MDE automatically concludes operations post-execution, optimizing batch processes and automation.
**Extended Functionalities:**
1. **Block Naming and Dependencies:**
- Fenced code blocks are identified by type (`bash`) and unique names for effortless referencing.
- MDE accommodates dependencies among code blocks, facilitating execution of prerequisite scripts before the target script.
2. **Code Block Reusability and Document Navigation:**
- **@import Directive:** MDE features an "@import" directive to boost code reusability, allowing the insertion of blocks from other documents at the directive's location, fostering modular coding.
- **Link Block Type:** MDE integrates a "link" block type for seamless document navigation. Execution of this block shifts focus to the specified file, as shown below:
```link :go_to_menu
file: menu.md
```
**Customization and Configuration:**
- MDE allows extensive customization, including numerous options for matching source document text, formatting, and coloring output, and personalizing the MDE interface.
- Users can configure MDE settings via configuration files, environment variables, program arguments, and within markdown documents.
**Configuration Sources:**
1. **Environment Variables:** MDE reads the current environment, including configuration in the current and child shells and the current command.
2. **Configuration Files:** MDE accommodates configurations in all shells and supports a dedicated `.mde.yml` file in the current folder, or a specified YAML file.
3. **Program Arguments:** Users can set options directly through command arguments.
4. **Opts Fenced Code Blocks:** MDE recognizes configuration in `opts` blocks, applying settings when the document is loaded or blocks are executed.
**Example Markdown Document:**
These blocks illustrate the use of named and dependent `bash` code blocks and the `link` block type.
```bash :initialize_environment
# Initial environment setup commands
echo "Initializing environment..."
```
```bash :data_processing +initialize_environment
First script with bash
Hey everyone,
I am trying to write a program that calculates the distance between two amino acids in a peptide sequence. I am stuck on an if statement to check if a user's choice of amino acid is found among the amino acid sequence. I know python has a string index like str1[1:n+1\]. But how do I use bash to check if the user's input is among the characters of a string.
​
Code:
​
echo "Enter your peptide sequence: " #To know what sequence to work with from the user
read $peptide_sequence #To sequence as a variable
​
echo "Select atom #1" #to prompt the user to pick an atom among the sequence they selected
read $atom1_select
​
echo "Select atom #2" #same prompt for the next atom
read $atom2_select
\#below I am trying to calculate the distance between the two atoms in the sequence
for amino_acid in ${peptide_sequence:0}
do
if [ $atom1_select==${peptide_sequence:0}\];
then
echo "Atom#1 is $atom1_select"
elif[ $atom2_select==${peptide_sequence:0}\];
then
echo "Atom#2 is $atom2_select"
else
echo "Select an atom from your peptide sequence"
fi
done
https://redd.it/18rpdrx
@r_bash
Help with find and exec function
I am trying to find some images and process them but i am not familiar with bash scripting
https://preview.redd.it/i1dk1gacz88c1.png?width=1717&format=png&auto=webp&s=8a430baf7e70791adefc3c22cb3f1eed1a88a787
https://redd.it/18pvkw5
@r_bash
Writing my first bash script -- looking for hints
Hi everyone, I hope you are all doing great!
The idea: I want to create a script to move all my screenshots from my desktop to a folder I called Screenshots.
What I did:
I needed a way to grep/find the screenshots on my desktop so I had the following
#SCREENSHOT=$(grep -lR "Screenshot" --include="2023" $DESKTOP)
SCREENSHOT=$(find Screenshot.png)
then I wanted to do a simple mv to my new folder
mv $SCREENSHOT Screenshots
The problem:
I realized a screenshot filename is written as follows: Screenshot 2023-XX-XX at XX.XX.XX
When I send it to mv it interprets this filename as 4 different strings. So it thought to use "tr" to remove the whitespaces like this example... it doesn't work at all.
NAME="Screenshot 2023-12-18 at 21.03.25"
VALUE=$(echo $NAME | tr ':space:' '-')
mv $VALUE Screenshots
This doesn't work because VALUE (Screenshot-2023-12-18-at-21.03.25) doesn't exists. so I need to rename my screenshot when it is on my desktop and then use the mv to its new destination. but how can I do so as mv sees my screenshot filename as 4 distinct elements?
btw-- i m on macos
https://redd.it/18pbz0t
@r_bash
First bash script
https://github.com/safesintesi/transient-prompt-bash
https://redd.it/18p4u6y
@r_bash
inserting a file into a another one after/before a matching pattern with and without new lines added
I'd like to update my CHANGELOG.md file by extracting automatically the changes from the repo, using the glab program.
I also would like to retain the format of my CHANGELOG.md so I'd like to insert the output of glab into the CHANGELOG.md after a certain string matching, but unfortunately I'm not able to insert it after a new line.
I've tried the following:
1221 Dec/22 - 01:55:31 sed '/v\d+\d+\d+/i changes.md\n' CHANGELOG.md
1222 Dec/22 - 01:58:54 sed '/## v\d+\d+\d+.*$/i changes.md\n' CHANGELOG.md
1223 Dec/22 - 01:59:05 sed '/## v\d+\d+\d+.*$/i changes.md\n' CHANGELOG.md | less
1224 Dec/22 - 01:59:40 sed '/semver/a \nchanges.md\n' CHANGELOG.md | less
1225 Dec/22 - 02:00:00 sed '/semver/a \n changes.md\n' CHANGELOG.md | less
1226 Dec/22 - 02:00:17 sed '/semver/a \n/a changes.md\n' CHANGELOG.md | less
1227 Dec/22 - 02:01:15 sed '/semver/r changes.md' CHANGELOG.md | less
1228 Dec/22 - 02:01:31 sed '/semver/r \n changes.md' CHANGELOG.md | less
1229 Dec/22 - 02:02:03 sed '/semver\n/r changes.md' CHANGELOG.md | less
1230 Dec/22 - 02:02:20 sed '/semver.org\/\n/r changes.md' CHANGELOG.md | less
1231 Dec/22 - 02:02:30 sed '/semver/r changes.md' CHANGELOG.md | less
1233 Dec/22 - 02:05:00 sed '/semver/a\nchanges.md' CHANGELOG.md | less
1234 Dec/22 - 02:06:55 sed '/semver/a\\\\\nchanges.md' CHANGELOG.md | less
1235 Dec/22 - 02:07:08 sed '/semver/a\\\nchanges.md' CHANGELOG.md | less
1236 Dec/22 - 02:07:18 sed '/semver/a\\nchanges.md' CHANGELOG.md | less
1237 Dec/22 - 02:08:18 sed -e '/semver/a\\\\\nchanges.md' CHANGELOG.md | less
1238 Dec/22 - 02:08:34 sed -e '/semver/a\nchanges.md' CHANGELOG.md | less
1239 Dec/22 - 02:08:49 sed -e '/semver/a \nchanges.md' CHANGELOG.md | less
1240 Dec/22 - 02:09:04 sed -e '/semver/a \\nchanges.md' CHANGELOG.md | less
1241 Dec/22 - 02:09:23 sed -e '/semver/a \\nchanges.md' CHANGELOG.md | head -n 20
1242 Dec/22 - 02:09:33 sed -e '/semver/a \\n changes.md' CHANGELOG.md | head -n 20
1243 Dec/22 - 02:09:42 sed -e '/semver/a \\\\\n changes.md' CHANGELOG.md | head -n 20
1244 Dec/22 - 02:09:50 sed -e '/semver/a \\\n changes.md' CHANGELOG.md | head -n 20
1245 Dec/22 - 02:12:33 sed '/semver/{r changes.md a\}' CHANGELOG.md | head -n 20
1246 Dec/22 - 02:14:06 sed '/semver/{\n e cat changes.md}' CHANGELOG.md | head -n 20
1247 Dec/22 - 02:14:18 sed '/semver/{e cat changes.md}' CHANGELOG.md | head -n 20
1248 Dec/22 - 02:14:47 sed '/semver/e cat changes.md' CHANGELOG.md | head -n 20
1249 Dec/22 - 02:15:13 sed '/semver/r \n e cat changes.md' CHANGELOG.md | head -n 20
1250 Dec/22 - 02:15:28 sed '/semver/r \n /e cat changes.md' CHANGELOG.md | head -n 20
1270 Dec/22 - 15:28:05 sed '/semver\.org\/\n/r changes.md' CHANGELOG.md | head -n 20
1272 Dec/22 - 15:28:13 sed '/semver\.org\/\n/r changes.md' CHANGELOG.md | head -n 20
semver.org line, nothing seemed to work. Also tried with Bard and ChatGPT...sed, I just thought it would have been the right tool for the job.
awk matching pattern and print until the next double empty blank line?
how can i print match string until the next double empty line?
# alfa
AAA
BBB
CCC
# bravo
DDD
EEE
FFF
# charlie
GGG
HHH
III
This command works but it only for the first matching empty line.
I need something that will match the next double empty line
awk '/bravo/' RS= foobar.txt
# bravo
DDD
EEE
Wanted final output
# bravo
DDD
EEE
FFF
https://redd.it/18om72p
@r_bash
Different behaviour in running script through bash and sh
This might be very naive.
I was testing something when I noticed this behaviour.
My script test.sh contains the following line-
source .bashrc
When I run it like bash test.sh it works but when I run it like sh test.sh, it gives the error -
sample.sh: line 1: source: .bashrc: file not found
I wanted to know the reason for it.
https://redd.it/18o95v0
@r_bash
Bash script as a service - but able to display STDOUT on request?
Hi guys
I have written a series of Bash scripts that run as services on my home server.
But is there a way, I can get the running service to come to the foreground and display STDOUT (and maybe evend STDERR) as if it was running as a standard script.
Like when I run a script in Screen, I can always just type screen -r (pid) and it pops to the front again, then CTRL+A D and it goes back to the background. I want to do something like this but as a service?
Edit: Reading the man page for screen. I see there is an option -S that allows me to name a screen. If I t was to put under exec in my service file: screen -S nameofservice ./nameofscript.sh would that run the service in a screen session then I can use screen -r nameofservice to bring it to the foreground.
https://redd.it/18ni4n6
@r_bash
Sed help needed!
I asked ChatGPT to help me create a function to help me parse YT URL's. FYI a YT URL consists of a string where it can have the video id alone, or the video id plus extra metadata. The video id is 11 characters long. Anything after it, after a "&" or "?" character is just extra metadata These 3 sample YT URL's are the exact same video: youtu.be/watch?=12345678901 and `youtu.be/watch?=12345678901&pp=abcdefghij%3D%3D` and `youtu.be/watch?=1234678901?si=abcd1234`
The code that ChatGPT gave me was: youtube_url=$1video_id=$(echo "$youtube_url" | sed -n -e 's/^.*[?&]v=\([^&]*\).*$/\1/p; s/^.*`youtu.be`\/\([^?]*\).*$/\1/p')
if [ -n "$video_id" ]; thenecho "$video_id"
else echo "Video ID not found in the given URL."
This works, basically. But my problem is that it only really works correctly with URLs of the first type. This will echo the correct video id,12345678901 and end. Which is the correct response I want with all 3 URLs. With the second or third type of URLs it will echo a Job id# then12345678901 and waits for me to hit ENTER before echoing a job id# with +Done and the function name. This is an unwanted result as I just want the result and no job id#s or having to press ENTER or an echo of the function name. What do I have to add for this to happen???
https://redd.it/18n7utu
@r_bash
Can I have a script output live/changing data?
Say I'm running a few dozen processes 3 at a time. I'd like to have the output show which is running, and update the existing output it to say when they're done. Is this possible?
eg:
Job 1: running
Job 2: running
Job 3: running
which would then change to:
Job 1: completed
Job 2: completed
Job 3: running
Job 4: running
Job 5: running
I'd prefer to do this without clearing the whole terminal.
https://redd.it/18mwl2z
@r_bash
How to handle empty values in bash
I'm trying to obtain the value of the "DEFAULT SOURCE VERSION" column for both rows. The issue arises from the fact that the "DEFAULT SOURCE VERSION" is located in the 4th column. However, there is an issue with the "lawn-mower" row where the column "PREVIOUS DEFAULT VERSION" is empty. As a result, when accessing the 4th column using a bash command, the correct value (1.25.0) is retrieved for "earth-shaker," but for "lawn-mower," it returns 1 due to the absence of a value in the "PREVIOUS DEFAULT VERSION" column. The expected output is "1.25.0" for both rows, but the actual output shows "1.25.0" for "earth-shaker" and 1 for "lawn-mower.
Tabular data
The columns here are not separated by any specific delimeter as versions may vary. I tried to use bash arrays to solve this issue but i was not able to resolve this issue .
​
OVERALL:
Expected output :
1.25.0
1.25.0
My Output:
1.25.0
1
​
Tabular data
NAME DEFAULT VERSION PREVIOUS DEFAULT VERSION DEFAULT SOURCE VERSION INSTALLED VERSION INSTALLED SOURCE VERSION UNKNOWN ERROR PENDING SUCCESS/DONE
earth-shaker 1 0 1.25.0 1 1.25.0 0 0 0 1
lawn-mower 1 1.25.0 1 1.25.0 0 0 0 1
https://redd.it/18m64pb
@r_bash
Tips for beginners
I’ve been thinking of starting learning bash scripting first quite a while and now that I have the time I want to know how to start. Can someone please suggest any links or materials?
https://redd.it/18lijx4
@r_bash
Why i can't get output when i use subshell?
I'm trying to solve OverTheWire's natas16 challenge its a simple command injection '$()' chars aren't restricted so i can run code through input. Tried something like "$(grep <token> /etc/natas_webpass/natas17)easters" as input it works but there is no output. When i read write-ups about natas16 similar ways are shared. If token in password file there is no output if not easters out. I can't understand why i can't get output when grep find something in password file?
lab is: http://natas16.natas.labs.overthewire.org/
username:natas16
password: TRD7iZrd5gATjj9PkPEuaOlfEjHqj32V
source code:
```<?
$key = "";
if(array_key_exists("needle", $_REQUEST)) {
$key = $_REQUEST["needle"\];
}
if($key != "") {
if(preg_match('/[;|&\\
'"\]/',$key)) {
print "Input contains an illegal character!";
} else {
passthru("grep -i "$key" dictionary.txt");
}
}
?>```
https://redd.it/18kdsef
@r_bash
Sometime "wc -l" is not enough to count all the outputs of a script, how can I solve?
I have this function to install programs using several scripts from a database:
function installarg(){
if grep -q 'read -r' ./$arg; then
$SUDOCOMMAND ./$arg | less -E
else
outputlines=$($SUDOCOMMAND ./$arg | wc -l)
for i in "$outputlines"; do
printf '\033['$i'A\033[K'
done
for i in "*"; do
echo $* 2>&1
done
fi
}
All this function should do is to replace extra outputs of the installation script.
During the installation, the progress bar of "wget" is shown, and each line is replaced depending on the state of the installation script.
Normally, on the same line, appear:
◆ RUNNING THE INSTALLATION SCRIPT FOR LXTASK
then something like this:
TaskManager-.0.1.9 100%===================> 2,97M 7,11MB/s in 0,4s
and finally
◆ "LXTASK" INSTALLED (4 MB OF DISK SPACE)
That said, this is what actually does:
-----------------------------------------------------------------------
>> START OF ALL INSTALLATION PROCESSES <<
-----------------------------------------------------------------------
◆ "FFWA-NETFLIX" INSTALLED (1 MB OF DISK SPACE)
index.html.tmp 100%===================> 2,94M 5,82MB/s in 0,5s
index.html.tmp 100%===================> 802 --.-KB/s in 0s
◆ "PALEMOON" INSTALLED (110 MB OF DISK SPACE)
◆ "FFWA-WHATSAPP" INSTALLED (1 MB OF DISK SPACE)
◆ "LXTASK" INSTALLED (4 MB OF DISK SPACE)
-----------------------------------------------------------------------
>> END OF ALL INSTALLATION PROCESSES <<
-----------------------------------------------------------------------
and this is what I'd like to see instead
-----------------------------------------------------------------------
>> START OF ALL INSTALLATION PROCESSES <<
-----------------------------------------------------------------------
◆ "FFWA-NETFLIX" INSTALLED (1 MB OF DISK SPACE)
◆ "PALEMOON" INSTALLED (110 MB OF DISK SPACE)
◆ "FFWA-WHATSAPP" INSTALLED (1 MB OF DISK SPACE)
◆ "LXTASK" INSTALLED (4 MB OF DISK SPACE)
-----------------------------------------------------------------------
>> END OF ALL INSTALLATION PROCESSES <<
-----------------------------------------------------------------------
The "wc -l" command is not enough for this, I'd like to list all the outputs of the script and grab them in the count to replace them (see that index.html.tmp 100%[===================>] and the doubled space between ffwa-whatsapp and lxtask).
How can I solve this?
PS: previously I had to use less -E, but now I'd like to show all the outputs without having to use less.
Please, help me.
https://redd.it/18jw9zi
@r_bash