r_bash – Telegram
How to determine what block device is mapped to /dev/mapper/

I have an external drive mapped to /dev/mapper/backup.

My question is how do I verify what block device said mapping is mapped to?

My backup drive appears as /dev/sda, what would the command/noscript look like in order to verify that /dev/mapper/backup is in fact /dev/sda?

My god the amount of research I've done trying to figure how to do this is insane. Please send help, thank you

https://redd.it/yg91x4
@r_bash
How to test for connections excluding ping's

Hi

i am trying to run a minecraft(game) server, and make it start the server when someone tries to connect.

so, i tried using netcat -l 25565 and it work when i tried to connect, but that also accepted a ping connection and exited.

so, my question is, how to test for a connection that is not ping (or test if a connection is ping, to ignore it.)

https://redd.it/yh6wd3
@r_bash
if something > do something

hey so I have this noscript here and I was wondering about the reposonse from git pull "Already up to date."... Is there anyway I can just continue (break) if git pull returns "Already up to date." and if not continue to pull the next repo? like automate those two pulls so I dont have to press y or n to confirm to continue? Thanks alot!

​

#!/bin/bash

cd ~/github/ares; git pull
while true; do
read -p "##### ares-update? ##### " yn
case $yn in
yes ) ares-update; break;;
[Nn]
) break;;
) echo "Please answer yes or no.";;
esac
done
cd ~/github/dhewm3; git pull
while true; do
read -p "##### dhewm3-update? ##### " yn
case $yn in
[Yy]
) dhewm3-update; break;;
Nn ) break;;
) echo "Please answer yes or no.";;
esac
done

​

https://redd.it/yhbvcv
@r_bash
Standard place to define PATH and other environment variables

I've noticed that almost every installation noscript that adds something to PATH, and almost every guide that instructs the reader to define environment variables, chooses .bashrc for that. But if you want to be able to run commands over SSH non-interactively which depend on those modifications, .bashrc has no effect. In fact, at least on Ubuntu, .bashrc comes with code at the top to prevent you from sourcing it even explicitly in a non-interactive shell.

I can see the value of keeping non-interactive SSH commands quick by not loading more than is necessary, but I would think there would be a noscript where you could define things that you always want, like custom PATH additions. I haven't found a standard solution for this, and it's often annoying and surprising when my commands don't work because PATH isn't getting populated like I thought it would.

Am I missing something? How do you solve this problem?

https://redd.it/yhlriw
@r_bash
How can I get simple output from cURL download progress data?

I'm trying to "marry" `cURL` with `grep`|`awk`|`sed` to extract from the cURL output the real-time download percentage without using extra tools like `pv` and without showing the progress bar.

Preferred format: **Installing file... 55%** ### incrementing on the same position.

This is what I've tried so far after some research:

curl -L --output-dir ${apps_dir} "${url}" -o "${file_name}" 2>&1 | awk 'FNR == 2 '{print $1}' ### fails to show any data

Then I tried this:

curl -# -L --output-dir ${apps_dir} "${url}" -o "${file_name}" 2>&1 | stdbuf -o L tr '\r' '\n' | stdbuf -o L grep -Eo '[0-9.]*%'

The problem with the last line of code (note `-#`) is that it shows what I want but:
* prints out data on new line
* I don't know how to put the printout next to the string "Installing file..." (shame on me!)

Thank you!

https://redd.it/yi8sou
@r_bash
If first word starts with a vowel?

Hi, I’m trying to make a if statement that prints the 10 first lines if the first word in the file starts with a vowel. If not, it should print the 15 first lines in the file.

$first is the first word in the text file

words-sorted.txt is a text file containing a lot of random words

This is what I have tried:

if [[ "$first" == [^aeiou]* ]]
then
cat words-sorted.txt | head -n10
else
cat words-sorted.txt | head -n15
fi

I think there is something wrong with the «[^aeiou]*» part, but I don’t quite understand it.

Now it just prints the first 10 lines of the file, even if the first word starts with a consonant.

If I remove the «*» it print the first 15 lines, even if the first word starts with a vowel.

Any help is appreciated :)

https://redd.it/yidruk
@r_bash
My first noscript, would appriacate feedback!

I wrote my first bash noscript for users and groups management, I've been studying on my own for around two weeks and thought I should try to write somthing.

The noscript is around 95% bash with little bit of awk.

I would appriacate any feedback or suggestion as for how to improve the noscript or what to learn next.

Also, I got help in this forum so I want to say thank you very much to anyone that helped!

Its too long to post here so I pasted it in github.

https://github.com/Kakabom/Kakabom/blob/main/Script.bash

https://redd.it/yilt70
@r_bash
Use diff to find new usb port

Hi, I have a lot of usb ports on my pc and oftentimes I don't seem to find the right one under /dev/tty*. Is there a cleaner version of what I do here to detect a newly added usbport?

Currently Im doing this:
\- I did a /dev/tty* snapshot with all usb devices I normally use plugged in and store it somewhere. The command is: ls /dev/tty* > ~/somewhere/usbdevices
\-as I am plugging in a new usb device I run this noscript and get the newly added usb tty port:
diff ~/somewhere/usbdevices <(ls /dev/tty*).

&#x200B;



I just wonder, whether there is a cleaner solution to get the same output.

&#x200B;

Thank you

Edit: if it is important, I'm on a mac

https://redd.it/yipoqr
@r_bash
ssh with expect, send and interact

Have this code, which works as intended:

exec expect -c "
spawn ssh user@host
expect {
"password:" {send "1234"}
}
interact timeout 3599 {send "000"}
"


I would like to add another line under the one with "password:" for auto "sudo su -" . How can I do that? - Adding multiple expects. Adding the trigger keyword and the send doesn't work.

TIA

https://redd.it/yjfc69
@r_bash
How do you create custom user interfaces for bash noscripts?

Just something like telnet which gives you a > instead of hostname:username:workingdirectory$, and lets you run functions and commands in it, unlike (code here) where everything is executed after you close it with a ).

https://redd.it/yjgkh3
@r_bash
How to set Mouse Cursor to Busy (or diff cursor) Temporarily?

I have a noscript that runs for a bit so i would like to modify the mouse cursor to give some indication that its busy (e.g. hourglass). The cursor can be anything as long as its just not the current one.

After the noscript is done i would like it to return back to normal.

https://redd.it/yjnmzn
@r_bash
Extreme brain fart and I should know this. How do I expand the variables in this use case for a curl string?

So...

curl -k https://10.10.10.10:666/api/thing -b 'variable=$result'


The value of $result is what I need to have function out of single quotes here, and for some reason I'm either blanking on the correct terminology to google or man page to flip here.

Basically I'm gonna for loop through a bunch of files (thousands of lines each) and then spin up a ton of arbitrary dynamic curl commands to this API to have it do things.

What was the quoting/bracketing malarkey to make that $result expand properly into a usable form here? It's probably been 10+ years since I've done this...

https://redd.it/yjpzil
@r_bash
Run remote commands then connect to interactive shell

LTLFTP

Yall im lost and I need some serious help. I'm driving myself insane.

I have a giant ssh command that is running remote commands on a client server.

ssh user@host \' command1 && command 2 && command3.....\'

It's running this way without double quotes because
1) im connecting to a ksh shell
2) I have other commands running that use double quotes.


Is there ANY way I can add something to the end of my command string to drop me into a shell on the remote system?

https://redd.it/yjrtzt
@r_bash
Array looping to csv

Hello

I've written a noscript which saves arrays to csv however it not output how i expect it to be.

channel=($(seq 1 3)
channelid=(1 2 3)
channelname=(one two three)

echo channel, channelid, channelname > mycsv.csv
echo "${channel@}", "${channelid@}", "${channelname@" >> mycsv.csv

but this output as

channel, channelid, channelname
1 2 3, 1 2 3, one two three


what i was hoping it'd output as

channel, channelid, channelname
1,1,one
2,2,two
3,3,three

I know know i can re-write this as

echo channel, channelid, channelname > mycsv.csv
echo "${channel0}", "${channelid0}", "${channelname0" >> mycsv.csv
echo "${channel1}", "${channelid1}", "${channelname1" >> mycsv.csv
echo "${channel2}", "${channelid2}", "${channelname2" >> mycsv.csv

is there a more cleaner way to output arrays to csv without referring to index number?

https://redd.it/yjotom
@r_bash
tr command not working inside noscript.

date +%X | tr ":" " " | cut -d " " -f1

This command gives only the hour from date command. It works fine on the console but doesn't work inside a bash noscript? How can I use it inside a noscript?

https://redd.it/yjztj7
@r_bash
brem - My first released noscript/program

After lurking on this subreddit for a while now, I've finally gathered the courage to write my first post and release my first real program/noscript. It's written in POSIX sh because I wanted to challenge myself a little and learn new stuff.

It's a notes/reminders management program, with a weird design decision about it's storage format.

I would greatly appreciate any feedback you might have!

https://gitlab.com/k\_lar/brem

https://redd.it/yk8jde
@r_bash
Continued 'last command' troubleshooting

Hey all, this is a revival of my previous post [https://www.reddit.com/r/bash/comments/xds4z6/printf\_vs\_echo\_showing\_last\_command/](https://www.reddit.com/r/bash/comments/xds4z6/printf_vs_echo_showing_last_command/)

I can't get any of the suggestions in that one to work.

I finally got a chance tonight to dig back into this a bit. I spent a bit of time going through shellcheck and fixing (surprisingly, to me) everything it came up with, even the cosmetic issues!

Anyway, my noscript is still failing to run for reasons I'm having difficulty googling or understanding.

Here's a link to a gist with the entirety (sanitized) of my noscript. This includes line numbers that are in line with the line numbers referenced in the shell output below.

[https://gist.github.com/bhoglan/b47809ac03a0ea878f0a01a9d90b54f3.js](https://gist.github.com/bhoglan/b47809ac03a0ea878f0a01a9d90b54f3.js)

And here's the shell output I get when I try to execute the noscript:

➜ docker1| shared: ./bookstackArchive.sh
./bookstackArchive.sh: line 39: :0: bad word specifier
./bookstackArchive.sh: line 39: syntax error near unexpected token `}'
./bookstackArchive.sh: line 1: /shared/archive.log: No such file or directory

Now, I've putzed around with the first error a bit. From the code I'm interpreting the !:0 as "Last *n* command" where *n* is the index of the command in my history, starting at 0, with 0 being the most recently run command. I was thinking that maybe it's interpreting the other stuff on line 39 as commands (date and echo) so I tried !:2 to get back to the command run that triggered the success, no dice, the error continues just with the updated index number.

The second error seems like nonsense to me given the context of line 39. I tried adding spaces and using things like \[\[ \]\], no joy.

The third error I just don't get. Line 1 in my noscript is the shebang. I played with the first instance of "/shared/archive.log" which appears on line 34, like maybe bash isn't interpreting the comment lines above as real lines? I dunno, I'm grasping at straws here.

I appreciate any advice or direction you can give. I'm learning a lot as I'm building these noscripts, most fundamentally, I think, is that I'm learning to think about things programmatically and how to tell a computer to solve problems.

Thanks all!

https://redd.it/yksdyz
@r_bash