r_bash – Telegram
What does this command do in .zsh file

I am using the docker-compose plugin in zsh and want to use docker compose v2 commands. so when exploring the plugins I found this line

(( ${+commands[docker-compose]} )) && dccmd='docker-compose' || dccmd='docker compose'

can someone explain what does this do?

https://redd.it/xdxqvd
@r_bash
Inserting variables under the third column in a textfile.

So i have two columns and would like add text to the Value3 column.

i've tried code below which works for the first two columns. But not for the third.

Help or suggestions is appreciated.

echo -e "" "val1 $numb1" "\n" "val2 $numb2" | paste -d "\t" /files/test.txt -

Columns

Value1 Value2 Value3
val1 34 val1 55
val2 34 val2 55

https://redd.it/x5y5wa
@r_bash
can someone what did I overlook

what is the purpose of the noscript: unlocking the dataset using the curl command

the code that works:

curl "`https://$host/api/v2.0/pool/dataset/unlock`" -k -X POST -H "accept: */*" -H "Content-Type: application/json" -H "Authorization: Bearer 1-$API_TOKEN" -d '{"id": "the_dataset_hardcoded","unlock_options": {"key_file": false,"recursive": false,"toggle_attachments": true,"datasets": [{"name" : "the_dataset_hardcoded" , "passphrase" : "the_password_hardcoded"}]}}'

now this one works flawlessly, but the moment i try to replace the "the_dataset_hardcoded" and/or the "the_password_hardcoded" as an environmental variable the noscript stops working

my next try is to create the part as a string that i can echo out for debugging so i create:

curl_builder3="'{\"id\": \"$pool\",\"unlock_options\": {\"key_file\": false,\"recursive\": false,\"toggle_attachments\": true,\"datasets\": [{\"name\" : \"$pool\" , \"passphrase\" : \"$pass\"}]}}'"

and run it like this:

curl "`https://$host/api/v2.0/pool/dataset/unlock`" -k -X POST -H "accept: */*" -H "Content-Type: application/json" -H "Authorization: Bearer 1-$API_TOKEN" -d $curl_builder3

but what i get for this is:

curl: (3) unmatched brace in URL position 1:

{"key_file":

^

bash: line 3: Server: command not found

​

but when I echo the created env variable i get the correct string that if i copy and run in terminal it does what is supposed to do

​

now what did I miss

​

​

edit:

the curl command is now fixed and it looks like this:

curl "`https://$host/api/v2.0/pool/dataset/unlock`" -k -X POST -H "accept: */*" -H "Content-Type: application/json" -H "Authorization: Bearer 1-$API_TOKEN" -d "{\"id\": \"$pool\",\"unlock_options\": {\"key_file\": false,\"recursive\": false,\"toggle_attachments\": true,\"datasets\": [{\"name\" : \"$pool\" , \"passphrase\" : \"$pass\"}]}}"

​

https://redd.it/x5v910
@r_bash
Launch an app from a noscript

Hi all,

​

I'm making a noscript in which i need to check if an app is launched and if it's not, launch it from the noscript.

​

The app is a .sh file, i can use the bash command to launch it but if i do the noscript doesn't stop until the app is shut down which is not what i'm looking for.

​

Basically i would like to run this noscript every 5 minutes or so, that's why i need a command that just launches the app and not runs it in the noscript. Hope i'm making sense here.

​

Any help is appreciated i'm still new to noscripting, thanks for reading !

https://redd.it/xe5egk
@r_bash
Need help with loop in a bash file

I have a bash noscript which needs to do the following:
change to folder /repos
loop through all the subfolders (git repos) and
run command "git config --bool core.bare true"

This is what I have so far:
for dir in ~/repos/*; do (cd "$dir" && git config --bool core.bare true); done

https://redd.it/x5g916
@r_bash
Need help improving my first Bash Script

​

What I wanted to do

* Have this scrip start every 10 minutes
* Exit the noscript if it is already running
* Copy new files to a network folder based on file creation date
* Verify the copy was successful
* Email me on completion and either a message of no errors or a list of files that did not copy correctly
* Unmount the cards

What I have so far

* A noscript that when run with the SD cards in it identifies the type of camera by card name
* Copies all the video files from camera 1's memory card to a network share
* Copies all the video files from camera 2's memory card to a network share
* Copies all the GPS data from camera 2's memory card to a network share.
* Unmount the cards

​

I should have 6 hours of time available to copy before I have to take the cards and put them back in the camera.

My code is below.

​

#!/bin/bash
clear
echo "Checking for SD Cards"
sleep 2
echo "checking for Lamnando Card"
sleep 5
if [ -d "/media/dashcam/6310-5AE7/rearVideo1" ]; then
echo "SD Card for Lanmando is inserted"
echo " Copying Files Front Camera Files"
rsync -a -P /media/dashcam/6310-5AE7/frontVideo0/* /synology-nas/shared/DashCam/
echo "Complete"
echo " Copying Rear Camera Files"
rsync -a -P /media/dashcam/6310-5AE7/rearVideo1/* /synology-nas/shared/DashCam/
echo "Complete"



sleep 2
else
echo "Lanmando Card Not Found"
fi
echo "Checking for VaVa Card"
if [ -d "/media/dashcam/VA-VD009/VAVA" ]; then
echo "SD Card for VaVa is inserted"
echo "Copying Video Files"
rsync -a -P /media/dashcam/VA-VD009/VAVA/MOVIE/* /synology-nas/shared/DashCam
echo "Complete"
echo "Copying GPS data"
rsync -a -P /media/dashcam/VA-VD009/VAVA/GPSDATA/* /synology-nas/shared/DashCam
else
echo "VaVa Card Not Found"
fi
echo "unmounting cards"
sleep 3
if [ -d "/media/dashcam/6310-5AE7/rearVideo1" ]; then
umount /media/dashcam/6310-5AE7
fi

if [ -d "/media/dashcam/VA-VD009/VAVA" ]; then
umount /media/dashcam/VA-VD009
fi
sleep 3
echo "All cards unmounted you can now safely remove them"

https://redd.it/x444uu
@r_bash
Script to unpack rar archives in multiple sub folders using bsdtar or p7zip?

I'm using a filezilla docker to download folders that each contain a set of rar files. I had a noscript that used unrar that filezilla would run when the download finished.

Unfortunately the docker is based on alpine linux and no longer has unrar in its repos. So my two options are bsdtar or p7zip and I haven't quite figured out how to get them to replace my previous noscript.

With unrar the line I used was unrar x -r -o- *.rar and it would go through all the folder and unpack all the archives.

7zip has a recurse option, but i can't get it to work. 7z x -r -aou *.rar is what I've tried.

Any suggestions?

https://redd.it/xeg082
@r_bash
How can I write a noscript that replaces all occurrences that match a pattern in all files within a directory and sub directory?

I am a frontend developer and we were using react-bootstrap 3 which had export a component Clearfix.
We have been importing that using

import {Clearfix} from "react-bootstrap" or import {Clearfix, (some other components separated by comma)} from "react-bootstrap"

We upgraded to react-bootstrap 4 but it doesn't export that component anymore and we have built a custom component for that. but because we have used the one from bootstrap in hundreds of files I don't wanna go and replace all one by one.
Is there a way to write a noscript that it will replace

import {Clearfix} from "react-bootstrap"

with

import {Clearfix} from "my/custom/path"

and if it finds

import {Clearfix, (some other components separated by comma)} from "react-bootstrap"

it just removes Clearfix from here and adds a new line below it with

import {Clearfix} from "my/custom/path"

https://redd.it/x3wrm3
@r_bash
"&&" operator not working with "curl"

I have the following commands I want to run in a noscript called example.sh:

./noscript1.sh && ./noscript2.sh && "curl -X POST http://<myServer>:xxxx/yyy/zzz" && "shutdown -h now"

Now the problem is that the && operator somehow breaks the curl command, it exits with the following message:

./example.sh: 3: curl -X POST http://<myServer>:xxxx/yyy/zzz: not found

Is there a working alternative to &&? Semikolon works but it breaks the && operator condition.

Thanks!

https://redd.it/x3w112
@r_bash
Authenticate using external noscript

I have a bunch of small noscripts which are interacting with a rest api. An example of such is the below list.sh noscript.

#!/usr/bin/env bash

set -euo pipefail

$HTTP "$INSTANCE_HOST/instances" "Authorization: Bearer $ACCESS_TOKEN"

Note that HTTP="http --verify=no --check-status"

All of these small noscripts have the need for a valid access token ($ACCESS_TOKEN) in common.

What I'd like to have is another noscript which I can source and have the ACCESS_TOKEN variable updated as needed. I've tried to do that using the following noscript. Named auth.sh.

#!/usr/bin/env bash

exp=$(echo "$ACCESS_TOKEN" | jq -R 'split(".")? | .[1] | @base64d | fromjson | .exp')

NOW=$(date +%s)

if [[ -z "$exp" ]] || (( $exp < $NOW )); then

echo "Update token..."

# shellcheck disable=SC2155

export ACCESS_TOKEN=$($HTTP --auth "$USER_EMAIL:$PASSWORD" post "$INSTANCE_HOST/tokens" | jq -r '.access_token')

else

echo "Token still valid!"

fi

But the above noscript always update the access token.

Using the above I'd like to have my initial list.sh noscript defined as below

#!/usr/bin/env bash

set -euo pipefail

source ./auth.sh

$HTTP "$INSTANCE_HOST/instances" "Authorization: Bearer $ACCESS_TOKEN"

Is this possible with bash? As mentioned before the auth.sh noscript always update the access token.

https://redd.it/x3r0af
@r_bash
Stupid (but documented) bash behavior

This is actually documented (and reading the documentation was what made me write this), but ... extremely surprising. I'm so horrified that I had to share. Try to figure out the output without running it.

Some of the code is not necessary to demonstrate the behavior in the version of bash I tested (5.1). But I left it in because maybe other versions do something else (since the documentation doesn't completely specify the behavior, and it surprised me).

#!/bin/bash

# Blame tilde expansion

alias foo=alias
foo=global

outer()
{
local foo=outer
inner
echo -n outer\ ; declare -p foo
}

inner()
{
#local foo=inner
alias foo=(lol wut)
local foo=inner
echo -n inner\ ; declare -p foo
}

outer
echo -n global\ ; declare -p foo
alias foo

https://redd.it/xeohfi
@r_bash
basic text editor help probably

I have 10000 plus lines , that must be made into a single line

Replacing \n with "" does not seem to work. Any idea how to do it?

https://redd.it/x38hpl
@r_bash
Help with file denoscriptors and redirections.

So, I have this issue: I'm running a command that prints to both stdout and stderr.

The stdout is huge, and must be stored into a file. But I also want the errors to be printed there, for later parsing.

This would be simple, right:


$ ( echo out; >&2 echo err; echo out ) 2>&1 > /tmp/file


However, since this is running in a pipeline, I also want it to print out the errors. I cannot use tee, as it would completely drown out the errors and make the pipeline log unreadable. So in essence I want to have the stdout directed into a file, and I want the stderr to be redirected to both the file AND the terminal that calls it.


This was the best I could come up with:

$ ( echo out; >&2 echo err; echo out ) 2> >(tee -a /tmp/file) > /tmp/file


But unfortunately this doesn't even work since it either prints the err at the end of the file, or it omits it from the file completely because of race conditions (I think¹).

I know this is kind of weird, but I wonder if there's a good way of doing it.

Edit:
I now understand that either the > creates the file, does its thing and then tee appends to it, or tee creates it first, and then > overrides it. I could always replace > with >> to have all the outputs, but since this is still pretty messy, I'd like to know if there's something better than:

$ > /tmp/file
$ ( echo out; >&2 echo err; echo out ) 2> >(tee -a /tmp/file) >> /tmp/file

Edit 2:

Best I could do:

$ ( echo out; >&2 echo err; echo out ) 2>&1 1>>/tmp/file | tee -a /tmp/file

https://redd.it/x3atli
@r_bash
Need a maintainer(MacOS) for a little bash noscript

I wrote this noscript for the kitty terminal. It works well on linux but fails to work on MacOS. This is obvious because it was never tested on a Mac but I hoped it'd work because Mac uses bash.

I've had two issues reported:
https://github.com/zim0369/pretty-kitty/issues/4
https://github.com/zim0369/pretty-kitty/issues/3

BTW, the noscript must sound a little exaggerated since it is probably a one time fix.

https://redd.it/x2xxv8
@r_bash
How do you get the return code for FTP commands in a heredoc?

I’m using sshpass to check if a file has been successfully delivered to an FTP outbox, but no matter what I do, the return status is always 0.

Here is my current approach:

sshpass -e sftp user@host << EOF
cd outbox
ls notafile # non-zero status
exit
EOF
echo $? #0
# Alternatively
echo ${PIPESTATUS0} # 0

How do I get the actual non-zero status code?

https://redd.it/x2uluo
@r_bash
Why powershell has more members than bash?

IDK is it appropriate to ask this question here. I'm wondering why powershell subreddit has more members than bash subreddit? :D. Shouldn't bash be more popular than powershell due to popularity of Linux servers?

View Poll

https://redd.it/x3fyoh
@r_bash
Two pieces of advice

I have been answering shell noscripting questions on Stack Overflow, on and off since 2013. As a result of doing so, there are two things that I have learned, which I wanted to pass on to anyone here who might be interested.

a} Learn to use the three utilities Ed, tr, and cut.

In my observation, the only two shell programs that anyone on SO uses are Awk and sed. I consider Ed the single most versatile noscripting utility that I have ever discovered. If everyone who asked questions there knew how to use Ed alone, I honestly think it would reduce the number of noscripting questions the site gets by 90%. Although I do use Ed interactively as well, my main use of it is in noscripts, via embedded here documents.

Although knowledge of Ed's use has been almost completely forgotten, this book about it exists. I would encourage everyone here who is willing, to read it. I also offer my own SO Answers tab which contains examples of how to use Ed in noscripts, although I am still learning myself.

b} Learn to search vertically as well as horizontally.

Most questions which I answer on SO, are about how to extract substrings from a much larger stream of information, and a lot of the time said information is all on a single line.

I have discovered that complex regular expressions are usually only necessary for sorting through a large single line from left to right. If I use the tr utility to insert carriage returns before and after the substring I want, I can isolate the substring on its' own line, and it will then generally be much easier to use cut to isolate it further. I find writing complex regexes very difficult, but identifying nearby anchors in a data stream for inserting carriage returns is usually much easier.

I really hope these two suggestions help someone. I don't know how to pass them on to anyone on SO really, but given how valuable they have been to me, I wanted to make sure that I communicated them to someone.

https://redd.it/xf08td
@r_bash
Issue with cron running a sh noscript / app

Hi all,

&#x200B;

I am trying to make a bash noscript (called mynoscript.bash) that will check if an app (called TheApp.sh) is running and if it's not, launches it.

Here is my code :

ps aux | grep TheApp | grep -v grep > /dev/null 2>&1

if $? -eq 1
then

bash /path/to/TheApp.sh > /dev/null 2>&1 &!

fi

&#x200B;

So my noscript is running well when i run it myself (bash mynoscript.bash) but doesn't work from cron.

&#x200B;

Cron line for my noscript :

/bin/bash /path/to/mynoscript.bash

I have some log files that states that my noscript is played by crontab BUT it doesn't do the "bash" line at all.

&
#x200B;

I also tried to only run the app from cron and that doesn't work either and i don't understand why :

/bin/bash /path/to/TheApp.sh

The app doesn't launches at all, i tried with /bin/sh but it's the same and the shebang of the app is /bin/bash. But if launch it myself it works just fine (bash /path/to/TheApp.sh)

&#x200B;

I am using my own account and not the root account but i also added these lines in the root's crontab but it's still the same.

I checked the /var/log/syslog for cron error but there is no error showing up at all, just some lines stating that the cron job is done i guess ?

Sep 15 17:35:01 computer CRON24307: (user) CMD (/path/to/TheApp.sh)

&#x200B;

Am i missing something ? I am running on Ubuntu 20.04.5 LTS.

https://redd.it/xf01ko
@r_bash