Naming conventions for constants (readonly variables)
For variables and functions, the naming conventions seems to be snake_case. Is this also the case for all constants?
Or are primitive constants (like int, string) always SCREAMING_SNAKE CASE and non-primitive constants (like arrays) use snake_case?
https://redd.it/1go60ek
@r_bash
How to sequentially execute commands with a bash script?
I want to make a simple blocklet for i3's status bar to cycle through ddcutil "presets" for my monitor brightness. Basically, to make it execute the ddcutil command (ddcutil set 10 100
) with brightness values in a sequence like 100, 75, 50, 25, 0 (and wrap around after reaching 0) each time the script is executed. But I'm really new to bash and I'm not sure how do I do it. Is there an example available?
I'm fairly sure I'll need to make it read the current brightness value (that is reported with ddcutil getvcp 10
with the output looking like VCP code 0x10 (Brightness ): current value = 50, max value = 100
) to decide which command should it execute next time.
https://redd.it/1gphkvb
@r_bash
How can I replicate this ZSH prompt using Bash?
this is the code:
function fmt_ms() {
local total_ms=$1
local ms=$((total_ms % 1000))
local total_seconds=$((total_ms / 1000))
local seconds=$((total_seconds % 60))
local total_minutes=$((total_seconds / 60))
local minutes=$((total_minutes % 60))
local hours=$((total_minutes / 60))
local formatted=""
[[ $hours -gt 0 ]] && formatted+="${hours}h "
[[ $minutes -gt 0 ]] && formatted+="${minutes}min "
[[ $seconds -gt 0 ]] && formatted+="${seconds}s "
echo "$formatted"
}
function preexec() {
timer=$(($(date +%s%0N)/1000000))
}
function precmd() {
if [ $timer ]; then
now=$(($(date +%s%0N)/1000000))
elapsed="$(($now-$timer))"
formatted=$(fmt_ms $elapsed)
PROMPT="%(?.%F{green}%?%f.%F{red}%?%f) %F{blue}%1~%f %F{8}${formatted:+$formatted}%f%F{yellow}$%f "
unset timer
else
PROMPT="%(?.%F{green}%?%f.%F{red}%?%f) %F{blue}%1~%f %F{yellow}$%f "
fi
}
PROMPT="%(?.%F{green}%?%f.%F{red}%?%f) %F{blue}%1~%f %F{yellow}$%f "
it looks like this:
https://preview.redd.it/regotlo19d0e1.png?width=768&format=png&auto=webp&s=9ecf1f46e5ac9ea31f7442b88c4856b8cb5f98f6
From left to right it shows the status code of the last command, 0 is green, anything else is red, it shows the current directory in blue, the execution time of the last command in gray formatted with hours, minutes and seconds and finally a yellow dollar sign.
https://redd.it/1gp84d9
@r_bash
DirDiff: does anyone know to set depht to compare?
Hi, I'd like to use dirdiff with a degree of depht for compare 2 dirs. why? I have some heavy sub-dirs (with lots of pics, vids) and spend lot of time seeing into them! If I can set depht: bingo!
Thank you and regards!
https://redd.it/1goywp9
@r_bash
Config skill issue
How to remove line number in bat command
https://redd.it/1gnm0ka
@r_bash
GitHub - helpermethod/up: Climbing the directory tree at the speed of thought.
https://github.com/helpermethod/up
https://redd.it/1gn7r6e
@r_bash
Article about Bash Strict Mode
I write an article about Bash Strict Mode.
I would love to hear your feedback:
https://github.com/guettli/bash-strict-mode
https://redd.it/1gmufop
@r_bash
Looking for some guidance on how to tackle this problem with bash. Had a few ideas that I think can work but would like more experienced opinions.
Hello! For context to the title, I'm trying to make a bash script(s) that use the ncat util from nmap to capture data being streamed by a phone server, that part I've been able to accomplish no problem. I'm running into an issue when trying to take that data capture and output it to unique daily files, and having those files have only the output for that specific day.
The issues i've ran into are:
* if I move the file where ncat is being redirected to, it keeps appending to that same file even after renaming
* if I try to have the script running ncat change the file name to make new output file every day, no dice.
* using a cron job worked to an extent, in that scenario the ncat command was still putting everything in 1 big file and that was what was copied daily.
How would you approach something like this??
I've changed my scripts around a lot and the concepts are pretty simple so I haven't pasted my code here in the post, but if you'd like to see some of it by all means just ask :)
One thought I had was to make the script running ncat in a way where it can be called by a cron job, the only issue I can see there is making sure the ncat process stops after the day's over.
https://redd.it/1gmlz4b
@r_bash
ImageMagick6: ¿how change save 75 compr.(default) to 95 compr.?
Hi, this ask is about ImageMagic 6: Do you know how I change the compression for save by default is 75 and I'd like to set compression 95 (so change 75 for 95 by default).
Thank you and Regards!
https://redd.it/1gm5myp
@r_bash
Simple bash script help
Looking to create a very simple script to start a few services at once just for ease. My issue is it only wants to run one or the other. I'm assuming because they're both trying to run in the same shell? Right now I just have
cd ~/path/to/file &
./run.sh &
sudo npm run dev
As it sits, it just starts up the npm server. If I delete that line, it runs the initial bash script fine. How do I make it run the first script, then open a new shell and start the npm server?
https://redd.it/1gl3ox2
@r_bash
Temporarily change terminal 16 color palette in a script?
What's the specific term to call/describe the 16 colors that's always being used by the terminal? (neofetch colored squares, etc.)
And is there a way to dynamically change them through a script?
Searching for solutions, not sure if the command I need is tput
or dircolors
or something else.
Why do I want to do this? One utility I'm using will only use the set of 16 colors used by the terminal. I'm looking for a workaround so that I can force it to use colors I specify (from the 256 color set) without changing the defaults of my terminal.
https://redd.it/1gh3xf1
@r_bash
Bash grep
I've tried numerous rules, but I can't seem to figure out the correct pattern.
I have numerous words in a file
abcd:1234:ef35
f24a:5fa3
ab12:34fa:cd42
abcd:1234:ef35 f24a:5fa3 ab12:34fa:cd42
Help (Newbie)
if i gonna learning bash scripting, where to start and how?. i know understand bash scripting, but can'not make it myself
https://redd.it/1gge37v
@r_bash
M3U file list
I know I can create a file list with ls -1 > filename.txt, but I don't know how to prepend the directory path. I'm trying to create an m3u file list I can transfer to Musicolet on my phone. Can someone point me in the right direction?
https://redd.it/1gfk5jr
@r_bash
Confirming speed / bash practices
Long story short, I have a CSV file, 5 fields.
I read it into bash, and used IFS / while loop to read through the delimiters.
I'd say the CSV file in total is roughly 40MB. Pretty large for a CSV.
I went back and just decided to add a condition to check if the folder existed, because each row can be placed in different folders depending on the category.
When I added that check along with mkdir, I noticed that the speed dramatically went to hell. Without mkdir, the file completed in about 2 minutes. When I add mkdir -p, the speed to run the file went above 30 minutes, and then I just killed the script because it wasn't worth waiting anymore.
Is mkdir that heavy in a while loop? Or is their a better way to do this to ensure the sub-folders are created. Obviously I can't do what I was doing, because that just tanked the speed.
Just to give an idea, all I did was:
if test ! -d ${CAT}; then
mkdir -p ${CAT}
fi
test
or mkdir
, and if there's a much faster set of commands when working inside a loop, or should I try to do as much as possible outside the loop and do the bare minimum inside.tr
just to manipulate a string, and that also did it.Run command after certain time has elapsed
Hi, I'm trying to create a volume/brightness overlay that opens a window and closes it after a certain amount of time. The problem is that if I run my overlay script multiple times, the overlay window gets closed at random and flickers a bit. I'm currently doing the following:
Overlay() {
eww update icon="$icon" percent="$percent" && eww open overlay
sleep 2
eww close overlay
}
if $percent -gt 100 ; then
wpctl @ 5%+
Overlay
fi
This is a simplified version of my script. The full version can be viewed here.
https://redd.it/1gpg5ki
@r_bash
can I use mv (here only files) dir/
Hi, could I use any flag in command mv for only move files to destiny (a dir is destiny). Not recursive! just first level.
mv -¿...? dir/
= only files (with and without extension)
Thank you and Regards!
https://redd.it/1gp7um4
@r_bash
Can I set bash to automatically start in POSIX mode?
Hi, stupid question: Can I set bash to automatically start in POSIX mode? Could this be done using chsh or .bashrc?
https://redd.it/1goz8x8
@r_bash
I have written a utility to transcribe user-specified media files to subtitles using Bash
https://gitlab.com/brlin/media-to-whisper.cpp-subtitles
https://redd.it/1gnroro
@r_bash
Bash script to simplify finding Flatpaks via the command line
https://github.com/TDGalea/flatpak-finder
https://redd.it/1gnahqc
@r_bash
Filter video files by codec?
How to list and sort/move video files by codec?
Ideally just move all have/x265 files into a subfolder kinda thing.
I've just grabbed mediainfo and am prodding at but don't really know what I'm doing....I suspect with a little bash and some knowledge it should make this simple.
https://redd.it/1gmtgfb
@r_bash
What terminal app do you use on your Linux distro
Hello everyone,
I am a Mac user, and there is a long time I don't use a Linux distribution. My terminal app is iTerm2. What is the best replacement for iTerm on Linux distro, such as, Ubuntu/Fedora/Alpine ?
My requirements are 256xterm colors and font change ability.
Thanks.
https://redd.it/1gmmjcr
@r_bash
When a process is killed because it exhausted free memory, I'd prefer bash says "Killed: out of memory" instead of just "Killed"
I see in siglist.c the internationalized string:
sys_siglist[SIGKILL] = _("Killed");
But I'm wondering if we can use anything that the kernel does around https://github.com/torvalds/linux/blob/master/mm/oom_kill.c#L947 to tell the user that the reason was low memory?
https://redd.it/1gmfdxb
@r_bash
Learning more practical automation
Can anyone point me to where I can learn more real world scripting. More so applying updates to things or monitoring system health, so far all of the “courses” don’t really help more than understanding simple concepts.
https://redd.it/1gm1gfm
@r_bash
Trying to understand why my search returns no results
Hi all,
Let me preface this by saying this is day one of my scripting journey. I'll also add that I am using ChatGPT to try to cheat the hell out of it!
With that said, here is my problem. I am going step by step through the process of converting a PDF to text, parsing the text for info and then saving that info into a csv file.
I am on OSX so I started by using Shortcuts to "Get text from PDF" which i initially outputted to a text file; it worked fine. I then added a script (generated by ChatGPT) to search the clipboard (I changed the Shortcuts output to the clipboard) for the line "Grand Total" and output the line below (which had the amount) to a csv file. However the script can't find the line "Grand Total". Ive tried this initially with Applescript and now with a shell script, neither work.
Here is the code I'm using:
#!/bin/bash
echo "pbpaste version: $(pbpaste)"
# Get the clipboard contents
input_text=$(pbpaste)
# Convert the clipboard text into an array of lines
mapfile -t lines <<< "$input_text"
# Initialize variables
grand_total_found=false
grand_total_value=""
# Loop through each line
for ((i = 0; i < ${#lines[@]}; i++)); do
# Normalize the line by trimming spaces and converting to lowercase
current_line=$(echo "${lines[i]}" | sed 's/^[ \t]*//;s/[ \t]*$//' | tr '[:upper:]' '[:lower:]')
# Check if line contains "grand total" (case-insensitive)
if [[ "$current_line" == *"grand total"* ]]; then
# Get the next line for the total amount and trim whitespace
grand_total_value=$(echo "${lines[i+1]}" | sed 's/^[ \t]*//;s/[ \t]*$//')
grand_total_found=true
break
fi
done
# Check if "Grand Total" was found
if [ "$grand_total_found" = true ]; then
# Prompt for output file location
echo "Enter the path to save the CSV file (e.g., /path/to/output.csv):"
read -r output_file
# Write "Grand Total" and value to the CSV file
echo "Grand Total,$grand_total_value" > "$output_file"
echo "Grand Total saved to CSV successfully at $output_file"
else
echo "No 'Grand Total' found in the clipboard text."
fi
And here is the output from that currrently:
> pbpaste version: Remittance Statement
>
> 1.00
>
> To:
>
> REDACTED
>
> Date: 31/10/2024
>
> Ref: TR16246
>
> (Property) REDACTED
>
> Date Main Tenant Description VAT
>
> Charge
>
> (inc VAT)
>
> Payment
>
> (inc VAT)
>
> 29/11/2022 Not Applicable 392208 7,850.29
>
> 0.00
>
> 7,850.29
>
> 0.00
>
> Grand Total
>
> 7,850.29
>
> REDACTED
>
> E-mail: REDACTED
>
> VAT No: REDACTED
>
> Page 1 of 2
>
> Income and Expenditure
>
> Type VAT
>
> Charges
>
> (inc VAT)
>
> c000- Contractor Charge 7,850.29
>
> Payments
>
> (inc VAT)
>
> Totals
>
> 7,850.29
>
> Total Remitted 7,850.29
>
> REDACTED
>
> Page 2 of 2
>
> No 'Grand Total' found in the clipboard text.
I added the echo just to review the text it was taking from the clipboard was correct.
Any help at this basic stage much appreciated as this is going to get more complicated (I'll eventually need to output multiple lines). Also, what are the best places to look for documentation onn this sort of stuff?
Thanks all.
https://redd.it/1gghmnl
@r_bash
Who else has something like this in their .rc?
straight to prod \(\/s\)
https://redd.it/1gkahoz
@r_bash
Idea's needed
I was mildly annoyed at the one line update script for ubuntu. So I created a wonderfully bloated monstrosity of a bash script to accomplish the updates and some other stuff. I wish to bloat this thing further with more functionality. Any ideas would be appreciated.
https://github.com/ageorge224/Update-Script
https://redd.it/1gkkkee
@r_bash
File names with spaces as arguments
I want to merge a bunch of PDF s. The file names have spaces : a 1.pdf, b 2.pdf, a 3.pdf. And they're a lot of them.
I tried this script:
merge $@
And called it with merge.sh *.pdf
The script got each separated character as an argument : a 1.pdf b 2.pdf a 3.pdf.
I there a way to feed these file names without having to enclose each in quotes?
https://redd.it/1gg0eh2
@r_bash
cat match string / move to end of file
i've been over a few different websites reading up on this, but I feel like I'm missing something stupid.
I have a file, which contains a mix of ipv4 and ipv6 addresses. I'd like to use sed to match all ipv6 addresses in the file, cut them from their current position, and move them to the end of the file.
I've tried a few ways to do this, including using cat to read in the file, then using sed to do the action. It seems to be finding the right lines, but I read online that /d should be delete, and I'm trying to just get that to work before I even try to append to the end of the file.
cat iplist.txt | sed -n "/::/d"
cat iplist.txt | sed -n "/::/d" >> iplist.txt
shellm: A one-file Ollama CLI client written in bash
https://github.com/Biont/shellm
https://redd.it/1gdp44f
@r_bash