r_bash – Telegram
Script to change system spelling language

Right now I'm using an AppleScript to do this, but the noscript is a bit long to execute, plus it shows some UI elements and I'd like for the process to be hidden.

Is there a defaults write command that would allow to toggle the system spelling language between US English and French?, or at least to switch from one to the other?

Thanks.

https://redd.it/114wjw1
@r_bash
posix Properly store positional arguments in string

I have the following noscript:

cmd="rg --ignore-case --files-with-matches {q} $@"

file=$(
FZFDEFAULTCOMMAND="rg --files $@" fzf \
--bind "ctrl-a:select-all" \
--bind "change:reload:$cmd" \
--disabled \
--preview "rg --ignore-case --pretty --context 2 {q} {}"
)

Here I store the positional arguments (paths for rg to search from) into the string variable $cmd, which is then passed as a literal string in --bind "change:reload:$cmd" \. However, shellcheck throws an error regarding assigning array to string that I don't understand if it's relevant.

To test, I made a directory with a space in its name and ensures it should containing a match, then past this as an argument to the noscript quoted. Sure enough, it doesn't show up so apparently the directory named "test me" is not interpreted as "test" "me" where the quotes are lost.

So is the issue here that "quotes" are lost since POSIX doesn't support arrays, making everything space delimited, which is problematic when you also have spaces in the arguments? How would I go about properly storing the positioning arguments with them quoted to account for spaces in the cmd string then? I'm pretty sure the suggested solution from shellcheck does not apply--the page doesn't mention any sort of quoting issue either.

Essentially, the line --bind "change:reload:$cmd" \ should be expanded to e.g.

--bind "change:reload:rg --ignore-case --files-with-matches {q} '/home/user/bin' '/home/user/test me'" \

when the noscript is passed the arguments: ~/bin "~/test me"

Much appreciated.

https://redd.it/1150jis
@r_bash
File with a variable of the date as part of the filename.

I am trying to use bash to create a backup of an existing file with a variable of the date as part of the filename.

    #!/bin/bash
dateandtime= date +%F-%H-%M-%S
sudo cp /etc/pacman.d/mirrorlist /etc/pacman.d/mirrorlist${dateandtime}.bak
echo "Mirror file backup created: /etc/pacman.d/mirrorlist${dateandtime}.bak"
sudo reflector --latest 20 --protocol https --sort rate --save /etc/pacman.d/mirrorlist
echo "Mirror list update complete."

This is the output I get
    Mirror file backup created: /etc/pacman.d/mirrorlist.bak

When looking in the file directory with a file manager I can see that is the only file that exists and it just keeps getting overwritten. sorry if this is a silly issue but I'm new to bash and I looked for about an hour on various forums and how to guides but was not able to find the solution.

https://redd.it/1159gor
@r_bash
Need helping figuring out what's wrong with this noscript on MacOS

I'm currently trying to turn some bash noscript that's in the form of a '.command' file into an App/Quickaction that can be run, using Automator on MacOS.

This is the original noscript: \(pastbin link\)

This is as far as I've gotten: \(pastebin link\)

When running the noscript, the finder window appears to choose a folder, I get a GUI popup to input a password, but then I get this error:

The action “Run Shell Script” encountered an error: “-: -c: line 143: syntax error: unexpected end of file”

I'm not really sure how to end this noscript, googling is not helping in this specific case either.

Any help would be appreciated!

https://redd.it/115a407
@r_bash
How to force a noscript to wait for its child gnome-terminal windows to finish before continuing its own execution

How can I force a noscript to wait for its child gnome-terminal windows to finish before continuing its own execution? When I run the noscript below, the father noscript does not wait for the children to finish execution and outputs "Finished" while they are still running.

I have this code:
gnome-terminal -- bash -c "cd /home;find . -name "foo" | tee /home/$USER/log-home.txt";
gnome-terminal -- bash -c "cd /home/
$USER; find . -name "foo" | tee /home/$USER/log-$USER.txt";
wait;
echo "Finished";

Thanks

https://redd.it/115cwde
@r_bash
mapping to control key

Hi,

is it possible to map a function key (f9 say) to a key with control-prefix (control-n say)?

I have tried

bind "\e20~":"\C-n"

But that does not work...

Many thanks.

[https://redd.it/115hree

@r_bash
Trouble finding info on for loops that include and print out user input?

I can't seem to find any examples online of a noscript that repeats user input to stdout a certain number of times??

#!/bin/bash
# Display message 5 times
for ((i = 0 ; i < 5 ; i++)); do
echo "Welcome $i times."
done

Here's a C-style for loop but what if I want to repeat a string, like "look at this repeat" for a set amount of times? I feel incredibly dumb I can't do this.

https://redd.it/115k1ot
@r_bash
Bash on M2 not accepting multi-line code.

Previously, I've always been able to paste my noscripts into iTerm/ Bash 5 without issue. Ever since migrating from the 2018 mini to the 2023 M2 Mac mini, no multi-line code seems to work. I'm even having trouble running noscripts "./mynoscript.sh" after chmod of 777. Am I going crazy?

https://redd.it/11603we
@r_bash
I have files in .epub and .mobi format, most are in both formats, but I want to find out (recursively) if any are in one format only

Just to make it clear, the basenames are the same, only the extensions differ if a file is in both formats. Files could be anywhere in a directory tree, but always grouped together.

Is there an easy way (which I can't think of at the moment) to spit out the unique files in one format which have no counterpart in the other format?

https://redd.it/116kl4b
@r_bash
bash-completor: Creating a bash completion noscript in a declarative way

Writing a [programmable completion](https://www.gnu.org/software/bash/manual/html_node/A-Programmable-Completion-Example.html) for Bash is difficult for novices. It may take much time on debugging.

Even though there is a library of completion noscripts like [scop/bash-completion](https://github.com/scop/bash-completion), it only includes common commands. For some commands that are not commonly used, we have to write completion noscripts by hand.

To reduce the painful time developers spend with developing, I created [bash-completor](https://github.com/adoyle-h/bash-completor).

It is a Bash completion noscript generator written in bash noscript. It provides a declarative way to help developer to implement bash completions quickly.

## Feature

* Declarative programming. You only need to know the most basic bash syntax.
* Only `bash` and `sed` are needed on at compile time. Only `bash` is needed on at runtime. No other dependencies.
* Support for command format looks like `<cmd> [options] [arguments]`.
* Support for sub-commands, which looks like \`<cmd> \[cmd-options\] <subcmd> \[subcmd-options\] \[arguments\]\`.
* Support for `-o`, `--option`, `--option <value>`, `--option=`, `+o`, `+option` format.
* Support for completing file or directory paths.
* Support for completing word lists.
* Support for custom completion functions.
* Friendly config typo checking and suggestions.

## Requirements

* For building noscript.
* Bash v4.3+
* cat, sed (GNU or BSD compatible)
* For runtime.
* Bash v4.0+

# [Installation](https://github.com/adoyle-h/bash-completor#installation)

## Usage

Take the example of creating bash-completor's own completion noscript.

First, create the configuration file `completor.bash`.

The configuration file is also written in Bash syntax. You only need to know the most basic Bash syntax.

output=dist/bash-completor.completion.bash
authors=('ADoyle (adoyle.h@gmail.com)')
cmd=bash-completor
cmd_opts=(
-c:@files
-h --help
--version
)

That's all. Then execute `bash-completor -c ./completor.bash` to generate the completion noscript. Done.

For complex examples, see below links.

* [zig.completor.bash](https://github.com/ziglang/shell-completions/blob/master/zig.completor.bash)
* [nvim-shell-completions/nvim.completor.bash](https://github.com/adoyle-h/nvim-shell-completions/blob/master/nvim.completor.bash)
* [Other examples](https://github.com/adoyle-h/bash-completor/tree/master/example)

Read [this document](https://github.com/adoyle-h/bash-completor/blob/master/docs/syntax.md) for the syntax.

## The completion noscript

The generated completion noscript follows below code style. No worry about naming conflict. And it's easy to debug at runtime.

* The main command completion function must be `_${cmd}_completions`
* All subcmd completion functions must be named with prefix `_${cmd}_completions_${subcmd}`
* All other variables and functions must be named with prefix `_${cmd}_comp_`
* The variable of main command options must be `_${cmd}_comp_cmd_opts`
* The variable of subcmd options must be named with prefix `_${cmd}_comp_subcmd_opts_${subcmd}`
* All reply functions must be named with prefix `_${cmd}_comp_reply_`
* All customized reply functions must be named with prefix `_${cmd}_comp_reply_custom_`

&#x200B;

If you like it, please give a star to the repo. [https://github.com/adoyle-h/bash-completor](https://github.com/adoyle-h/bash-completor)

https://redd.it/116qbw9
@r_bash
How to add an input that is actually multiple hexadecimal elements

I have an input like this
“30 0A 0F” that is enter into the command line

I’m not sure how to loop through and add these numbers. I have tried using printf but I don’t have a decimal to hex conversion. I need these strings to individually be casted hexadecimal as is.

How can I do this?

https://redd.it/117gs0u
@r_bash
How to edit my git command to output requested data using string separator?

Using git I'm getting a list of all large files in a repo. The command looks scary but it's really just pulling data and then doing some data engineering.

git rev-list --objects --all |
git cat-file --batch-check='%(objecttype) %(objectname) %(objectsize) %(rest)' %(id)' |
sed -n 's/^blob //p' |
sort --numeric-sort --key=2 |
gcut -c 1-12,41- |
$(command -v gnumfmt || echo numfmt) --field=2 --to=iec-i --suffix=B --padding=7 --round=nearest

Here is an example output of what the noscript above pumps out:
ne3ee2e43e12 1.0MiB path/to/large/files.png ID

I would like to use a semi-colon as a delimiter between each datapoint called in the command. So the expected output would be like this:

ne3ee2e43e1x; 1.0MiB; path/to/large/files.png; ID


I can do so like this:

git rev-list --objects --all |git cat-file --batch-check='%(objectname);%(objecttype);'

Adding semi-colons in between each git object. But for some reason when I combine everything together, the noscript does not work?

How can I fix my noscript below to get it working and add semi-colon separators for my --batch-check param?

git rev-list --objects --all | git cat-file --batch-check='%(objecttype);%(objectname);%(objectsize);%(rest)' | sed -n 's/^blob //p' | awk '$2 >= 2^20' | sort --numeric-sort --key=2 | gcut -c 1-12,41- |$(command -v gnumfmt || echo numfmt) --field=2 --to=iec-i --suffix=B --padding=1 --round=nearest

https://redd.it/117li9u
@r_bash
Script cannot use text file given as command line arguments

Practicing some basic bash to prepare for a new job. I am using a cygwin terminal on windows 10.

I am trying to output the text from a file (numbers.txt in my cwd) which I am entering as parameter with this line.

&#x200B;

cat $1

&#x200B;

I keep getting this error

&#x200B;

cat: 'numbers.txt'$'\\r': No such file or directory

&#x200B;

What am I doing wrong?

https://redd.it/117mqzt
@r_bash
how do I use/save bash files on windows and execute?

i tried looking it up but i either see download extension for my code editor which hasnt worked or use linux which i can't rn. is there something im missing?
Edit: solved
I used vs code and ctrl + shift +p then selected default profile and chose bash which let me execute the bash file

https://redd.it/117mdtv
@r_bash
decrypt a file in bash

i have a scrip that i am looking to automate so as to have it run once a month. my issue is that this bash noscript reads the password file but cannot come up with a way to secure that password file so its not in plain text. any idea how i can encrypt and decrypt that password file and decrypt it using the noscript and still keep it secured? decryption inside the noscript is not working as its simply leaving the key under the door

https://redd.it/117xgr0
@r_bash
Hello guys how can I revert regex

cat misc.txt | egrep '[[:digit:\]\]{1,2}-[A-Za-z\]{3}-[[:digit:\]\]{4}'

on 18-Jun-1815 outside Brussels, Belgium lasted a whole day and

The American George B. Selden filed for a patent on 8-May-1879.

&#x200B;

Hello guys I have problem reversing the dates in a format where the year is first like 1815-jun-18 without using date any help will be much appreciated

https://redd.it/117yz67
@r_bash
Headphone indicator bash noscript help

Script suppose to show an headphone icon when the headphone is contented, and remove it when the headphone jack is removed.

the problem is with the pid and kill pid, I can remove the icon from the terminal using pidof and kill commands but it doesn't work from the noscript.

#!/bin/bash
FLAG=1
while true; do
HEADPHONE=$(amixer -c 0 contents | awk -F "=" 'FNR == 3 {print $2}')
if [ "$HEADPHONE" = "on" && $FLAG -eq 1 ]; then
yad --notification --image="/home/oren/Documents/headphone.png"
PID=$(pidof yad |awk '{print $1}')
FLAG=0
echo "$(date) on flag $FLAG PID $PID" >> "/home/oren/Documents/headphone.log"
fi
if [ "$HEADPHONE" = "off" && $FLAG -eq 0 ]; then
kill '$PID'
FLAG=1
echo "$(date) off flag $FLAG PID $PID" >> "/home/oren/Documents/headphone.log"
fi
sleep 1
done

https://redd.it/1183pvy
@r_bash
Systemd in a Debian container on ChromeOs accepts bash

Yup, as long as your noscripts executed by a systemd service starts with a bash shebang, things are good. Arrays, and [[ \]\] and other stuff works totally fine.

Even if the documentation states that the noscripts must be sh compatible.

https://redd.it/118fy6j
@r_bash
Ctrl + Meta(Windows) + Right/Left - How come it isn't well known?

So I've accidentaly (I have ctrl + alt + left/right bound as home/end) discovered that ctrl + meta + left/right in bash jumps the cursor between words/commands/arguments/whatever, separated by any non-alphanumerical sign, like whitespaces, slashes, underscores, so for example instead of manually replacing file name in a long path I can easily jump to the last slash and use alt+d to remove the filename and type another.

Why I haven't seen this anywhere in 20 years of using linux? I couldn't find any docs using Google either, and it is soooo useful.

https://redd.it/118jmc6
@r_bash
I need some bash help

I need to convert all my ps1 games to chd files (type of compressed data) and I'm having trouble with part of automating it. What i need to do is used sed to get the file names (or if there is a way to strip extensions then use that) and then for everyone one of those files extract it, go into the folder created run chdman, copy the .chd file back to the main directory with the same name then move on to the next one. How can i run that for every file named that. I could probably do it with the find command and not need to do it through bash but I'd like a file to run that would do this

https://redd.it/118mjtg
@r_bash