r_bash – Telegram
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
I have this little noscript to command from my MacBook, a Linux Enigma2 Decoder TV

#!/bin/sh

sshpass -p asdfg ssh root@192.168.5.192 opkg update

sshpass -p asdfg ssh root@192.168.5.192 opkg upgrade

sshpass -p asdfg ssh root@192.168.5.192 reboot

I have this little noscript to command from my MacBook, a Linux Enigma2 Decoder TV

I would simply like to run the update upgrade and finally reboot commands.

When I launch it, it updates and upgrades me correctly, but it doesn't reboot. It gives me this error.

How can I solve? Thank you.

https://preview.redd.it/ghlug79dewx91.jpg?width=1934&format=pjpg&auto=webp&s=a4245b31379dff8e74b954343b6afa1fd55ab18f

https://redd.it/ylt62g
@r_bash
reading CSV file and producing output

Hi All,

I'm in a little bit of a pickle and was wondering if someone could help with a small noscript.

I have this csv in this format:

root@ip-192-168-1-18:~# cat listing.csv
www.trust115.com/projectId=814aebb0113fcb9a1ad5a897,trust115.com/startingUserId=345bb1ce0113fcb9a1ad5a897,trust115.com/type=Workspace,trust115.com.com/wspaceid=2235a1ce91423fcc7ad5a897
www.trust115.com/projectId=814aebb0113fcb9a1ad5a897,trust115.com/startingUserId=345bb1ce0113fcb9a1ad5a897,trust115.com/type=Workspace,trust115.com.com/wspaceid=3335k11e31413ffc7vc51827

I'm looking at extracting the wspace id and then running the command below against each wspace id i extract to get the Used by run result.

root@ip-192-168-1-18: ~]$ kubectl describe pvc -n hardware-compute wspaceid-2235a1ce91423fcc7ad5a897 | grep run-
Used By: run-7354e65ba872320fa5e4862e-chb6b

Is there a quick way to noscript this?

https://redd.it/ylvijr
@r_bash
New to bash, please help with my function.

Hello!

I am making a function that I am going to put in bashrc like an alias. Here is a simplification:

my_cp() = {
user="$whoami"
a="/home/$user/Desktop/project1"
b="/run/media/$user/My\ USB/project1"
err="/var/log/my_cp.log"
desk="/home/$user/Desktop/ERROR_OCURRED.log"

if ! cp -r $a $b 2> $err; then
cp $err $desk
fi
echo "func done"
}

I have several questions about this:
1. do you need to write "function" before the function?
2. is it "$whoami" or $(whoami) ?
3. do variable pahs need quotes?
4. do variables that contain paths with spaces ignored by \, need quotes?
5. does ! really negate the output of the command with multiple components?
6. does the output reditection > really not influence the success or failure status of the command that is then negated by ! and evaluated with if? Without any braces or parantheses or anything?
7. does the if condition need to be put in [] brackets?
8. is the ; at the end of the condition ok?
9. is there a way to bundle this (with hardcoded paths ofc) into a single line? Because I haven't found how to do (cp -r $a $b 2> $err || cp $err $desk) && echo "func done"

Please if you can answer any of my questions, it would be great. I find so many sources online, but some do it this way some do it that way, and I can never deduce what the correct way is.

Thank you!

https://redd.it/ylxww2
@r_bash
Permission denied error after executing a shell noscript for SSHing into an instance

Hi guys,

I'm trying to develop an automation noscript for automatically generating a user's SSH key pair and uploading it in Jenkins, but am experiencing issues, as the SSH connection cannot be established:

ssh <user>@<ip> -- "sudo -i | mv /home/<user>/file /etc/asdf/asdf.bla"

the error is: mv: cannot stat 'path/to/file': permission denied

Everything in the noscript that's above the above line works just perfeclty, but when it comes to it it breaks with a permission denied error even tho there is sudo -i in the command

Any assistance would be greatly appreciated!

Thank you!

https://redd.it/ym0a2b
@r_bash
[Newb] how do I assign variable to adb devices output?

Trying to automate adb command using a noscript

ADB="adb"
$ADB devices

output

id1 devices
id2 devices
id3 devices

How do i grab these and assign

MAIN=id1
SUB1=id2
SUB2=id3

When more than 1 id requires -s command, so do i use -lt 1? or do I write

if $adb_device > 1 then
do $ADB -s
else $ADB

Thanks

https://redd.it/ym54js
@r_bash
Argument count

With a function that takes a lot of arguments, where several of the arguments appear multiple times, is it possible to count exactly HOW many times any given argument occurred?

Example:

main() {

echo $1 $2 $3 $4 $5 $6 $7 $8 $9

}

main a b b n c c a n a

and then find how many times I used "a" as an argument?

https://redd.it/ymdi45
@r_bash
How can I write the heroku pg:copy command?

Heroku allows to copy from one app database to another app database. The command is heroku pg:copy example-app::HEROKU_POSTGRESQL_ORANGE_URL GREEN --app example-staging-app

restore-a-backup

How do I write this in my bash noscript.

#!/bin/bash

echo $HEROKUAPPNAME

heroku pg:copy $HEROKUAPPNAME::$DATABASEURL $DATABASEURL --app $HEROKUAPPNAME

echo "Import is done."
bundle exec rake db:migrate

&#x200B;

This is getting syntax error.

https://redd.it/ymnj63
@r_bash
Using CSV field as an arguments in loop

Hi!

I'm trying to copy some files with a bash noscript. In the CSV file there would be two columns - file_path and copy_path, for example.

&#x200B;

I want to loop through CSV and copy using

cp file\_path copy\_path

In powershell I would use each field as a variable, but in bash this does no work.

For now my bash noscript looks like this

&#x200B;

#!/bin/bash

while IFS="," read -r name path

do

cp $name $path

done < nfs.csv

&#x200B;

Any help or direction appreciated :)

https://redd.it/ympovr
@r_bash