r_bash – Telegram
Changing placeholder denoscription via sed

Hi! As an input for sed program I have smth like this: {bool some flag denoscription} (it's a part of an input string to be more precise) where:

- bool is a placeholder type
- some flag denoscription is it's denoscription

How do I replace all such placeholders to this {{some_flag_denoscription}}? I don't know how to replace spaces between 3 denoscription words with underscores. What sed technique should I use? I wanna transform some flag denoscription in a such way not to broke the whole placeholder. Another difficulty is that this placeholder can appear in any place in a command invocation example like some_executable --dark {bool whether to enable dark more} --colorize {bool whether to colorize files}. I need to know how to do this to fix my noscript.

https://redd.it/10nblyy
@r_bash
Beginner having trouble using variables as literal strings

Hello,

I'm new to Bash, and I've been hammering my way through issues with the help of ChatGPT, but it's running me in circles on this one.

I'm using Curl to make a request to Pushover's API. Many of the options that I call Curl with are exactly the same, so I thought that I'd put them in a variable to make things easier. I know that In an actual request, I would have to include more options, but I wanted to trim this example down.



po_token="ExampleToken"
po_user="ExampleUserKey"
curl_ops='-s -o "/dev/null" --connect-timeout "5" --retry "6" --retry-delay "600" -F "token=$po_token" -F "user=$po_user" "https://api.pushover.net/1/messages.json"'
curl $curl_ops



It seems that everything in my "curl_ops" variable is being passed as a single argument instead of a literal string, and I'm certain it has to do with my usage of quotes, but I have no idea how to fix it. The only noscripting language that I'm familiar with is AutoIt, and Bash seems to handle this kind of thing very differently. I was hoping someone here may have some suggestions.

Thanks

https://redd.it/10nbdv5
@r_bash
how to supress this

hi

im using psexec example PSEXEC \\\\workstation64 x:\\runme.bat


noscripts runs fine how ever i want to supress the displaying of the exit code here is the output

at exited on srv1 with error code 0.

https://redd.it/10nl7yc
@r_bash
Is there a difference between " tr a-z n-za-m " and " tr 'a-z' 'n-zam' " ?

Both provide the same output, but I'm seeing online people are recommending using " tr '[a-z\]' '[n-za-m\]' "

Why is that?

https://redd.it/10ny8a4
@r_bash
Linux bash practice site

HI, I am preparing for OSCP and hence I am learning bash.
Can someone please suggest me a best place to practice bash(For linux).
Any online link would be helpfull

https://redd.it/10o0z6g
@r_bash
What is wrong with this noscript?

Trying to grep an array of strings from a file on a remote server but the noscript does seem to do it, I might be missing quotes or the use of array is wrong here?

PARAMS=(“abc” “xyz” “ijk”)
REMOTEFILE=“pathtoFile”
NODES=(“srv1” “srv2”)

for srv in “${NODES@}”
do
ssh $srv << EOF
grep -f “${PARAMS@}” $REMOTEFILE >/path/remotefile.txt
EOF
done

https://redd.it/10nxxc0
@r_bash
Cannot backup a directory with tar. When i am trying to run the noscript it says "Cannot stat: No such file or directory". What is the problem? How to resolve it? Or maybe there is some other way to backup a directory. Please help

https://redd.it/10o3h0k
@r_bash
How can I modify this bash command that will also output the content of the given file.
https://redd.it/10o2qi5
@r_bash
What is the fastest way to read groups of N lines from stdin / a pipe?

Im trying to figure out a good, efficient and reliable way to read groups of N lines at a time from stdin or (more generally) from a pipe. Id like to accomplish this without needing to save (or wait for) the entire contents of stdin into a variable. So far the only reliable way Ive found to do this involves something like:

nLines=8 # change as needed
while true; do
outCur="$(kk=0; while (( $kk < $nLines )); do read -r </dev/stdin || break; echo "$REPLY"; done)"
[ -z $outCur ] && break || echo "$outCur"
done # | <...>

That said, if reading the entire pipe, there are some options that are much faster than read. I wrote a little speedtest to test a few:

fgg() { while read -r; do echo "$REPLY"; done | wc -l; }
fhh() { cat | wc -l; }
fii() { head -n $1 | wc -l; }
fjj() { echo "$(</dev/stdin)" | wc -l; }
fkk() { wc -l </dev/stdin; }
fll() { mapfile -t inLines </dev/stdin; printf '%s\n' "${inLines@}" | wc -l; }
fmm() { inLines="$(</dev/stdin)"; echo "${inLines}" | wc -l; }

# note: fll and fmm dont meet the "dont save the full stdin to a variable" criteria

nLines=100000

for fun in fgg fhh "fii $nLines" fjj fkk fll fmm; do
declare -f "${fun% }"
echo
time seq 1 $nLines | $fun
echo
echo '----------------------------------'
echo
done

Which, on my test system, produces

fgg ()
{
while read -r; do
echo "
$REPLY";
done | wc -l
}

100000

real 0m5.780s
user 0m3.655s
sys 0m4.306s

----------------------------------

fhh ()
{
cat | wc -l
}

100000

real 0m0.086s
user 0m0.077s
sys 0m0.028s

----------------------------------

fii ()
{
head -n $1 | wc -l
}

100000

real 0m0.085s
user 0m0.107s
sys 0m0.000s

----------------------------------

fjj ()
{
echo "$(</dev/stdin)" | wc -l
}

100000

real 0m0.124s
user 0m0.117s
sys 0m0.039s

----------------------------------

fkk ()
{
wc -l < /dev/stdin
}

1000001

real 0m0.085s
user 0m0.094s
sys 0m0.000s

----------------------------------

fll ()
{
mapfile -t inLines < /dev/stdin;
printf '%s\n' "${inLines[@]}" | wc -l
}

100000

real 0m2.241s
user 0m1.064s
sys 0m2.331s

----------------------------------

fmm ()
{
inLines="$(</dev/stdin)";
echo "${inLines}" | wc -l
}

100000

real 0m0.185s
user 0m0.180s
sys 0m0.039s

This suggests (on my test system) in a worst-case scenario (i.e., many many inputs and most of the time spend looping the `read` command), `read` is about 75x slower than optimal, loading the lines into an array is \~25x slower than optimal (+ more memory usage), and saving the full input into a (non-array) variable is \~2x slower than optimal (+ more memory usage).



From this test, using head -n in a loop seems like the obvious optimal answer, but when I try this I get....strange behavior...

seq 1 1000 | {
head -n 10
head -n 10
head -n 10
}

produces


0
1
2
3
4
5
6
7
8
9
284
285
286
287
288
289
290
291
292
293
540
541
542
543
544
545
546
547
548
549

Playing around with different sized input lines suggests that on every subsequent call after the 1st on the same pipe, head -n throws away the 1st kilobyte worth of input lines. Perhaps it is trying to "skip" reading a 1kb pipe header of some sort, but since it isnt at the start of the pipe it throws away data instead? If anyone knows how to stop head -n from doing this
please let me know. Or if there is some other quick way that I havent tested here please do suggest it.

Thanks in advance.

https://redd.it/10odhbo
@r_bash
Random Joke generator

Small bash file to tell a random joke. (100+ available!)

&#x200B;

\#!/bin/bash

\#mmartin.,29/01/23, Purpose:Print a random joke

awk '

BEGIN{ srand() }

rand() * NR < 1 {

line = $0

}

END { print line }' \~/Documents/VariousTexts/jokes.txt

exit

&#x200B;

Download from my github

github.com/myklmar/noscripts with jokes.txt file.

https://redd.it/10oc0a4
@r_bash
Thank you

I don’t post much but I do lurk around looking to learn. From beginner to expert questions everyone is very helpful. Just wanted to say thank you to the community.

BTW will be cross posting to r/commandline because they are a great community as well.

https://redd.it/10p3tia
@r_bash
How to filer partial matches that do not end in certain characters

first time posting, forgive me


Basically I want to filter output to go from something like this:


mate-desktop-common
gedit
gedit:i386


to this


mate-desktop-common
gedit:i386


How would I do that?

https://redd.it/10p6duw
@r_bash
exit terminal without closing the program / headless mode

Hi there!I run this noscript:

#! /bin/sh.
waydroid session stop
weston --width=555 --height=875 -Swayland-droid & WAYLAND_DISPLAY=wayland-droid waydroid show-full-ui

The goal is to open weston, which will start waydroid in that window size. Everything works fine, but I'd like to hide or exit the terminal and only show the weston window. (Like headless mode)? How could I achieve this?

https://redd.it/10pd91w
@r_bash