r_bash – Telegram
bash completion for pet

I use pet command-line snippet manager (GitHub link)

there are few commands:

Usage:
pet command

Available Commands:
clip Copy the selected commands
configure Edit config file
edit Edit snippet file
exec Run the selected commands
help Help about any command
list Show all snippets
new Create a new snippet
search Search snippets
sync Sync snippets
version Print the version number

Flags:
--config string config file (default is $HOME/.config/pet/config.toml)
--debug debug mode
-h, --help help for pet

Use "pet command --help" for more information about a command.




I write this very simple bash completion:

petcompletions() {
local cur commands

cur="${COMPWORDS[COMPCWORD]}"

commands="clip configure edit exec help list new search sync version"

if [ ${COMP_CWORD} -eq 1 ]; then
COMPREPLY=( $(compgen -W "${commands}" -- ${cur}) )
fi
}

complete -F petcompletions pet



it works,
but I would like to know if it is well written or if it can be improved

very thanks

https://redd.it/1ewqnsb
@r_bash
How to uninstall a package installed via curl?

I originally posted this in r/AskProgramming and a fellow redditor suggested I also post my questions here, just in case someone has some additional input on this. Thank you in advance for your help, and pardon the newbie questions.

Hi, everyone 😃 Any input and help is greatly appreciated.

# The Background

I recently installed the package zplug from its repo. I don't have a use for it anymore, however. So, I would like to uninstall it.

I installed the package (regrefully so) via curl, rather than using my handy-dandy brew package manager.

I did this because the project's recommendation was to install via curl:

>The best way (source)

I've uninstalled curl-installed programs before thanks to the devs providing an easy way to do so, via a simple command (like Starship).

# The Problem

I don't know how to reverse engineer the installer noscript for zplug to correctly uninstall the package, and any other files it may have created in my system.

# Questions

1. Is there a tool I can install to programmatically fetch any binaries and related files installed via curl , and then uninstall them?
2. If not, could you please explain how to go about manually uninstalling curled binaries and their files?

# My Setup

Operating System: macOS Sonoma 14.6.1
ZSH version: zsh 5.9 (x86_64-apple-darwin23.0)
`which zplug`: `zplug not found`
Files are indeed within my home directory, though.

https://redd.it/1ex1zlf
@r_bash
Linux Bible and error

I have been going through the Linux Bible by Christopher Negus. In it he discusses using aliases. He gives an example to use

alias p='pwd ; ls -CF'

whenever i run that I get

ls -CF:not found

I then enter ls --help and can see both C and F for arguments. I can type ls -CF from terminal and it will show the files formatted and in columns. However, when using it with the alias command it is not working.

Is there an error in the book? I have also ensured that /bin is in $PATH

I also tried to run it as root and I still received the same error.


UPDATE: well i figured out what was going on. I was using putty and was ssh into my machine. I went directly to the machine and entered the command and it was fine. so weird thanks all.

https://redd.it/1ex3j5w
@r_bash
pong.bash

Was bored so I wrote this bad pong game in bash... https://0x0.st/XJg2.bash

https://redd.it/1ex4bvf
@r_bash
For loop, grep, and curly braces

Me again :)

Today, I had to pull this one out of the hat. I found it tough, as I wanted to use the curly braces as a range. Like I've done in the past for simpler queries (E.g - {0..9} and at the beginning of this command!). But a colleague of mine pointed out, when using grep, you cannot do this, and instead they are interpreted as repetition operators. I confirmed this, myself, when reading the man page. However, only after stepping away did things click.

I must point out as well, we were after logs from a specific time/ date (15:40-15:59)

I hope this is somewhat useful to those who are, like me, trying to get a firm grasp of bash.

Here is what was ran:-

for i in servers-{1..3}.domain.io; do ssh user@"$i" "zgrep -E '16/Aug/2024:15:4-50-9:0-9{2}' /var/log/nginx/access.log-20240817.gz >> /tmp/outfile"; done

https://redd.it/1exkfum
@r_bash
what is a "string"

hello, i keep hearing people talking about "strings"?

what is a string? what are people talking about?

thank you

https://redd.it/1exmlum
@r_bash
Remote launch bash noscript with ssh problem

sshpass -p "pass" scp ~/Documents/Scripts/bash2.sh remoteuser@ip:~/Documents/
sshpass -p "pass" ssh remoteuser@ip bash -s < ~/Documents/bash2.sh
exit

There are no problems with scp, but the second command searches bash2 on my computer, but I need it to search on a remote PC.

https://redd.it/1eydjjp
@r_bash
How do i activate a wireless network?

I have a headless raspberry pi. I messed up some wifi settings by adding a new access point, then ssh connection closed. I rebooted the device hoping it would connect to the new AP but it couldnt connect.

I have access to the root filesystem through sd card. And when i check /etc/NetworkManager/private_networks, i see that configuration is correct but i guess its not automatically connecting. It doesnt connect to the old network as well...

How do i activate specific network from root filesystem on my raspberry pi?

https://redd.it/1eyiw8r
@r_bash
Can someone please explain this cryptic noscript?

I'm not able to follow the awk and apt-* commands. I need every piped command explained. Thank you!

```txt
# source: https://github.com/nodejs/docker-node/blob/main/20/bullseye-slim/Dockerfile

apt-mark auto '.*' > /dev/null \
&& find /usr/local -type f -executable -exec ldd '{}' ';' \
| awk '/=>/ { so = $(NF-1); if (index(so, "/usr/local/") == 1) { next }; gsub("^/(usr/)?", "", so); print so }' \
| sort -u \
| xargs -r dpkg-query --search \
| cut -d: -f1 \
| sort -u \
| xargs -r apt-mark manual \
&& apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false
```

https://redd.it/1eyi703
@r_bash
learning bash

hi i am learning bash (on kali) and i cant figre out what is the error tryid ai but with no luck code:

#!/bin/bash



read -p 'username: ' name

read -sp 'password: ' pass

entered = $1



echo your user name is: $name your password is: $pass inputted number is: $entered



if someone recommend a totrail say to me

https://redd.it/1eyo77n
@r_bash
awk delimiter ‘ OR “

I’m writing a bash noscript that scrapes a site’s HTML for links, but I’m having trouble cleaning up the output.

I’m extracting lines with :// (e.g. http://), and outputting the section that comes after that.

curl -s $url | grep ‘://‘ | awk -F ‘://‘ ‘{print $2}’ | uniq

The problem is that some sites use single quotes for certain links and double quotes for other links.

Normally I’d just use Python & Beautiful Soup, but I’m trying to get better with Bash. I’ve been stuck on this for a while, so I really appreciate any advice!

https://redd.it/1eyt150
@r_bash
what separates a string in bash?

so i didn't want to have to make a completely new thread for this question, but i am getting two completely different answers to the question

what separates a string in bash?

answer 1: a space separates a string

so agdsadgasdgas asdgasdgaegh are two different strings

answer 2: quotes separate a string

"asdgasgsag agadgsadg" "asgdaghhaegh adsga afhaf asdg" are two different strings

so which is it? both? or one or the other?

thank you

https://redd.it/1eyynw1
@r_bash
Can a conditional return a capture group?

Hello,

The test sample is

009026405
01009556500
226-356-839
00829029200
008-018-454
009-513-213
00383951900
000147765000

I want to use named capture groups. I want my search pattern to match every line where the number of positions of constituent digits is a multiple of 3. Thus, a line may comprise three or four groups of 3 digits each. If the trailing fourth group exists, it should match.

I thought that a method to achieve the last requirement could be a conditional syntax containing a back-referenced named group so that my pattern would go smth like this:

^(0-9{3})-?(0-9{3})-?(0-9{3})-?(?<LAST>0-9{3})?((?(\k<LAST>)\k<LAST>)$

The conditional fails as invalid syntax or (without \k), has no result or omits the 4-group number despite the named group match. Isn't it possible to back-reference a named group in the conditional?

https://regex101.com/r/owl7Ix/2


https://regex101.com/r/oJdviZ/2

https://redd.it/1eyxwqd
@r_bash
Issues with trying to store a tmp file as a variable.

I'm making something that writes an noscript that will wrap around a symlink located in /usr/local/bin

Before I was simply using

cat <<-"HEREDOC" >> "$TMPFILE"
content of wrapper noscript here
HEREDOC

then ask some questions with a for loop that would edit the $TMPFILE with sed -i and as the final step, the symlink in /usr/local/bin gets replaced with the $TMPFILE and the wrapper noscript is placed in the original place of the symlink.

I've been trying to avoid making a temp file, and instead storing the wrapper noscript in a variable as it is being made:

tmpnoscript="$(cat <<-'HEREDOC'
content of wrapper noscript here
HEREDOC
)

And simply tmpnoscript$(echo $tmpnoscript | sed etc etc) to edit it. Which works all nicely.

Now here is where the problems start.

I tried doing:

$SUDOCMD echo "$tmpnoscript" > "$TARGET"

To the replace the original mv "$TMPFILE" "$TARGET" I was doing before.

$TARGET is the path to the symlink
$SUDOCMD is either sudo or doas depending on what's available

The first issue I had was that the echo "$tmpnoscript" > "$TARGET" was following the symlink and replacing the actual file that the symlink pointed to, so I fixed that issue by changing it to:

$SUDOCMD rm -f "$TARGET"
$SUDOCMD echo "$tmpnoscript" > "$TARGET"

For some reason the last step is giving me a permission denied error? but SUDOCMD is being set to doas in my case and it works to remove the $TARGET symlink, why does it fail right after?

https://redd.it/1ezjfuy
@r_bash
Unable to remove a word from the names of files

I have a collection of files whose names are in the following order:

ABC Company 01 Priority Member Account.docx
ABC Company 02 Priority Member Account.docx
ABC Company 03 Priority Member Account.docx
......and so on.......

I wish to remove the words `Priority` and `Account` from the each of the above files such that the results are:

ABC Company 01 Member.docx
ABC Company 02 Member.docx
ABC Company 03 Member.docx
....and so on.....

In a terminal, I typed:

username@hostname:~$ cd test
username@hostname:~/test$ for f in *Priority *; do mv "$f" "${f/Priority}"; done
mv: cannot stat '*Priority': No such file or directory
username@hostname:~/test$

Despite the warning message that `mv: cannot stat '*Priority': No such file or directory` the names of the files were changed to:

ABC Company 01 Member Account.docx
ABC Company 02 Member Account.docx
ABC Company 03 Member Account.docx
......and so on...........

Next, I wish to remove the word `Account` from the names of the files.

In a Linux terminal, I typed

username@hostname:~/test$ for f in * Account; do mv -n -- "$f" "${f//Account }"; done
mv: cannot stat 'Account': No such file or directory
username@hostname:~/test$

Nothing happened. The word `Account` still remained.

Could someone provide me the correct command please?

Thanks.

P.S.: Do you think that the command `for f in *Priority *; do mv "$f" "${f/Priority}"; done` can be improved?

https://redd.it/1ezwtx8
@r_bash
My build_and_run.sh evolution during learning how to C & bash
https://redd.it/1f07x29
@r_bash
Output coloring

Bash Script

When running this command in a noscript I would like to color the command output.

echo
log_message blue "$(printf '\e[3mUpgrading packages...\e[0m')"
echo
if ! sudo -A apt-get upgrade -y 2>&1 | tee -a "$LOG_FILE"; then
log_message red "Error: Failed to upgrade packages"
return 1
fi

output:

[https://ibb.co/jMTfJpc](https://ibb.co/jMTfJpc)

I have researched a method of outputting the command to a file making the color alterations there and display it. Is there a way to color the white output without exporting and importing the color?





https://redd.it/1f0bb8z
@r_bash
GitHub - TheKrystalShip/KGSM: A bash cli tool to install/update/manage game servers

https://github.com/TheKrystalShip/KGSM
I've been working on this for the past few months and I'd like to share it with the community. This is my first project in bash, pretty much learned as much as I could along the way and it's at a point where I feel relatively confident about putting it out there for other people to see/hopefully use.

It's a project that came into existence because of my own personal need for something exactly like this (yes I know about the existence of LGSM, nothing but love to that project <3) and I wanted to try and challenge myself to learn how to make decent bash noscripts and to learn the internals of the language.

If you're in the market for some light tinkering and you happen to have a spare PC lying around that you can use as a little server, please try out the project and leave some feedback because I'd love to continue working on it with new outside perspectives!
Thank you for your time

https://redd.it/1f0etrn
@r_bash