r_bash – Telegram
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
Beginner can't make a simple noscript to work

1 #!/bin/bash
1
2 bt="bluetoothctl info 74:45:CE:90:9C:4F | grep Connected"
3 if [ $bt='Connected: yes' ]
4 then
5 dunstify "headphones connected"
6 else
7 dunstify "unknown error"
8 fi

What is the wrong here? It always prints the 'headphones connected' -line even if my headphones isn't connected.


I know awk would be much better, but I couldn't make that to work. (The "Connected: yes" is the 10th line of that command)

https://redd.it/10jjxd9
@r_bash
What is the proper way to relatively source a file?

I am in the toplevel directory inside a project. There is a directory test and inside that is a bash file named test.sh. This file tries to source another bash file in the same directory called testlib.

When I run ./test/test.sh, it fails with "file not found" if I do source testlib or source ./testlib inside test.sh, but will work when I do source ${0##*/}/testlib, which I think is hideous even though I know exactly what it's doing (I also think using dirname "$0" there is just as hideous).

Why does source not automatically include the directory of the file trying to do the sourcing so you can do relative sourcing? This seems counterintuitive for some reason (probably because all other languages I've worked with permit it). Is this for security reasons?

I'm on Bash 5.2.15.

https://redd.it/10jipn2
@r_bash
Concept: Self configuring noscript in BASH

Smaller noscripts, I place a bunch of declare settingvariable="Setting goes here" at the beginning of my noscript, right after the #!/bin/bash

But what if I could edit those variables without editing the actual file.

I am working towards having ./noscript.sh --config settingvariable fetch the variable and display it's setting, then allow me to either press return to keep it as is, or to type in a new variable.

The question I had is, can I get get a noscript to read and write to itself? This is what I came up with...

#!/bin/bash

#Name this file test.sh

# $ chmod +x test.sh

#Usage: $ ./test.sh <comment to insert between START and STOP>

###START###

###STOP###

start=$( cat -n test.sh | grep "###START###" | head -1 | awk '{print $1;}' )

stop=$( cat -n test.sh | grep "###STOP###" | head -1 | awk '{print $1;}' )

echo "Start of config: $start"

echo "Stop of config: $stop"

sed -i $stop' i #'$1 `test.sh`

Now run it as follows:

$ ./test.sh Hello

$ ./test.sh World

&#x200B;

$ cat test.sh

https://redd.it/10jowj9
@r_bash
Brace expansion syntax bug?

I may be doing this incorrectly:

epitaph64@debian64-laptop:~$ echo -e {1..5}{a,b}"\n"
1a
1b
2a
2b
3a
3b
4a
4b
5a
5b

I've tested this more recently on Ubuntu 22.0.4 using Bash version 5.1.16(1)

Does anyone one know why it's like this? Having the unnecessary spaces for all but the first line of output looks disordered so it seems like a bug...

https://redd.it/10jfhpi
@r_bash
I am not even mad, I just want an explanation (first and last line)
https://redd.it/10jutp4
@r_bash
bash noscript to copy password from a different file

Hey everyone, having a project deadline and would love some help please.

i have the below noscript set to run and get the credentials from another file but i keep getting an error saying credentials file cannot be found. would love some help please. i will also take any other ideas for a better noscript.

\#!/bin/bash

fail() { printf "%s\\n" "$2"; exit $1; }

credsfile=/home/njames/.config/windows.credentials

[[ -e "$credsfile" \]\] || fail 1 "Credentials file '$credsfile' not found"

source "$credsfile"

[[ -n "PKR_VAR_vsphere_password" \]\] || fail 1 "PKR_VAR_vsphere_password not set in '$credsfile'"

[[ -n "PKR_VAR_winadmin_password" \]\] || fail 1 "PKR_VAR_winadmin_password not set in '$credsfile'"

packer init .

packer validate .

packer build .

https://redd.it/10jwg35
@r_bash
Why does la invoke ls?

$ la
manifest.json ...

I can't find a symlink from la to ls in /usr/bin.

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