r_bash – Telegram
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 🧩 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
Where to install config files?

I'm writing an install noscript so when someone inexperienced downloads one of my noscripts they can run the install.sh noscript to set everything up for them.

At first I wrote it to create $HOME/bin and $HOME/bin/config folders (if they didn't exist). But some of my noscripts are written for Synology NAS devices and I remembered that on a Synology you can disable home folders. So I changed my install noscript to check if the $HOME folder exists, and if not then install the noscript in /etc/local/bin. But the config file and exclude files (for tar and rysnc) need to easily accessible and editable by inexperienced users.

Where should extra files that a noscript needs normally be installed?

If you want to see, and critique, what I've written so far it's here: https://gist.github.com/007revad/8df1075b6a67168e1c2e9042de42885b

https://redd.it/10ycc31
@r_bash
can someone help me be able to send a zenity window to a logged in user?

I feel like I have tried every combo of modifying the DISPLAY variable but cannot get the window to pop up for that user.

Does anyone have a trick or a way to send a zenity (or equivalent) window to a logged in user.

https://redd.it/10yehfn
@r_bash
is that can be done beneath only bash noscript ?

goal is

entering(editing) multiple text files (10s+) and exchange a specific line 'a line that exist in whole given text files exactly the same' exchange this line as it's few words ..exchange it to a given new line 'words' that will be in putted with 'read' as input from user 'user is me' , to change that word to anew given word for those multiple text files as only one time by doing such a noscript ? that can be only inside a bash noscript ?

https://redd.it/10ygj3q
@r_bash
identification of duplicate files within folders AND archives

I purposefully changed this from deduplication as, normally, files within zips are there for a reason, but I have stacks of zip files, and extracted zip files (yah know how lazy it is to extract but keep the zip for posterity).

I'm interested in a noscript that will generate sha1 all files within an folder of all files AND archives (gz,xy,zip,rar,7z) and output path and checksum to a text file, then another which will either identify duplicates or remove unique SHA1 checksums leaving me with a curated list of files which I can go through.

Has anyone done anything similar to this before pls ?

https://redd.it/10yq5kg
@r_bash
printing from background

I have multiple tasks that run in the background. They are detached with nohup and &

I would like to print a message to stdout when i am done. (This code is part of a "multithreading"-noscript that allows me to run multiple instances of a command easily)


what i have currently is the following:

$command &>> log.txt &

&#x200B;

Just adding an echo does not work:

$command &>> log.txt && echo "$command: success!"

I would have to wait for $command to finish, which breaks my noscript. Can i print to the foreground shell from a background task?

https://redd.it/10yvxcq
@r_bash
Testing an unset variable returns true... always?

Running into some unexpected behavior. When testing if an unset variable is empty/non-empty, it always returns true.

I'm seeing the following:
$ echo ${TEST}

$ test -z ${TEST}; echo $?
0
$ test -n ${TEST}; echo $?
0


This also happens using if [ ... ] as well. This happens when the variable is set to an empty string too (TEST='')

Tested this on rhel 8 and ubuntu 22.04, is this expected behavior? I am confused.

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