r_bash – Telegram
How to run a background noscript when a user logs in?

I have a bash noscript I need to run in the background on macOS. I want it to launch automatically when a user logs in. I tried all approaches that I could find and nothing worked as expected.

The closest solution was a LaunchDaemon. However, I need my noscript to be located and to have access to "\~/Downloads" and LaunchDaemon is unable to do that :(. If you try to run a noscript from "\~/Downloads" it will fail with 126 or 129 error (I don't remember). If you put it somewhere else and try to access "\~/Downloads" it will fail.

I also tried to create an Automator app and add it to "Login Items", but it looks like it starts my noscript and kills it immediately, I don't see it running in the background. My noscript is launching `fswatch` and observes a file all the time.

I even tried adding my noscript using `crontab -e` with "@reboot /my\_noscript", but it doesn't work either. It looks like it doesn't launch at all.

Any ideas? My main requirements are:

* operating system: macOS Ventura or lower
* the noscript will be running in the background after each login
* the noscript is located in "\~/Downloads"
* the noscript has access to "\~/Downloads"
* the noscript has access to packages installed via Homebrew for the current user

https://redd.it/10vc5gw
@r_bash
How do I synchronize threads?

These ideas didn't work

#!/bin/bash

stop=0
# (for ((i=0 ; i<10 ; i++)) ; do echo $i"/10" ; sleep $i ; done ; stop=1 ; echo stop="$stop" ) & # Doesn't work
# for ((i=0 ; i<10 ; i++)) ; do echo $i"/10" ; sleep $i ; done ; (( stop++ )) ; echo stop="$stop" & # Doesn't work
for ((i=0 ; i<10 ; i++)) ; do echo $i"/10" ; sleep $i ; done && stop=1 && echo stop="$stop" & # Doesn't work

echo waiting
while (( stop==0 )) ; do echo "Waits stop=$stop" ; sleep 5 ; done
echo Done :\)

https://redd.it/10vjmla
@r_bash
Is there a repository with snippets to manipulate csv data and create them from the data inside the file system (folders and folder content and file extensions)?

Just trying to write some useful noscript for internal tracking for myself to get better at bash, because the Udemy tutorials I watch goes from one ear and goes out to the other.

https://redd.it/10vl9z9
@r_bash
Minimal setup for shellcheck as a compiler in Vim for linting bash noscripts.

Hello.

This is the most minimal setup I wanted, because I don't have time to fiddle with this, and I have a setup for quickfix lists, with keyboard shortcuts I remember.

I run 9.0, but there is only 8.x code in the files.

So, I have an sh.vim that I have dropped in `~/.vim/ftplugin` it`s below:

" Vim-licence (c) 2023 McUsr
" Miniscule shellcheck setup
" you :copen the quick fix list however you open the quickfix list!
compiler shellcheck

nnoremap <buffer> <F9> :silent exe "make %:p" <bar> redraw!<CR>


And then there is the sh.vim file in the `~/.vim/compiler` folder file below:

" Vim-licence (c) 2023 McUsr
" Miniscule shellcheck setup
" you :copen the quick fix list however you open the quickfix list!
"
if exists("current_compiler")
finish
endif
let current_compiler = "shellcheck"

CompilerSet makeprg=shellcheck\ -f\ gcc\ \"%:p\"
CompilerSet errorformat=
\ '%f:%l:%c: %trror: %m,' .
\ '%f:%l:%c: %tarning: %m,' .
\ '%I%f:%l:%c: note: %m'


And that is pretty much all there is to it, after I dropped the files in their respective folders I now hit F9 to lint a shellnoscript in the active window.
I hit `\co` to open the `quick fix list`, and then I can jump to the error messages.
FYI. I nicked the format string and the reload-guard from [here](https://github.com/itspriddle/vim-shellcheck).

Enjoy, if you aren't set up already.

https://redd.it/10vmjcp
@r_bash
why from a function in .zshrc i can't process files in another folder ?

I have a noscript that use [Swaks](https://www.jetmore.org/john/code/swaks/) i try to be able to run it from a simple command in my terminal without having to go to the right folder and from their open terminal and run the noscript.

cd .noscript
cd swaks
sh email.sh

I made this function in my .ZSHRC

&#x200B;

function email {
zsh $HOME/.noscripts/SWAKS/email.sh
}

Program run, but the program doesn't process the file **mod.html**

&#x200B;

&#x200B;

#!/bin/bash
# encoding: UTF-8

while true; do
echo "give email adress"
read -r email
echo "PRICE:"
read -r prix


email_regex="^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$"


if [[ $email =~ $email_regex ]]; then
cp template.html mod.html
echo "Adresse email valid."

if [ -z "$prix" ]; then
sd ppprice " " mod.html
else
declare -i prix
if [[ "$prix" -gt 350 && "$prix" -lt 1500 ]]; then
sd ppprice "the price of this is $prix&euro;" mod.html
else
clear
echo "PRIX INCORECT POUR UNE VENTE";
rm mod.html;
exit 1
fi
fi
else
clear
echo "bad email";
exit 1
fi

swaks --body @mod.html -t "$email" --add-header "MIME:Version 1.0" --add-header "Content-Type: text/html" --h-Subject "buy my product" --h-From: '" my name" <xxxxx@gmail.com>' -s smtp.gmail.com:587 -tls -au xxxxxx@gmail.com -ap ejixxxxxxxlelinp -f xxxxxxx@gmail.com --tls-protocol tlsv1_3
clear
rm mod.html;
done

&#x200B;

https://preview.redd.it/8zjt7qg6tnga1.png?width=315&format=png&auto=webp&v=enabled&s=dcfda9f7d80e13e2068e1fa6dbe69007bce7980c

https://redd.it/10vmzpj
@r_bash
oh ..what a so many variety of 'output' bash noscript have to have?

i know there's visible output in the terminal and there some others are hidden while doing it's process.

my goal 'straight question' is : i need to export whatever is the bash noscript is writing to a text file , what ever kind of out put is happening 'or not' i want it to be exported to a text file, then after use same noscript again it creates A NEW text file to save those logs OR in same old text file but without wiping the old logs , well ..should i study those standard outputs or someone just tell me how things going ?

what sort of syntax should i add to the noscript to save whatever output happened and write it to specific text file into specific directory ? if someone have time explain to me please . that was my real question for real people .

,if my question doesn't seems cool to you, hold on please don't rush to be a reason for infection of the downvote button syndrome here the question you will like bro. what types of bash outputs and how to manage which is to write and be visible and which to hide if needed 'in case those outputs not needed' thanks in advance .

https://redd.it/10vr5gk
@r_bash
How would you fuzzy find a file then open it in your editor?

I have a directory with a large number of files (a second brain). I often want to find the full path to a file, then open it with an editor (say vi). Normal, linear tab completion of the path is useless because a) the filenames start with meaningless dates and b) I want to fuzzy search for parts in the middle of the filename because I can't remember how I named each file.

I want to do this from the CLI only, not e.g. from within a full vim or emacs setup, because I'm doing it on Android in Termux and I don't want to set all that up. So basic bash stuff preferred. But some functions or aliases for a .bashrc are on the table.

I don't know a quick way to do this. How would you?

Is there another place you recommend that I post this? Is there a google search that could have led me to a solid answer?

https://redd.it/10vwf1m
@r_bash
Using expect to execute remote curl command with ssh



Hi guys, i do have following situation:
I am running a Bash-Script on a Linux Machine. From this Linux Machine i need to ssh to another Server on which i want to execute two curl-Commands. Those curl-commands will be aiming a machine which has no public ip-address, and the private ip is only known to the host i want to run the curl-command on.

I detected "expect" for this. But either i don't know how to use it properly, or it does not even work the way i want. I need two curl commands. One will receive a token which i have to use in the second curl.
I tried it the way i would expect it to work in shell. Like saving with

token=$(curl ...)

the token into a variable, and use it afterwards in the second token commands. I tried this but it didn't work, as it tried to resolve $(curl ...) as a variable which it says is unknown.
What would be the right way to do this?

https://redd.it/10vxcaa
@r_bash
Need help maintaining emoji after deleting elements in XML file with Xmlstarlet

I'm trying to minimize the size of an xml file with these code.

sed -i 's/\x1B/X/g' "/storage/emulated/0/Tasker/configs/cache.xml"

xmlstarlet ed -P -d '//Actionnot(code="130" or code="300")not(code = "300" and starts-with(label, "#desc"))' "/storage/emulated/0/Tasker/configs/cache.xml" > "/storage/emulated/0/Tasker/configs/temp.xml"

However this element value that is supposed to look like this `🧩` AutoInput V2 in the source file ends up looking like this &#x1F9E9; AutoInput V2 in the output file.

<TaskerData>
<Task>
<Action sr="act14" ve="7">
<code>130</code>
<Str sr="arg0" ve="3">&#x1F9E9; AutoInput V2 Assist</Str>

&#x200B;

How do I maintain the original value even after the delete process? I have several others element that has emoji in their value and I want to keep them.

&#x200B;

I have tried executing below command below but the emojis got reverted to the wrong emojis

sed -E 's/&#x(0-9A-Fa-f+);/\xF0\x9F\x87\xB8\xF0\x9F\x87\xBA/g'

Thankyou!

https://redd.it/10vzdg9
@r_bash
Can someone please help me understand this shell noscript

So I was handed this noscript from the guy who had my job previously, and there are no comments so I have no idea whats happening. I haven't used shell before as well. I know that this basically downloads data from a website and writes to Postgres, but not sure what the individual commands mean. Any pointers would be really helpful!

(I modified any sensitive information to generic terms)

#!/usr/bin/env bash
exec 1> command.log 2>&1
set -ex
yday=$(date --date="yesterday" +"%Y-%m-%d")
wget -O /home/user/Downloads/results.txt --no-check-certificate --quiet \
--method GET \
--timeout=0 \
--load-cookies cookie.txt \
'https://wesbite/report/getExportUri?username=something&password=something&timeZone=0&start=0&limit=999999&q=col1,col2,col3 WHERE day ="'$yday'"'
sed 's/\x22//g' /home/user/Downloads/results.txt > /home/user/Downloads/outfile.txt
wget -O /home/user/Downloads/output.csv -i /home/user/Downloads/outfile.txt
PGPASSWORD=password psql -h 10.10.10.10 -U postgres -c "\copy postgres_table_name from '/home/user/Downloads/output.csv' with csv header"
rm -rf /home/user/Downloads/*.csv
rm -rf /home/user/Downloads/*.txt
rm -rf /home/user/Downloads/*.log

https://redd.it/10w77ey
@r_bash
Ever write a noscript to see if you could do something, without considering whether you should?

I present to you, 2 simple functions.

# Compress and encode an entire folder structure to/from plain text.
function encodeFolderAsText()
{
tar -I 'gzip -9' -cv "${1}" | base64 -w0
}

# Decode and decompress an entire folder structure from plain text.
function decodeTextAsFolder()
{
cat "${1}" | base64 -dw0 | tar -xz
}

Example Usage on a folder named "noscripts".

#Test Use on a folder named "noscripts"
[ -d testOut ] && rm testOut/* || mkdir testOut;
encodeFolderAsText "noscripts" > "testOut/noscripts.base64"
(
cd testOut
decodeTextAsFolder "noscripts.base64"
)
du -sh "noscripts"
du -sh "testOut/noscripts.base64"


Why? I don't know, maybe you thought it would be a good idea to be able to turn a typewriter into a printer, and haul your documents around in a 3 ring binder, or fill a warehouse. Maybe you want to share a very small binary file over a Reddit comment. Who knows? If you find a use, let me know.

Also:

printf 'H4sIAAAAAAACAwvISU0sTlUo
zkgsSlVIzKtUKC1OzUktLlYoTi7KLCgp
VshPU6jMLy1SyC/P0+MCAA4oKM0uAAAA' | base64 -d | gzip -d

https://redd.it/10wj2ha
@r_bash
Exclude a pattern from bash command completion?

I have an annoying tab completion conflict with one of my most-used commands (I am talking about command completion in bash, i.e., not completing the arguments of a particular command).

Ideally, I'd like to specify an exclusion pattern that indicates commands that should not be matched.

https://redd.it/10wli4e
@r_bash
Compare two folders and output the filename as well as a checkbox or boolean for the columns present and not present in a csv file

Compare two folders and output the filename as well as a checkbox or boolean for the columns present and not present in a csv file. Trying to see how to pipe the content of a folder and then make a 2d array and then output it to a csv file using bash.

https://redd.it/10wl0p7
@r_bash
Using variables for storing default values for other variables

Hi! I literally have the following piece of code for setting variable if it's unset:

declare SUMMARYDESCRIPTIONPREFIX="${SUMMARYDESCRIPTIONPREFIX-Denoscription: }"

and hundreds lines later:

SUMMARYDESCRIPTIONPREFIX="$(yq '.summary.denoscription.prefix // "Denoscription: "' "$themetoset")"

to load variable value from file. What bothers me - that default value is duplicated twice. It's error prone and furthermore requires find and change things more than I want. What I want to do: to write something like this:

declare SUMMARYDESCRIPTIONPREFIX="${SUMMARYDESCRIPTIONPREFIX-$VARIABLEWITHDEFAULT}"

and:

SUMMARYDESCRIPTIONPREFIX="$(yq '.summary.denoscription.prefix // "$VARIABLEWITHDEFAULT"' "$themetoset")"

How do I do such thing for the first code line? Bash doesn't recognize $VARIABLE_WITH_DEFAULT as a variable in the expansion context.

I've tried the following thing:

getordefault() { declare -n name="$1"; declare default="$2"; if [ -v "$name" ]; then echo -n "$name"; else echo "$default"; fi; }

but it turns out that for the set but empty variable it will the provided default value.

https://redd.it/10wobbx
@r_bash
Need advice on file searching and filtering in subfolders

I have a folder that contain multiple-level subfolders, inside those subfolders may contain different srt file with ending "\_vi.srt" and "\_vi\_x.srt" (x from 1-9). The filename part are matched with its subfolder name but may contain spaces. It looks like below:

In subfolder named "Hello World":

* Hello Word\_vi.srt
* Hello Word\_vi\_1.srt
* Hello Word\_vi\_2.srt

In other subfolder named "Hello Reddit":

* Hello Reddit\_vi.srt
* Hello Reddit\_vi\_1.srt
* Hello Reddit\_vi\_2.srt
* Hello Reddit\_vi\_3.srt

And I have about 30-40 subfolders like above.

I want to checked for srt file with highest x in each subfolder and replace it to the one without x ("\_vi.srt").

Below is my noscript for checking in a specific folder but I'm still finding the way to achieve it in multiple-level subfolders:

#!/bin/sh

folder="Source"
srt_files=($folder/*_vi_*.srt)

if [ ${#srt_files[@]} -gt 0 ]; then
max_index=0
for i in "${!srt_files[@]}"; do
x=$(echo "${srt_files[$i]}" | grep -o "_vi_[0-9]*" | sed 's/_vi_//')
if [ "$x" -eq "$x" ] 2>/dev/null && [ "$x" -gt "$max_index" ]; then
max_index="$x"
max_file="${srt_files[$i]}"
fi
done

if [ -n "$max_file" ]; then
vi_file="${max_file%_vi_*}_vi.srt"
mv "$max_file" "$vi_file"
echo "Lastest SRT Version: $(basename "$max_file")"
echo "Replaced to: $(basename "$vi_file")"

fi
fi

Any advice is really appreciated!!!

Thank you.

https://redd.it/10wptye
@r_bash
Why does 1> create a file but >1 does not?

When using output redirection, 1> creates the output file if it does not exist but >1 does not. Is there a reason for this? Am I mistaken when I think that 1> and >1 are the same thing?

https://redd.it/10x99ot
@r_bash
Compare two folders and output the filename as well as a boolean and put the data inside a csv file

I need two columns, one named filename and the other called present in both folders. How do you do this? I heard 2d arrays are not possible in bash, but that we can simulate it somehow. How?

https://redd.it/10xgipr
@r_bash
expr called by echo doesn't evaluate anything

So I tried this example here:

#!/bin/bash
read -p "Enter first number: " v1
read -p "Enter second number: " v2
res='expr $v1 + $v2'
echo "Sum is : $res"

Output:

user@user:~/Desktop$ bash bash.sh
Enter first number: 2
Enter second number: 3
Sum is : expr $v1 + $v2

What's wrong? Tried res="expr $v1 + v2" didn't work

https://redd.it/10xmgs8
@r_bash
Why is this curl command giving me error?

Using the exact example command from API documentation just replacing username and password.. it throws error saying URL using bad/illegal format or missing URL. What am I missing here? Does this command not work on mac systems?


curl --location -g --request GET 'https://dashboard.api.pixalate.com/services/2022/Report/getDetails?username=user&password=password123&timeZone=0&start=0&limit=20&q=fraudType,impressions,sivtImpressions,sivtImpsRate,givtImpressions,givtImpsRate WHERE day>='\''2020-09-01'\'' AND day<='\''2020-09-10'\'' GROUP BY fraudType ORDER BY impressions DESC'

https://redd.it/10y2i33
@r_bash
what is the correct way to show follow a symlink while showing hidden files?

I am trying to fix up my noscript [published a couple of days ago](https://github.com/robss2020/rmdryrun), to correctly follow symlinks and hidden files.

What do you think [about this version that may support symlinks and hidden files](https://hastebin.com/share/ujefixojod.bash)?

I'm not 100% sure what the desired functionality is with respect to following or not following hidden files and symlinks. Right now if you list the hidden file explicitly (by giving it, rather than matching it with *) it will list it.

Let me know what you think!

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