r_bash – Telegram
No redirect to variable?

There is probably a reason this is not a thing but it seems useful.

You can redirect stdin from text/a variable:

cmd <<< text

but it seems logical to implement something like the following for simple and flexible output redirection to variables:

cmd 1>>>var1 2>>>var2

Has this already been considered?

The best solution I have to capturing output this flexibly is this monster cobbled together / copied from stackoverflow:


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

catch MYSTDOUT MYSTDERR './useless.sh'
echo "exit: ${?} stdout: ${MYSTDOUT} ${MYSTDERR}"

https://redd.it/135mdqt
@r_bash
how do i write stuff to a line in a file?

so i am making a noscript that sets a wallpaper and color scheme and i want it to replace the color scheme i have set in my .bashrc for the one i picked in the program how would i do so?

https://redd.it/135n7qt
@r_bash
Loop only executes on first Line

while IFS="=" read -r filename url; do
if [ ! -f "$folder/$filename" ]; then
echo "Downloading $filename from $url..."
wget -O "$folder/$filename" "$url"
echo "Download complete."
else
echo "The file $filename already exists in $folder. Skipping download."
fi
done < "$noscript_folder/urls.txt"

This is supposed to work on a File like:

name=url
name=url

but it seems to only be running on the first line.

What am i doing wrong ?

https://redd.it/135td5q
@r_bash
Help with using mktemp files and gnuplot

Please could someone help me with using gnuplot. I am just trying to practise the basics but wanted to use mktemp instead of the .csv file (which the noscript does work with).

This is my noscript:

#!/bin/bash

# Define the input and output files
datafile=$(mktemp)
cat data.csv > $datafile

head $datafile
echo ""
echo ""
echo "Using data file: '$datafile'"
echo ""

# Use Gnuplot to create a graph
gnuplot -e "set terminal pngcairo; set datafile separator ','; set output 'output.png'; plot '$datafile' using 1:2 with lines"

echo ""
echo "Using data file: '$datafile'"

rm $datafile

This is the output:

1,5
2,3
3,2
4,7
5,4

Using data file: '/tmp/tmp.KmYFhmSjTl'

line 0: warning: Cannot find or open file "/tmp/tmp.KmYFhmSjTl"
line 0: No data in plot


Using data file: '/tmp/tmp.KmYFhmSjTl'


The mktemp file seems to be there both before and after the gnuplot is used so I am confused.

If it matters I am using cygwin on Windows.

Thanks for your help in advance!

https://redd.it/135uyro
@r_bash
Explain the Bash reference entries about `$@' and `$*'

I'm struggling with understanding the language of the following denoscriptions of these two parameters.

`$@`

>Expands to the positional parameters, starting from one. In contexts where word splitting is performed, this expands each positional parameter to a separate word; if not within double quotes, these words are subject to word splitting. In contexts where word splitting is not performed, this expands to a single word with each positional parameter separated by a space. When the expansion occurs within double quotes, and word splitting is performed, each parameter expands to a separate word. That is, "$@" is equivalent to "$1" "$2" …. If the double-quoted expansion occurs within a word, the expansion of the first parameter is joined with the beginning part of the original word, and the expansion of the last parameter is joined with the last part of the original word.

`$*`

>Expands to the positional parameters, starting from one. When the expansion is not within double quotes, each positional parameter expands to a separate word. In contexts where it is performed, those words are subject to further word splitting and pathname expansion. When the expansion occurs within double quotes, it expands to a single word with the value of each parameter separated by the first character of the IFS special variable. That is, "$*" is equivalent to "$1c$2c…", where c is the first character of the value of the IFS variable.

1. "each positional parameter expands to a separate word": what is a **word**?
1.1 Is the sentence saying that each parameter is a word or a list of words?

2. "if not within double quotes, these words are subject to word splitting": are these words the ones that came up as the result of the parameter expansion?

3. "this expands to a single word with each positional parameter separated by a space": is this word a part of the positional parameter or preceding the list of the parameters? Could you illustrate this situation?

4. "If the double-quoted expansion occurs within a word, the expansion of the first parameter is joined with the beginning part of the original word, and the expansion of the last parameter is joined with the last part of the original word. ": this is the spot I'm tripping over particularly as I'm having a hard time trying to visualize the scenario. What is "double-quoted expansion within a word" and how does it superimpose on this expansion? Could you give an example?

5. "it expands to a single word with the value of each parameter": what is a single word? What does the expansion look like? The situation is similar to the one referenced in #3.

https://redd.it/1360x8a
@r_bash
Join me



1. I'm working on a new project for WordPress websites that checks which plugins are causing 500 error and turns them off https://github.com/Noam-Alum/wp\_plugins\_checker if someone wants to join me send me DM, an extra pair of hands would be helpful (Note that the project is purely a backend tool since the website is not working... and I wrote it in bash noscripting )
2. feel free to try it and add to the issues tab on github

https://redd.it/136lbxj
@r_bash
Help me understand the possibilities of Bash (best uses, etc)

Little about me and why I'm posting.

Currently studying cyber security and taking a Linux class which is diving a little bit into bash. I have nearly a decade of experience messing with different programming and noscripting languages, mostly from experimenting with web design, game development and some software development, all on a hobbyist level.

I've picked up Bash really fast (obviously by no means on the level of the rest of this subreddit), but my class is coming to an end and I want to see if it's worth continuing learning bash in my free time. It's simple enough understanding the syntax, and I can see some cool tools that I could make with it, but what are the major uses and reasons I should stick with it?

I see things about using Bash for automation, I could see that being useful. I saw a post in this sub about someone writing some back-end code for a web project in bash? Is there any application of this language that could be used for anything GUI oriented? I think of bash as all command line/terminal dirty-work.

Just looking to gain some knowledge on the possibilities of Bash.

TL;DR: College student learning bash, is it worth continuing to learn after my class is over, and what are the best uses for bash noscripting?

https://redd.it/136rgkx
@r_bash
Can Bash replace Perl ?

I don't see many limits on Bash. I wonder if it could replace Perl.

https://redd.it/137ctci
@r_bash
Using variables/functions from one bash file to another

How do I use variables/functions from one file to another file? Pls help.

I was looking for the help on web and all of them recommending source , but source command is executing the File-1 first

https://redd.it/137hrjd
@r_bash
I am on Windows system and trying to chmod +x to a .sh file on VSCODE, git bash terminal. I ran the command, and nothing changed. I read other posts on stack overflow, but I can't understand their solution and explanation. Also, there's no error messages or anything. Just, nothing happens.

note: I am running on my personal laptop so I am the admin. I tried SUDO but got "command not found" instead. This is for a webdev school project btw.

https://redd.it/137swgq
@r_bash
What does % after a variable name do?

I just saw in some legacy noscripts ${vers%} where vers is a version type passed in as an argument in the form of 1.0.1.

I can’t find anything online about it, and in testing it, it doesn’t seem to make a difference if it’s there or not. Any ideas?

https://redd.it/137uxvr
@r_bash
Package version is visible, but package not technically "Installed" from tar.gz.

If I try to extract a pre-compiled package from a tar.gz file to my system's /usr/local/bin and lib, I get the version of the package by doing <package_name> --version.


But I do not see it in the installed list when I do dpkg --list.
dpkg --status <package> says package not installed.

Am I missing something? Reminder that the tar.gz is pre-compiled.

https://redd.it/137xn0d
@r_bash
Need help debugging a noscript that runs a command given by parameter and stores stdout and stderr in variables

I am trying to make a function that will execute a command, store stdout and stderr in variables, check if the command errored or not, and then do something for each case. In this example I'm just echoing "success" or "failure".

This is the current state of the noscript:

#! /bin/bash

execute_with_error_handling() {
step = $1
shift

# Execute the command passed as argument, store stdout and stderr in variables and capture the exit status
exec 3>&1 4>&2
output=$( { "$@" 2>&4 1>&3; echo $? > exit_status.tmp; } 2>&1 )
exec 3>&- 4>&-

# Separate stdout, stderr, and exit_status
stdout=$(echo "${output}" | grep -v '^stderr:')
stderr=$(echo "${output}" | grep '^stderr:' | sed 's/^stderr: //')
exit_status=$(cat exit_status.tmp && rm exit_status.tmp)

if [ "${exit_status}" -eq 0 ]; then
echo "success"
echo $stdout
echo $stderr
else
echo "fail"
echo $stdout
echo $stderr
fi
}

execute_with_error_handling "something that works" echo "hello world"
execute_with_error_handling "something that fails" false


https://redd.it/137zryq
@r_bash
Comparing Float in if statement

I ran into a problem while comparing python version in my noscript, if anyone is familiar with this error please help

My noscript
#!/bin/bash
PYTHON="/usr/bin/python3"
PYTHON_VERSION=$(${PYTHON} --version | grep -Po "(\d+\.)[^ .]+")

if [[ $PYTHON_VERSION -ge 3.11 ]] ; then
echo "python - ok"
elif [[ $PYTHON_VERSION -lt 3.11 ]] ; then
echo "lowest version"
fi

Error i am getting

./2.bash: line 5: [: 3.11: syntax error: invalid arithmetic operator (error token is ".11")**

**./2.bash: line 7: [[: 3.11: syntax error: invalid arithmetic operator (error token is ".11")**

[https://redd.it/138it52

@r_bash
Had a suit made, they asked if I wanted anything embroidered...
https://redd.it/138wgld
@r_bash
How to delete the last blank line in a .txt file?

Hi;
I’m working on a noscript that’s supposed to remove the white spaces and empty lines from a txt file. On a Macbook.
So far, I’ve been able to do that with sed an a regex pattern.
The issue is that it keeps the last blank line in place.
For example, once edited, I’d like my document to show:

- x
- y
- z


and stop there.
Instead, I’m getting:

- x
- y
- z
[blank line here]


Would you know why that happens?
I’ve tried many variations of suggested stackoverflow “sed” solutions, but none of them has been helpful so far.

https://redd.it/1394kj1
@r_bash
Replacing multiple spaces

Hi everyone.
So i have been trying to replace multiple (>2) whitspaces in a file with two dashes. --

I am trying this sed command. But it doesn't work.

 sed`s/[ ][ ]*/\-\-/g` 


Can anyone help?

Example

 Hi             Lokesh kumar             maxi 


Expected out

Hi--Lokesh kumar--maxi

https://redd.it/139e3dr
@r_bash
Finding the current cursor position in bash

The call for getting printed current row and col on screen are easy,(echo -e "^]]6n"), but it is harder to stuff the values into variables, so you can actually use them.

So, having other things to spend my time on, after a little while, I came by these two links, that seem okay for terminals in linux.

This way uses the ncurses tput command, which you should install anyways for this. Stack-exchange

The answer by Dennis Williamson in Stack Overlow post is more finicky but uses only bash, and, returns correct row/col positions for further use with ncurses.

https://redd.it/139hvju
@r_bash