r_bash – Telegram
Why does "cat | ls" behave like that?

Hello everyone,


I am doing a project that consists on replicating bash (some of its basic behavior) in C.
I am still on the early stages of the project, analyzing how bash behaves with different inputs.
The point is that I was playing with some basic commands and its combinations through pipe (|) operator, and when I tried "cat | ls" I was kind of expecting just the ouput of ls, but instead got:


bash-4.4$ cat | ls
file1 file2 file3


And it just holds there awaiting for input. Then after just enter it stop the execution with a result of:

bash-4.4$ cat | ls
file1 file2 file3

bash-4.4$

It feel like bash its prioritizing ls over cat, does anyone know what is going on underneath?


Thanks!




https://redd.it/1apxpef
@r_bash
some help with this example noscript

So lets say I have 50 different noscripts I need to run.
For the last section that checks error codes -ne 0, is there a way
I can some how put this in some loop that checks $exitcode1-50
without having to state each variable.

-----------------------
#!/bin/bash

testnoscript1="test1.sh"
testnoscript2="test2.sh"
testnoscript3="test3.sh"

# Run the first test noscript and store the exit code
$testnoscript1
exitcode1=$?

# Run the second test noscript and store the exit code
$testnoscript2
exitcode2=$?

# Run the third test noscript and store the exit code
$testnoscript3
exitcode3=$?

# Check the exit codes and exit with error 1 if any of them is non-zero
if $exit_code_1 -ne 0 || $exit_code_2 -ne 0 || $exit_code_3 -ne 0 ; then
exit 1
fi

https://redd.it/1aqzji0
@r_bash
Using JQ to return the index number for every occurrence of an element in an array

Take the following example. How can I use jq to return an array of the embeds index number for each occurance of the field element in the Json? I.e. [0 0 1 2 2 2]

{
"embeds":
{
"name": "Name1"
"field": "Field1"
"field": "Field2"
}
{
"name": "Name3"
"field": "Field3"
}
{
"name": "Name1"
"field": "Field1"
"field": "Field2"
"field": "Field3"
}

}

​

https://redd.it/1ard3he
@r_bash
Interested in using zsh to run a program that requires bash...

I feel like this would be an easy lift for an experienced linux user but I haven't done more than a few google searches on this yet...

This app requires a settings.sh noscript sourced before you use it, so I cant just alias all the cmds with `bash -c $app`. I need the bash shell env setup by the settings.sh, then I need to be able to have zsh push all cmds starting with that apps name to the shell that has been setup.

I feel like there's got to be a way to have cmds run from zsh that open a bash shell and then specify in .zshrc (with aliases) which cmds get sent to that bash shell...

also, the only reason this particular linux box exists is to run this app, so I don't care if the solution somehow lightly interferes on other use cases...

thanks for reading.

​

https://redd.it/1ard6yd
@r_bash
Continuously update display while waiting for user input

I want to write a little bash noscript that continuously updates some values on the screen (in my case, packets per second), while also waiting on user input from a small menu of options presented to the user. In the year of our lord 2024, is there not a better solution than an infinite while loop and read with a 1 second timeout? Is this something dialog(1) could help me do? Thank you.

https://redd.it/1asksmi
@r_bash
I developed a program that interacts with a shell but accidentally I entered a backslash

After I entered \ the shell is stuck, expecting me to press enter. Is there any way to exit this?

As I interact via a text field I don’t know how I could simulate an enter keypress and \n does not work

https://redd.it/1askmtv
@r_bash
How many disk reads are too much?

Hello, I designed a daemon that has to periodically retrieve a bit of information from disk, and I want it to be as realtime as possible, but that would imply may be too often hard disk read operations.

An alternative to file read could also be an xattrs read.

What time lapse is sane for a read operation or a read xattrs operation?

https://redd.it/1at6kns
@r_bash
Script update announce: apt-undo

Hey guys. Over two years ago I published the apt-undo bash noscript on github and announced it in this sub. Recently I overhauled it and implemented some features it lacked. I guess you'll like the brand new -t option.

​

Notice: examples in the README are reproducible now.

https://redd.it/1atydbh
@r_bash
Pass output of python/js file as data when using curl

I have been trying to use curl for a post request but the data I am trying to use is from another noscript file (say output of js or python file).

Something like this :

curl -X POST -H "Content-Type: application/json" -d '$(python test_data.py)' http://localhost:4000/curl-check

in the data section I want to send the json dump from python file, is this possible ?


Thanks.

https://redd.it/1aum73d
@r_bash
50 useful noscripts you use as a devops?

I know putting json keys to lowerCase and ordering them by alphabetical order is a useful noscript, but not necessarily for devops, but I am trying to make a list of useful noscripts I might have to use at work.

https://redd.it/1av0gp6
@r_bash
am playing a gameshel and got stuck here

You are tasked with controlling the pneumatic tube system used by the Mail Service of the Pierre-Dansereau Science Complex. The system connects three pavilions of the complex as follows:

​

E --->--- PK --->--- SB --->--- CO

\---> transport direction --->

​

This diagram indicates that all cylindrical shuttles transported by the system are inserted at the entrance E and first pass through PK. Those that do not stop at PK continue to SB, and those that do not stop at SB are subsequently routed to CO.

​

The command "depart" inserts shuttles into the system at entrance E. In particular, it generates a sequence of lines in the format:

​

CP message

​

where "CP" is a postal code written on the inserted shuttle and "message" is a string of lowercase letters that form the content of the shuttle. The postal code is a capital letter with the following meaning:

​

the shuttle stops at PK if the postal code is the letter "P",

the shuttle stops at SB if the postal code is the letter "S",

the shuttle stops at CO otherwise.

​

To control the system, you must produce:

​

a "pk" manifest that contains shuttles reaching PK,

an "sb" manifest that contains shuttles reaching SB,

a "co" manifest that contains shuttles reaching CO.

​

For example, if the output of "depart" is:

​

X bonjour

P il neige

S adieu

P il pleut

​

then the "pk" file will contain:

X bonjour

P il neige

S adieu

P il pleut

​

the "sb" file will contain:

X bonjour

S adieu

​

and the "co" file will contain:

X bonjour

​

Constraints

​

You are allowed "one command line" for this task.

https://redd.it/1av59p1
@r_bash
What boredom does to a man | replicating the old TVs 'no signal' color bars in BASH
https://redd.it/1av6c7b
@r_bash
am stuck in a gameshell (part 2) btw i cant use if while ... and cant use && ; .... and thx for the help



Objective

Bathilde has received several ASCII puzzles for her party. An ASCII puzzle consists of multiple pieces, where each piece contains a line of ASCII art.

Unfortunately, a sprite has decided to mix up the pieces of all her puzzles. Help Bathilde find the pieces of her favorite puzzle and complete it.

The pieces of her favorite puzzle are located in the files in the "art" directory with a filename:

that starts with the letter "m"
where the 2nd character is an even number
where the 3rd character is an odd number
where the 4th character is the letter "n"
where the 5th character is random
where the 6th character is a question mark "?"
and which ends with a backslash ""

Each piece contains a line in the format:

mathematicaCopy code

N TEXT

where "TEXT" is the N-th line of the puzzle. To complete the puzzle, simply put the pieces in the correct order. For example, if the pieces are:

Copy code

2 | | 1 +-+ 3 +-+

the completed puzzle will be:

Copy code

1 +-+ 2 | | 3 +-+

Once you have completed the puzzle, save the result in the file "ct".

Constraints

You are allowed only "one command" and "zero or one redirection".

https://redd.it/1av8f21
@r_bash
Commands executed in a subshell meaning?

My understanding of what a subshell is comes from

https://www.gnu.org/software/bash/manual/bash.html#Command-Execution-Environment

which says that “a subshell is a copy of the shell process,” and that “commands are invoked in a subshell environment that is a duplicate of the shell environment…”

This implies that a subshell looks exactly like the parent shell aside from some minor differences which is different from creating a new shell since the new shell won’t see the previous shell’s shell variables.

So for example a bash noscript is not run in a subshell since it creates a new shell.

Assuming my understanding of what a sub shell is is correct, my confusion stems from

https://www.gnu.org/software/bash/manual/bash.html#Pipelines which says that

“each command in a multi-command pipeline, where pipes are created, is executed in its own subshell, which is a separate process.”


Is a new subshell created for every command “invoked in a subshell environment,” or does it work where builtins or other bash stuff cause a subshell to be created, but a subshell is not created for simple commands and they are the same as running a simple command from the parent shell?


https://redd.it/1aw0o98
@r_bash
I'm trying to make a noscript to enable/disable a gnome extension

Here is my noscript :

!/bin/bash

set -x

STATE="$(gnome-extensions show | grep State)"

if [[ $STATE == *"ENABLED"* \]\]; then

gnome-extensions disable

else

gnome-extensions enable

fi

but it succeeds only in enabling the extension and not disabling it, but I can't find what's wrong...
Do you have a clue ? 🎺

https://redd.it/1awd3ql
@r_bash
I'm trying to make a noscript to enable/disable a gnome extension

Here is my noscript :

#!/bin/bash

STATE="$(gnome-extensions show **window-list@gnome-shell-extensions.gcampax.github.com** | grep State)"

if [[ $STATE == *"ENABLED"* \]\]; then
gnome-extensions disable **window-list@gnome-shell-extensions.gcampax.github.com**
else
gnome-extensions enable **window-list@gnome-shell-extensions.gcampax.github.com**
fi

but it succeeds only in enabling the extension and not disabling it, but I can't find what's wrong...
Do you have a clue ? 🎺


https://redd.it/1awd3qh
@r_bash
I have a lot of folder in my linux machine with a lot of file, find duplicate files and delete them

I have liinux box and I have many many folders and in each folder many many files.

I am sure many of these files are duplicate, for example I have 1.mp4 in folder Videos, in folder Downloads and other folders.

I want bash noscript search all hard drive and find these files and show me and after that delete all of them.

https://redd.it/1awh0sm
@r_bash
PS1 issues,

My prompt glitches sometimes when scrolling through history, it will do things like drop characters,

"$ git push" will become "$ it push" but still work.

Another one that appears sometimes is ✔️ will add another of themselves for each character that I delete from my command.

Any ideas what is causeings this?

\------ a the majority (but not all) of my prompt code ------

PS1="\\n${PS1_USER}\\u ${PS1_BG_TEXT}at${PS1_SYSTEM} \\h ${PS1_BG_TEXT}in${PS1_PWD} \\w ${PS1_GIT}\\${GIT_INFO}\\

\\n\\${EXIT_STAT}${PS1_WHITE}\\$ ${PS1_RESET}"

\# function to set PS1

function _bash_prompt(){

\# This check has to be the first thing in the function or the $? will check the last command

\# in the noscript not the command prompt command

\# sets a command exit statues

if [[ $? -eq 0 \]\]; then

EXIT_STAT="✔️" # Green "✔️" for success

else

EXIT_STAT="" # Red "" for failure

fi

\# git info

export GIT_INFO=$(git branch &>/dev/null && echo "$(__git_ps1 '%s')")

}


(Edit grammar and formatting)

https://redd.it/1awhjr5
@r_bash
`fg` does not work in noscripts for raw terminal mode processes

just so we are on the same page, here is small c program

void enableRawMode() {
struct termios raw ;
tcgetattr(STDIN_FILENO, &raw);
raw.c_lflag &= ~(ECHO | ICANON | ISIG);
tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw);
}
int main() {
enableRawMode();
sleep(3)
return 0;

}

to accurately get pid, we do this:

./main & MAIN_PID=$!
echo $MAIN_PID
fg

for some reason this works when copy-pasting this into terminal, but when written in noscript, running it gets "`tcgetattr: Inappropriate ioctl for device`"

same with when running other programs which use raw mode, like vim (although different error message)

plz help >:

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