#Shell Script
Explore tagged Tumblr posts
Text
I guess you were born with geekiness in your blood 😂
13 notes
·
View notes
Text
Clarity trumps efficiency.
*I would've liked to write this essay to be understandable for someone without a programming/Linux background, but it was a bit too difficult. If you skip to the paragraph beginning with "...", it gets a bit easier from then on.
If you’ve ever written your own shell scripts you may have heard of the phrase “useless use of cat*”, or less tactfully, “cat abuse”. This refers to the practice, common among new shell script enthusiasts, of writing commands like “cat file.txt | grep name”, when “grep name file.txt” would serve perfectly well. Tools like shellcheck will bug you about it—along with similar constructions like “ps ax | grep Discord | wc -l” instead of “pgrep -c Discord”.
Well, I’m here to defend cat abuse! There are two arguments I see against the cat | grep construction, one of which is valid but situational, and the other of which is completely invalid. The former is that the extra pipe just adds additional overhead into the command. Yes, it does. And it’s unlikely to matter at all if you’re using it on 20KiB text files on a system built in the past 40 years; however, in production, when writing tools that need to be able to deal with arbitrarily large text files as efficiently as possible, sure.
The latter is “well, it’s just unnecessary”. I disagree. I think the cat | grep construction—along with similar such as grep | wc, ps | grep, ps | awk, and so on—serves a very important purpose in that it makes shell scripts easier to read, easier to modify, and easier to debug.
Consider this example from above:
ps ax | grep Discord | wc -l
Read the process table; filter for "Discord"; count the number of lines. It’s very atomic. Each operation can be swapped out for something else without confusing the reader. On the other hand:
pgrep -c Discord
Now, this does the same thing—counting the number of lines in the process table with "Discord" in them. It looks like only one operation... but it’s really still three in disguise. And worse, imagine you suddenly want to add another filter; sorting not only by Discord, but by processes that include the word “title”. This is not straightforward at all! It turns out that while regex has a standard way of searching for alternatives, it really does not provide an easy method for searching for BOTH of two words. On the other hand, with the atomic version, it’s easy:
ps ax | grep Discord | grep title | wc -l
Take that, “useless” use of cat.
There’s a broader meaning, though, to my statement of “clarity trumps efficiency”. I apply it to every aspect of use of electronics, from web searches to backup routines to yes, silly little shell scripts that use cat.
I use command aliases, but to a pretty limited degree; I avoid cutesy stuff like “ll” for “ls -l” and “yeet” for “pacman -Rns”, along with possibly-dangerous substitutions like “rm” for “rm -i”; I’d never dream of aliasing “nano” or “vi” to my preferred text editor (vim). I believe strongly that my commands should be transparent, and saving me from my own muscle memory once or twice is not worth making them completely opaque.
Tab completion on the other hand is one of my favorite features in the shell. It’s the perfect combination of transparent and convenient; without having to alias any of my application names or get hit by the information overload fuzzy finding gives you, I can still launch any of them in no more than four keystrokes. (Except audacious and audacity, admittedly.)
I use a floating window manager (Openbox), and when I need to briefly use a tiling layout, I have a very boring way of doing so: focusing each window one by one and moving it into the slot I want. (While holding down the Super/Windows key, 1-C-2-V does a basic left-right split.)
... I make some use of spellcheck on assignments to be turned in, but never autocorrect, which I abhor even in messaging apps. Every change to your inputs should be deliberate; otherwise you’ll never learn what you’re doing wrong, and you’ll never need to be precise because you’ve turned over that part of your brain to the algorithm.
This leads me to an important corollary of my principle: “it’s better to have a slow algorithm that you understand, than a fast one that you don’t”.
Satya Nadella’s vision of the PC of the future is one where you tell it what to do in natural language and it interprets that using LLMs and so on into machine instructions. Instead of viewing a PC as a toolbox you go into the workshop with, and work on projects with in certain defined ways, he wants the PC to be an assistant; you give the assistant directions and pray that it gets things right. Of course you aren’t allowed into the workshop with the tools anymore; that’s the assistant’s job!
Anyone who’s used Google Search over the past ten years knows how miserable this model is; you search for a specific phrase that Google “helpfully” corrects to something it thinks you meant. There was a learning curve to the old way, but once you learned how to state queries precisely, you were done; now you need to play psychologist, sociologist, and statistician all at once.
This is a decent part of why I dislike generative AI, though far from the main reason. I don’t want an opaque algorithm making decisions for me, unless those decisions are incredibly low-level stuff like core parking that no human should be directly involved with in the first place.
To get back to my own setup, I have a whole text file documenting the system maintenance process I go through once every month; most of it could be automated, but I make every step a deliberate choice. Not to go all new-age, but for me specifically—it all ties back in to mindfulness.
I think people have only a vague concept of what mindfulness is. Until two years ago or so, I was the same way. But to who I am now, mindfulness means not doing anything on autopilot. Instead of letting yourself half-doze off on a drive home, scarcely remembering the 20 minutes from the parking lot to the garage, be conscious of every turn. Instead of immediately putting on music and blocking out the world on a train ride to the next city, force yourself to be present in the train car, and notice the way the light reflects on the plastic seat two rows in front.
And to me, clarity in code, and in UX, is a part of this mindfulness. Programs that are easy to read, easy to modify, and easy to debug encourage you to look closer—to consider every atom that goes into their statements instead of taking them for granted. Slow algorithms that you understand can help you think of improvements; fast algorithms that you don’t encourage you to give up and leave the real thinking to someone else.
So write silly little shell scripts with five pipes in a single statement, and yes, that uselessly use cat. Rather than doing anything wrong—you’re allowing yourself and others to think, to try, and to improve.
#programming#linux#mindfulness#i would have gotten deeper into spirituality in this essay but i think it would've scared anyone off#might post on another site#shell script
12 notes
·
View notes
Text
updating my "resvim" script to support changing working directory and i've come across an interesting design choice to make:
the script checks for "-d" flag as $1 and passes $2 to vim remotely
vim --servername MAIN --remote-send ":cd $2<CR>"
however, because the remaining file arguments are relative to the shell's working directory, invoking it from idk ~/Documents as
resvim -d ~/Projects/Foo bar.c baz.c
results in vim changing to ~/Projects/Foo but trying to open ~/Documents/bar.c and ~/Documents/baz.c
so, my options are:
leave as-is (in case you wanna open files from your shell's PWD but change vim's PWD; also makes dealing w/ relative and absolute paths easier)
force file arguments after a '-d' flag to be relative to that directory ONLY
add a new flag that specifies files are relative to the path provided by '-d'; otherwise, leave as-is (potentially simplifies things re: option 1)
oh, i should mention this is fully POSIX-compliant shell despite it being unlikely i'll need it to be portable (plus it depends on dtach and currently i3 soooo) and just as unlikely anyone else would use it d:
#daemon.md#shell script#POSIX shell#linux#progblr#design decisions are the most interesting part tbh#closely followed by dumb simple solutions to complex problems#and then by code golfing xD
3 notes
·
View notes
Text
stop using chatgpt!!!! take a bronze pin and carve your questions onto an ox scapula, then toss it into the fire!!!! use the cracks to divine the gods answer!!!!
70K notes
·
View notes
Text
Permessi dei file
Su ciascun file, ogni utente può effettuare diversi tipi di operazioni: leggere, scrivere, eseguire. Con il comando "ls -l" otteniamo un risultato del tipo
-rwxr-xr-x 1 Giacomo staff 170 Dec 16 14:14 operations.sh
il primo carattere può essere - (file regolare), d (directory), l (link simbolico). I successivi 9 caratteri si leggono a gruppi da tre. I primi 3 indicano i permessi del proprietario del file. I secondi 3 sono i permessi del gruppo. L'ultima terna sono i permessi degli altri utenti.
I permessi sono disposti con l'ordine rwx (read, write, execute). Se il permesso è disponibile, troviamo la lettera corrispondente, altrimenti troviamo -. Nel nostro esempio: il file è un file regolare; Giacomo possiede i permessi di lettura, scrittura, esecuzione; il gruppo staff può leggere ed eseguire, ma non scrivere; gli altri utenti possono solo leggere ed eseguire. 170 è la dimensione in byte. Dec 16 14:14 è la data di ultima modifica. Alla fine troviamo il nome del file.
Come cambiare permessi
chmod [opzioni] [permessi] nomefile
tra le opzioni possiamo scegliere u (user), g (group), o (others). Poi possiamo specificare quale aggiungere o togliere con ad esempio +r, -r, oppure possiamo specificare l'intera terna, ad esempio chmod g=rw
0 notes
Text
It's weird using natural language in a AI Linux terminal when you already know advanced shell script. It feels like you are learning a whole new language. That's much easier using the command 'ls /home' than asking AI on natural language to list files on my Home directory. Of course very complex shell script tasks will be much easier asking AI on natural language... maybe... for newbies...
0 notes
Text
Open the most recent file for a given folder
So at work we use this gem called letter_opener to handle emails in the local rails environment. When an email is sent in your rails environment, then letter opener stores it in tmp/letter_opener and automatically opens the email in your browser. It's very useful when resetting your password on local, or testing new mailers etc.
Brilliant, you say! Yes, it is. However, since we migrated to Docker in our local environment, the automagical opening in your browser feature no longer works. The email html still generates in the tmp/letter_opener folder but it's a pain in the neck to dig around in there every time you need to check an email. So I set about finding an alternative way to open the emails. I settled on a simple shell script that would find the most recent html file in the tmp/letter_opener folder and open it using the open command:
open $(find /path/to/search -type f -name "rich.html" -exec ls -lt -- {} + | head -n 1)
I have this assigned to a zsh alias called email. Every time I need to check an email I can trigger it and see the last email my rails app sent in my browser.
P.S. I've also been thinking about exploring using a mac folder action to achieve a similar feature to the original functionality. This would trigger whenever a new file appeared in the tmp/letter_opener directory and open it in the browser.
1 note
·
View note
Text
Macbook Battery Stats in Your ZSH Terminal Prompt
As a power user of my Macbook, I’ve found that I often overlook the small battery icon on my menu bar, especially when I’m immersed in a fun project. This minor inconvenience sparked a thought: why not incorporate the battery status directly into my terminal prompt? Thus, I embarked on a fun exploration into ZSH scripting. With a bit of coding magic, I was able to enhance my terminal prompt to…

View On WordPress
#Automation#Battery Monitor#Coding#Command Line#Customization#MacBook#MacOS#Power Management#Productivity#Scripting#Shell Script#Tech Hacks#Terminal#User Experience#zsh
0 notes
Text
you shouldn't need to cat the link file. I think… just for i in media-links.txt; ? Not sure of syntax, on my phone in bed RN
For the URL encode issue, try mv "$file" "${count}-${file}"
Always quote your vars, and use the curly braces too if needed
also seems like file=echo $i is useless? It simplifes down to file=$i as far as I can tell (while adding a chance of unquoted spaces and such)? Just use $i later on in the mv, should be the same.
I hope this may help, on the other hand like I said this all out of my brain no testing or even lookup cause on my phone and its a pain, so maybe it is useless and wrong LOL
Also check out the linter "shellcheck", it is fucking rad.
So, wrote a dumb bash thing
$> count=$(wc -l media-links.txt) $> for i in $(cat media-links.txt); do wget $i; file=echo $i | awk -F '/' '{print $NF}'; mv $file $count-$file; count=$((count - 1)); sleep 2 ; echo ================================; done
It mostly works, although that sleep apparently needs to be longer because I was getting of "ERROR 429: Too Many Requests".
The rename doesn't always work because sometimes the URL has a URL encoded character, which gets decoded by wget when the actual file gets saved. e.g.
--2024-07-01 00:11:17-- https://static1.squarespace.com/static/55dca83de4b018ac1524526f/t/622990a6d88dfa624f168cbd/1646891207920/Samuel+Spruce%2C+The+Little+Wooden+Boy+in_+The+Frosty+Wood.mp3 Resolving static1.squarespace.com… 146.75.32.238 Connecting to static1.squarespace.com|146.75.32.238|:443… connected. HTTP request sent, awaiting response… 200 OK Length: 33151680 (32M) [audio/mpeg] Saving to: ‘Samuel+Spruce,+The+Little+Wooden+Boy+in_+The+Frosty+Wood.mp3’ Samuel+Spruce,+The+Little+Wooden+Boy+in_+The+Frosty 100%[=================================================================================================================>] 31.62M 20.2MB/s in 1.6s 2024-07-01 00:11:19 (20.2 MB/s) - ‘Samuel+Spruce,+The+Little+Wooden+Boy+in_+The+Frosty+Wood.mp3’ saved [33151680/33151680] mv: can't rename 'Samuel+Spruce%2C+The+Little+Wooden+Boy+in_+The+Frosty+Wood.mp3': No such file or directory
Anyway, that's a problem for future me.
14 notes
·
View notes
Text

this was meant to be a silly concept sketch that got mildly overproduced for the sake of putting some concepts on paper
it's still fun to make so i'll take it though
[ Description in ALT ]
#original#artists on tumblr#oc#original character#ai oc#robot oc#aster#rigel (aster)#vega (aster)#CaelOS#puts frutiger aero elements into the art. and then doesn't tag it as such because MGHHH IT'S NOT TECHNICALLY ENOUGH IS IT#i was gonna make a window but im running out of steam. and need to sleep soon lol#that yellow bird thing is the alarm clock. i don't know what the green process is#probably a generic script with baby's first graphic shell made by rigel
69 notes
·
View notes
Text
Effettuare più operazioni sui file contenuti in una cartella.
Avevo una cartella contenente tanti file audio e numerati in modo progressivo (01-AudioTrack 1.mp3, etc...). Visto che la numerazione non corrispondeva al numero della lezione, volevo aggiungere in ciascun nome "- Lezione X", con X numero della lezione. Con questo script vediamo come fare. Operazioni simili possono essere eseguite con lo stesso metodo.
Per prima cosa posizioniamoci nella cartella e creiamo uno Shell script come descritto nel post precedente. Poi inserire il seguente file testo nello script.
#!/bin/bash i=1 for file in *.mp3; do if [ -f "$file" ]; then base_name="${file%.mp3}" new_name="${base_name} - Lezione $i.mp3" mv "$file" "$new_name" ((i++)) fi done
Vediamo le varie linee e il loro significato.
La prima è dice alla shell quale interprete deve eseguire il file. In questo caso /bin/bash
i=1 inizializza la variabile i. I file audio verranno numerati a partire da 1, se vogliamo partire da un altro numero, basta cambiare questo valore.
for file in *.mp3; do è poi un ciclo for che itera su tutti i file nella cartella corrente con estensione .mp3.file è la variabile temporanea che assume il nome di ogni file .mp3 uno alla volta.
if [ -f "$file" ]; then controlla se l'elemento rappresentato da $file è effettivamente un file (e non una directory). -f è l'opzione per "file regular"
base_name="${file%.mp3} estrae il nome del file senza l'estensione .mp3. ${file%.mp3}: % indica la rimozione di una parte finale di una stringa, in particolare %.mp3 rimuove l'estensione .mp3 dal nome.
new_name="${base_name} - Lezione $i.mp3" crea una nuova variabile new_name che rappresenta il nuovo nome del file.
mv "$file" "$new_name" rinomina il file.
((i++)) incrementa la variabile i.
fi chiude il blocco if e done conclude le operazioni.
0 notes
Text
Wouldn't it make the man page have line numbers on the side?
Not necessarily
So I'd have to add another if case onto my bashrc and those are slow
Learn to use && and ||. && is non-branching then, || is non-branching else. No shell should take a full second to start.
But yes Debian calling it batcat is kinda dumb.
i just spent like an hour to solve linux problem again.. because my man page colors suddenly stopped working! and you can't have uncolorful man pages. 0_0
thankfully i had a second machine (my server) to compare outputs to..
i eventually tracked down the culprit, and it was groff being updated to version 1.23.0! seems totally unrelated to man, doesn't it¿ :)
now i have to figure out if i can solve this better than just downgrading it to 1.22.4
PS: i spent way too much time barking up the less tree, because the config variables are called LESS_TERMCAP_*
edit: you have to set the GROFF_NO_SGR environment variable to 1
50 notes
·
View notes
Text
saw the clip of landoscar touching hands and IMMEDIATELY hopped onto tumblr.com. i know the girls are losing their minds over this. thank you hilton 🙏🏼
#landoscar#i can’t figure out if this is scripted or not#bc some moments feel so THEM especially oscar’s natural reactions to lando#but also the end was DEFINITELY scripted#are they just better actors than i expected??#maybe c2 were just so bad in the shell ads that it lowered my expectations for everyone else#also#completely random but how do you think lando would’ve fared as a drama/theatre kid?#i can kinda see it in him#i feel like normal high schooler non-racing driver lando would’ve signed up for laughs and ended up becoming unexpectedly invested
91 notes
·
View notes
Text
I am watching all the Link Click endorsed CMs, Part 1 : Link Click X 王老吉 Wang Lao Ji (herbal tea drink)
#Link Click#shi guang dai li ren#时光代理人#Yea I am watching all the Link Click commercials#I think you will find this drink very familiar Yuigadokson!#Studio Lan be straight-up animating full-length CMs like it was left over footage they had sitting around#This is something I don't really see happen with *anime* product endorsed CMs#^ most times they just borrow readily available footage direct from the anime or manga to use#These sponsors really did shell out monies for the full nine yards#Imagine the production cost of an animated short from scratch complete with a full script and voice over!#It dang well could be an omake on its own merit
65 notes
·
View notes
Text
A Bash Script to Read All Command Line Arguments into an Array Using mapfile Command: Simplify Argument Handling
12 notes
·
View notes
Text
Listen carefully. LOVE Rise. Absolutely fantastic rendition of the turtles. But if there's one thing '03 did better, it's the amount of times they use the word shell as a swear. My GOSH if that's not the funniest, punniest, stupidest shit I have ever seen. I laugh every damn TIME.
#I literally have started saying what the shell#I cry laughing every time#where the SHELL are we??#tmnt#tmnt 2003#rise tmnt#rottmnt#the script writers were COOKING with this oh my gosh#Btw if it wasn't obvious#this is meant to be lighthearted#but I'm also so deadass#I need more what the shell shit ty
38 notes
·
View notes