r_bash – Telegram
Help needed with getting all the lines containing <noscript></noscript> for a simple rss noscript

So this is the basic noscript:

>\#!/bin/bash
\# URL of the RSS feed
FEED_URL=" http://rss.cnn.com/rss/cnn\_topstories.rss"
\# Download the RSS feed
curl -s $FEED_URL > feed.xml
\# Extract the article noscripts
grep -E "<noscript>[\^<\]+</noscript>" feed.xml | sed -e "s/<noscript>//" -e "s/<\\/noscript>//"

The problem is that it gets all the xml and the grep and sed commands aren't doing anything.

I also tried these patterns:

>grep -E "<noscript>[\^<\]+</noscript>" feed.xml | sed 's/<noscript>\\|<\\/noscript>//g'

and:

>grep -E "<noscript>[\^<\]+</noscript>" cnn_topstories.rss | sed -n -e "s/<noscript>//" -e "s/<\\/noscript>//p" | tr -d '\^[0-9\]\\{1,\\}[:blank:\]'

and even with awk:

>grep -E "<noscript>[\^<\]+</noscript>" cnn_topstories.rss | awk -F'<noscript>|</noscript>' '{print $2}'

but nothing seems to do what I want.

I just want to get the noscript tags from the xml and echo all of them in a new line.

is that too much to ask LoL?
Any help is much appreciated

https://redd.it/10gb6mq
@r_bash
How to increment an index refer to parameter to read the value?

First time doing any bash noscripting so having some difficulties with it being so low level


Given the noscript below, how do I increase the index in the second part of the echo statement so that it prints the next parameter value (i.e print the first parameter, then print the second parameter)

paramIndex=1

while [ $paramIndex -le $# ]

do

echo "${!paramIndex}" "${!paramIndex}"

((paramIndex++))

done

So, for example running is as 'sh mynoscript.sh param1 param2' should echo:

param1

param2

&#x200B;

Currently its just printing param1 twice, and I've tried everything I've found online, but just cant get it to work

https://redd.it/10h2tjy
@r_bash
Calculate sum of pattern occurence in text file

So I have a text file, with each line being "X tests passed". X can be any number. I want to loop through the whole file and add up all the numbers, so the output would be "N tests passed" where N is the sum of all tests passed. How do I do this?

https://redd.it/10h2i85
@r_bash
Need help with formatting of collected data

My goal is to automatically name computers in the organization using a noscript. The only data required for the naming is Locale-ModelType-CurrentUserName (i.e. us-mbp-kunkmaster.flex)

The furthest I have gotten in the noscript is actually pulling the data that I need. What I cannot figure out is how to format that data into the end result. For example, the noscript in its current form will echo "MacBookPro". However, I want that to be translated to "mbp". Or if it echos "MacBookAir", that translates into "mba". I just cannot figure out the if/then as when I create those commands, the noscript errors out.

I will tip anyone that can solve this for me as I will also use it to learn how to automate several other things.

#!/usr/bin/env bash

# get currently logged in user
user=$( /usr/bin/stat -f "%Su" /dev/console )
echo "Current user is - $user"

# Get the Model of the Machine
model=$(system_profiler SPHardwareDataType | awk '/Model Name/ {print $3 $4}')
echo "Machine type is - $model"

#Get Locale of Machine
locale=$(locale | awk '/LANG/ {print $1}')
echo "Locale is - $locale"

https://redd.it/10hf2fz
@r_bash
Text separated by new lines acts different than text separated by spaces

I have this noscript that finds and deletes unwanted text (badwords) in directory names, subdirectory names and file names in the current working directory. The unwanted text is in the file /home/oops/badwords.txt

The noscript works as it should when the badwords in /home/oops/badwords.txt are all on seperate lines:

oops@8740w:~$ cat badwords.txt
CDRip
YIFY
AAC_5_1
WEBRip
YTS_MX

When I run the noscript those words are all deleted from the folder and file names in the working directory when they are on their own lines.

However, when the badwords in /home/oops/badwords.txt are just seperated by spaces:

oops@8740w:~$ cat badwords.text
CDRip YIFY AAC_5_1 WEBRip YTS_MX

then the noscript fails and doesn't find and delete the badwords.

Here's the noscript:
*********************************************


#!/bin/bash


input="/home/oops/badwords.txt"



while IFS= read -r line

do



echo "$line"



find -name "*$line*" -print0 | sort -rz | while read -d $'\0' f; do mv -v "$f" "$(dirname "$f")/$(basename "${f//$line/}")"; done

#
#
done < "$input"


**************************************************************************

I know while IFS= read -r line is working when the text is separated by spaces because the echo "$line"

prints the badwords when they're separated by spaces

But when "$line" gets passed into the find command it doesn't find and delete the badwords.

I'm thinking I need to add a newline somehow during the read process or maybe I don't have the right syntax(?) in the read command.

It doesn't really matter to me if the text in badwords.txt is all on separate lines but there's something I'm missing here and it just bothers me that I can't find the answer.

Please, go easy on me, I'm learning this on my own and know I haven't used the right words. Tried shellcheck but it doesn't see a problem.

I'd much appreciate a kick in the right direction.

https://redd.it/10hgw08
@r_bash
No output

Hi guys im new to shellbash , im running this on ubuntu virtual box ,When i run my program I don't get any errors. The program asks me for a number i write it in and then it just stops, could anybody tell me why the digits are not printing? thanks in advance

program

https://redd.it/10hsrpe
@r_bash
read -a variable <<< $dataset (Change delimiter)

The data set I am working with has spaces.


Structure:
SUBURB TOWN NUM1 NUM2

Since Suburbs and Towns often have Spaces in i have chosen to use the typical CSV structure.

VALLEY VIEW,JONES TOWN,2032,2033

Using the read command I can easily save it into variables using the dataset but the issue is that read -a defaults to a space character as the delimiter, so the above becomes...


${variable[0\]} is VALLEY
${variable[1\]} is VIEW,JONES
${variable[2\]} is TOWN,2032,2033
${variable[3\]} is null


I need to use the comma character as the delimiter so I can get the following...


${variable[0\]} is VALLEY VIEW
${variable[1\]} is JONES TOWN
${variable[2\]} is 2032
${variable[3\]} is 2033

I tried using ...

read -d "," -a variable <<< $dataset

&#x200B;

No difference. Any one got some idea?

https://redd.it/10i1iiu
@r_bash
How to uses pipes with an executable that does not support shebang line?

wasmtime does not support shebang; see https://github.com/bytecodealliance/wasmtime/issues/3715, https://github.com/bytecodealliance/wasmtime/issues/5614.

So when we do this


#!/usr/bin/env wasmtime
(module
;; ...
)

this happens

$ ./demo
Error: failed to run main module ./demo

Caused by:
0: if you're trying to run a precompiled module, pass --allow-precompiled
1: expected (
--> ./demo:1:1
|
1 | #!/usr/bin/env wasmtime
| ^

So far I have tried

#!/usr/bin/sh

wasmtime run /dev/stdin <<END
(module
;; ...
END

and


#!/usr/bin/sh

cat >foo.wat <<END
(module
;; ...
)
END

wasmtime run foo.wat

to no avail.

What I am trying to do is create a WebAssembly Native Messaging host that functions the same as C, C++, Python, JavaScript engine Native Messaging hosts.

Thanks in advance for your help.

https://redd.it/10i09se
@r_bash
Newline & carriage return not behaving like I expect with echo command.

I'm trying to make a one-line terminal command to append an alias to my .bashrc file. I want 2 returns/newlines/carriage returns so the file isn't a pain to read, then a commented "noscript" explaining the line's function, then the alias command.

I want it to look like:

#last line of the file


#Title/explanatory statement
alias quickbackupC1='rsync -ac --info=progress2 --delete /home/user/{Downloads,Desktop,Documents,GitHub,Pictures,Videos} /media/user/CRUCIALX6-1'

Here's what I came up with:

echo "\r\r#Alias for quick backup to drive 1\ralias quickbackupC1='rsync -ac --info=progress2 --delete /home/user/{Downloads,Desktop,Documents,GitHub,Pictures,Videos} /media/user/CRUCIALX6-1'" >> /home/user/.bashrc

All this does it paste everything inside the double quotes into the file like:

#last line of the file
\r\r#Alias for quick backup to drive 1\ralias quickbackupC1='rsync -ac --info=progress2 --delete /home/user/{Downloads,Desktop,Documents,GitHub,Pictures,Videos} /media/user/CRUCIALX6-1'

I've tried both \n and \r to insert blank lines. Where am I going wrong?

https://redd.it/10i6uoa
@r_bash
Why are my strings not concatenating? The current output is just "viewers!"
https://redd.it/10ic7w0
@r_bash
neovim on gitbash

im using gitbash as my terminal and i want to install neovim to it i already got vim and it works fine but hot to install neovim !!

https://redd.it/10iixqh
@r_bash
Compiling .sh files

Hey all , my task is to run a noscript ive made using makefile,I know that compiling .cpp files is with g++, but is there a command for compiling .sh files?

i just want to do something like g++ filea.sh fileb.sh \-o run

but this command returns following errors:

/usr/bin/ld:lab03a.sh: file format not recognized; treating as linker noscript

/usr/bin/ld:lab03a.sh:5: syntax error

collect2: error: ld returned 1 exit status

https://redd.it/10ijqdk
@r_bash
How can I chain commands together in bash that send each one into the background? Keep getting syntax errors, and I'm wondering if there's a way.

Here is an example of what I am trying to do:

less .bashrc &; less .bashrc &

less .bashrc & && less .bashrc &

Both give syntax errors at ; or &&, respectively. Is there a way to do this that can work in chained commands like this?

https://redd.it/10iqwh0
@r_bash
Is polling in a separate noscript the correct approach to terminate process substitution

wasmtime does not support using shebang then raw WAT for the noscript body, so we can't do


#!wasmtime

(module
;; ...
)


I've cobbled together a workaround using a separate noscript. Is the the correct approach to proceed?

nmcwat.sh

#!/bin/bash
# https://www.reddit.com/r/bash/comments/10i09se/comment/j5blsrw/

noscript='
(module
;; ...
)
'

./killwasmtime.sh &
./wasmtime
<(printf '%s' "$noscript")


killwasmtime.sh

#!/bin/bash
while pgrep -f nmcwat.sh > /dev/null
do
sleep 1
done
killall -9 wasmtime
exit 0

https://redd.it/10ixjx2
@r_bash
Grep for multi-character patterns in random order

Is there a way to grep for multiple patterns, each with several characters, in random order?

What I am trying to do would be something like

echo -e $inputline1 $inputline2 | grep [pattern][another_pattern]

However, that returns all lines containing any character in pattern right before any character in another_pattern, while I need something that returns all lines that match either pattern another_pattern or another_pattern pattern, preferably without nested greps.

https://redd.it/10iz8ke
@r_bash
Correct way to create a noscript-accessible environmental variable

Context

I've created my own equivalent of f.lux using xsct and a bash noscript. One feature I have is the ability to disable the bash noscript temporarily via a terminal command "evmode off" and to enable it via "evmode on". As the noscript runs once per minute via Cron, I need some way of preserving this setting outside the noscript itself.

Question

Right now, I just have a text file called "evmodeon"; if I enter "evmode off" into the terminal, the file is renamed to evmodeoff. The noscript checks for the presence of either file in order to determine whether it should run or not.

This seems like it is the wrong way to do it. I can always modify it so that the noscript checks the content of the file instead of the file name, but that still seems like I've just created a janky version of environment variables. However, as I've learned through my attempts to use actual environment variables, they are a pain to work with since I can't easily modify them with the noscript itself, and if I use source whenever the noscript exits the whole terminal session goes kaput. Indeed, that's why I used the file-name-as-variable approach to begin with.

What is the correct way of creating a system-wide variable that any noscript can reference and modify as needed? Should I just make a text file in my home folder called "variables" and pull everything from there, or is there an easier way?

https://redd.it/10j0wof
@r_bash
Wait command questions

The following example has a wait command 4 times.

if [ $2 == "local" ]; then
case ${srcOS,,} in
ubuntu|debian)
sudo service plexmediaserver $1
wait
linux)
sudo systemctl $1 plexmediaserver
wait
;;
esac
elif [[ $2 == "remote" ]]; then
case ${dst
OS,,} in
ubuntu|debian)
ssh user@IP "service plexmediaserver $1"
wait
linux)
ssh user@IP "systemctl $1 plexmediaserver"
wait
;;
esac
fi

Can I delete the 4 wait commands and add 1 wait command after fi ?

if [ <test> ]; then
command foo
else
command bar
fi
wait

For the ssh user@IP command is wait going to wait for ssh or the command?

https://redd.it/10j1wgs
@r_bash
( command & )

Anyone able to explain this syntax to run command in background and attached to init

( command & )

Cheers

https://redd.it/10iv1dl
@r_bash
How to extract the (unknown) middle part of a filename with fixed beginnings and ends?

I have files with names ABC. The middle part B changes, while A and C are fixed. A ends with a `-`, C starts with a `[`. I have the names in a variable `var`.

I know it is possible to get rid of A with `"${var#*-}"`, leaving BC.

I know it is possible to get rid of C with `"${var%\[*}"`, leaving AB.

Is it possible to combine this into one step, so that only B is left? What would be the best way to have this in a quick oneliner with

for var in *; do <stuff with B>; done

while not knowing the value of B?

https://redd.it/10j9qba
@r_bash
how to read file but ignore first and last line?

I have a .txt file which contains hundreds of numbers in a single column.

This file is used for some other program and I don't wanna modify it.

The file looks like:

>frequency in wavenumber
>
>1231.123
>
>222.2222
>
>3343.4311
>
>....
>
>end of file

Both the first and last lines are not numbers but strings, not sure how ti ignore them while only extracting the number.



>input="scanned_freq.dat"
>
># using IFS to remove all leading and trailing whitespace
>
>while IFS= read -r line
>
> do
>
> echo "$line"
>
>done < "$input"

This is what I currently have..

https://redd.it/10jeg9t
@r_bash
How to return just directory name of one directory back?

I want to return only the name of the directory that is one directory "up" from the current.

For example, if my current path is:

/dir1/dir2/dir3/dir4/current-dir/

I only want to extract dir4

What would be the best way to do this?

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