Telling a Computer to Do Things
Posted by vismit2000 1 day ago
Comments
Comment by adityaathalye 1 day ago
1. Universal Inter-Process Interface: Arbitrary program composition via the Standard Input / Output / Error interface, and "everything is a file" abstraction... None of the rest is possible without some such core design decision ("everything is an object" is the fraternal twin).
2. Widespread standard utilities: The broad availability of core util tools designed to cooperate via the same abstraction is a close second. Along with some default scripting language, that is also built for the same computing model.
3. Language of choice: Important for ease of access, but a distant third in terms of raw power, be it bash, perl, awk, node, clojure (babashka) etc. That's icing on the cake, because if one had no choice in the matter, the shell program would still be awesomely powerful. Proof is in the pudding---"scripts that matter" are always /bin/sh.
Generally speaking, the pain of learning to avoid sh and bash footguns is primarily a matter of some basic research reading: manuals, lore, and warnings (I love BashPitfalls). Well, either that, or learning the hard way by copy-pasting code, and if luck runs out, rm -rf ing something you shouldn't have.
There is no substitute to taking the day or so to read the manual, so you know it's a lot, but it ain't magic, and you can eat that elephant one bite at a time, at your own pace. Seriously... the wall-clock time of trial and error with BashPitfalls (about a day or so), is nothing compared to learning to use---and then having to re-learn every so often---the python or node or whatever ecosystem without losing it. Besides, the former knowledge is stable and so are the programs --- my Bash code will keep working the same way, forever, practically speaking.
Comment by happyPersonR 23 hours ago
Learning and getting good at shell scripting is something that takes a while usually. I would recommend that people new at shell scripting actually try to use Python instead writing shell scripts where possible.
I’ve seen people not use traps, not know when to use set properly, xargs if you don’t know what you’re doing with it is just a giant foot cannon, and folks new at shell scripting don’t know how to debug running shell processes.
They may have learned some or all of these things for python?(please? )
Comment by johnisgood 23 hours ago
My 2 cents. :)
Comment by marcosdumay 22 hours ago
On other languages those things are either obvious because you have to write the behavior yourself, or fail loudly in a way that requires you to learn them before your code will run.
You don't have to hope. The same people that fall to the foot-guns in sh don't make the same mistakes when they write it in python.
Comment by necovek 1 day ago
I'd even drop the "if" from their footnote version, and go with simply
npm install || (echo "boom" && exit)
(Not exactly equivalent because there won't be an "exit" if echo fails, but it's my preferred instict over ";") Also, "(" starts a subshell, if we are being pedantic.Comment by Sharlin 1 day ago
Comment by necovek 1 day ago
test 1 == 2 && echo "ok" || (error_code="$?"; echo "failed with '$error_code'"; exit "$error_code")
When command (test 1 == 2) fails, it goes to the "or" section, sets the error_code variable, does something (echo command, but it could be anything), and then re-exits with the same exit code.It's not so obscure, there is just no native "re-raise" function so you have to record the exit code yourself (actually, bash's built-in exit will return the last command's exit code, but in the example above, that's the "echo" which actually succeeds).
Edit: since the above runs it in a subshell, $error_code is not available outside the parentheses but if you need this, you can try:
test 1 == 2 && echo "ok" || error_code="$?" && echo "failed with '$error_code'"
exit $error_codeComment by dasyatidprime 1 day ago
Works in dash/bash/zsh at least; not 100% sure whether the behavior of $? on the command in an if test is mandated by POSIX and/or portable. Note that you can't replace the empty then-branch with a negated condition because the negation also eats the return code.
Also, absolutely correct propagation is essentially impossible (but usually not useful anyway), because some errors might be unexpected signal exits or inability to find the command in the first place, and those will get punned onto other nonzero exit codes. I forget the exact conventions OTTOMH but generally they're in the high half (≥128).
Comment by necovek 23 hours ago
Where texinfo is available, "info bash" is a great reference; "man bash" is everything in a single page — so less manageable — but you can look for BUILTIN section, which documents "exit" built-in function (among other things).
Comment by necovek 1 day ago
I believe the answer is the same as to why Python remains hugely popular despite all the well-documented shortcomings: batteries included.
Shell languages include so many neat and concise ways to execute commands, deal with their input, output and exit codes, allow simple traversal of deep directory structures with globs and advanced tools like 'find' or 'xargs', that everything else is very unergonomic.
And niche by default (until it gets used widely enough), so even your LANG-OF-CHOICE-expert you recruit, they will not know a thing about that shell-focused library in LANG-OF-CHOICE.
Comment by yjftsjthsd-h 1 day ago
Comment by utopiah 1 day ago
Scripting in the terminal can manipulate windows too (e.g. xdg-open with KWin), or send notification locally (e.g. notify-send) to any devices (again or ntfy), or open dialogs (e.g. kdialog), or controlling other devices (e.g. curl to HomeAssistant API) so indeed it opens up an entire new world of possibilities... and it's combinatorial.
Also for another bit of context, scripting works ... on the weirdest newest devices, e.g. a Meta Quest on Android thanks to Termux. If there is an OS there is surely a terminal in there somewhere, and if there is, you can script on it. So... scripting isn't "just" for your desktop or a remote server, it's everywhere.
Comment by KetoManx64 17 hours ago
Comment by carra 1 day ago
Comment by ozgung 1 day ago
Comment by TheOtherHobbes 1 day ago
It's slow and very inefficient, but it works.
Why do this? It would be insane for production, but for development it frees you up to improvise and experiment. You can iterate strategies and solutions far more quickly, test as you go, and bake a final production version into static code.
Comment by ccapitalK 1 day ago
Comment by rw_panic0_0 1 day ago
Comment by Sharlin 1 day ago
Comment by sourdecor 1 day ago
Comment by disgruntledphd2 1 day ago
Ruby is much, much prettier for similar purposes though, and so that's what I'll learn if I ever need to do this more frequently.
Comment by ZeroDayDreamer 1 day ago
Comment by kalx 1 day ago
Comment by ryuuseijin 1 day ago
The great thing about a shell is that you can glue together programs that don't know about each other. This is not a silver bullet, with shell programs being text oriented etc., but it does give rise to some powerful possibilities that don't really work with UIs.
Comment by sire-vc 1 day ago
Criticising people just because they don't use your choice of tools is just elitism and leaves a bad taste.
If someone uses a GUI to accomplish their goals that should be complimented not criticised.
Comment by TheOtherHobbes 23 hours ago
As soon as commands become even a little non-trivial, those costs mount up.
Comment by BeetleB 22 hours ago
Thank God for LLMs.
Comment by mplanchard 22 hours ago
Comment by JimA 1 day ago
Comment by blairfrandeen 1 day ago
Comment by jeremyjh 1 day ago
If you take nothing else away from this, memorize the syntax of one-liner for loops, it has saved me so many times.
Comment by weikju 1 day ago
Comment by fyredge 1 day ago
If scripting were to be excluded, the belittling of GUIs falls a bit flat. I'd like to see graphic design work be done via CLI.
Comment by applfanboysbgon 1 day ago
Comment by altmanaltman 1 day ago
For example
> At a fundamental level, I wasn’t able to tell my computer to accomplish anything that involved logic or stitching together multiple programs
How can that be true if you are telling it to boot on into a os? It doesn't involve logic? Or stiching together multiple programs?
Using a shell =\= telling a machine what to do on a broad level. You are just using the abstractions provided by the os. If you mean get better at shell so you can use it more efficiently than okay cool but that doesn't someone who uses a GUI doesn't "tell" the machine what to do as much as you.
Comment by aldricarchive 1 day ago