r_bash – Telegram
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
My first Bash noscript - Git / GitHub Automation noscript

I'm a complete beginner at bash noscripting.

I have written a Python noscript which creates a local git repository and also connects it to a remote repository on GitHub. I wanted to have an installer for this noscript as in I would run that installation file and then the executable Python noscript will be available in any directory.
I have written an installer noscript which writes to the .bash_profile file. Here is the link to my repository.

Is there a better way to do this? Sorry for the bad grammar, English is not my first language.

https://redd.it/yl32ro
@r_bash
curl a file of data blocks with parallelism?

I'm trying to accomplish batching of millions of curl requests with different data blocks, for example..

curl --parallel --parallel-immediate --parallel-max 100 -X POST -H 'this header' -H 'that header' https://host/uri

I then want it to parse a list of data blocks, I tried using -K/--config and passing in: data="{\\"key\\": \\"value\\"}"

... but it wouldn't iterate through the file. I've read about using GNU parallel instead, but I'm not sure how I would parse a list of data blocks for the same curl command.

https://redd.it/yl693n
@r_bash
How to make this into one-line?

MYVARIABLE="$(command1)"
command2
command3 $MY
VARIABLE

Is there a way to avoid this MY_VARIABLE stuff? The thing is, command2 must execute before command3; but command1 only returns the right value when executed before command2.

https://redd.it/ylbal3
@r_bash
Does sourcing another noscript affect -e option?

I have this test snippet and want to confirm if my assumption is true. It looks like the sourced noscript continues despite having -e option. But if the noscript is just executed, then -e option works as expected.

cat `a.sh`

#!/bin/bash

echo "Test 1"

./b.sh

echo $?

echo "Test 2"

source ./b.sh

echo $?

​

cat `b.sh`

#!/bin/bash -e

ls ~/doesntexist

​

./a.sh

Test 1

ls: cannnot access '/home/user/doesntexist': No such file or directory

2

Test 2

ls: cannnot access '/home/user/doesntexist': No such file or directory

2

​

## modify `b.sh`

cat `b.sh`

#!/bin/bash -e

ls ~/doesntexist

echo "This shouldn't run"

​

./a.sh

Test 1

ls: cannnot access '/home/user/doesntexist': No such file or directory

2

Test 2

ls: cannnot access '/home/user/doesntexist': No such file or directory

This shouldn't run

0

https://redd.it/ylhm3a
@r_bash
kubernetes bash one liner and storage class details.

Hi All,

Hoping someone could get help or provide some guidance, I'm looking at trying to get a list of users or pods list used by a particular StorageClass i.e. hwtierdisk how would one do this?

At the moment I have this one liner.

for c in $(kubectl get pv -n hardware-compute | awk '/wspace/ && /hwtierdisk/ {split($6,a,"/") ; print a2}') ; do
for d in $(kubectl get pvc -n hardware-compute $c --show-labels --no-headers | awk '{print $8}') ; do
echo $d >> list.csv
done
done

Which works and provides the run details etc but I was hoping to expand on it to provide the pod details with the above storageclass

https://redd.it/ylglfx
@r_bash
Help/ideas for writing better code for a dumb program

I have written a program I call powernap, which is an "advanced" equivalent of writing this:
`sleep $1 && poweroff`

It started out as just that, but eventually I got bored and wanted to know exactly when my PC would poweroff. I added a `date` at the beginning to show when it was executed but I still had to calculate when it would poweroff in my head, but I'm also too lazy to keep doing that. So I figured it shouldn't be that hard to write a noscript to tell me exactly when it will shut down my computer...
**It was bad.**

This is the single worst program I have probably ever written and I'm doing some very stupid stuff that I know is dumb, but I honestly have no idea how to make it simpler, without writing my own version of `date` in bash.

Most of the problems come from converting one time format to another.
Examples:

* 1.5h / 36m / 300s (these should be valid for you to pass to `sleep`
* 50 minutes / 305 seconds (this should be valid for you to pass to `date --date="$number minutes"`

How I'm doing it, is taking $1, splitting it into a number and it's "timeval" which is one of these h/m/s. Then I wrote this stupid if statement for each one of the letters:

usrtime=$(tr -d 'hms' <<< "$1")
timeval=$(tr -d -c 'hms' <<< "$1")

if [ "$timeval" = "h" ];then
usrtime=$(awk -v value="$usrtime" 'BEGIN {print(value*60)}' | sed "s/\..*//")
if [ "$usrtime" -gt 5 ];then
time2sleep=$(awk -v value="$usrtime" 'BEGIN {print(value-5)}' | sed "s/\..*//")
dowarn=1
else
time2sleep="$usrtime"
dowarn=0
fi
datenext=$(date --date="$usrtime minutes" +"%T")
time2sleep="${time2sleep}m"
fi

If anyone can help rid the world of this god-awful and hacky way of doing this, please tell me how to do it.

Here's the link to the whole thing: [https://gitlab.com/k\_lar/dotfiles/-/blob/master/bin/powernap](https://gitlab.com/k_lar/dotfiles/-/blob/master/bin/powernap)

https://redd.it/yljlwt
@r_bash
Save body of email or attachment to file in specific directory?

I'm exploring an idea and want to pick the collective brain in here to see what people think. Would it be possible to extract the contents of an email - the subject can be the trigger or keyword - and save it's contents to a text file? Or would it be possible to save an attachment to a specific directory or I guess any directory?

Any help/guidance is appreciated.

https://redd.it/ylmxt1
@r_bash
Variable failing in expect

Hi All,

I am doing a noscript which will execute a specific command, that command includes quotes, tried defining as follows:

set stats {command "command.command *"}

When i run it as is on the server its designated to, works fine, however, running from the noscript, shows like below:


[local\]MyHost#command "command.command *"

\----------------------\^

syntax error: incomplete path

Can you please help with this?

https://redd.it/ylqj0l
@r_bash
Can we convert the bash noscript into a custom command on our system?

Let's say I've made a bash noscript.

We have to run this command to execute that bash: `bash yourbash.sh`

But can I make any command which will execute the above command?

https://redd.it/ylt80l
@r_bash