by pmarreck · 1y ago · 0 comments
I think I've finally settled on Lua- It's small, coherent, surprisingly fast (especially with LuaJIT) and has the "virtually no startup cost" I was looking for (which ruled out, for example, Elixir for a lot of things).
And, I've finally figured out how to bang my nix-darwin flake.nix config in a way that gets it AND a bunch of popular libraries installed together and all seeing each other.
Wondering what others have found to address this. Or if they just stuck with Bash, since it's just not going away anytime soon.
The “Unix way” stands on the shoulders of stdin, stdout, pipe, and fork.
And the shells make that a first class concept. Breaking up the command line and linking processes together is the #1 job of a shell, everything else is gravy.
The scheme and lisp shell takes fail here, just because you have to go through the gymnastics of syntax.
My fantasy would be to have something like this:
(! ls | (myfunc) | sort -r -n | >list)
(“100 x.txt” “200 y.txt”)
Piping system commands into my own functions within the environment.Sure, I could do:
ls | myfunc.scm | sort -r -n
But then I can do that with anything, not just scheme (there’s that Unix superpower again).I would be something like this,
(->
ls
myfunc
sort -r -n
(> list))
Followed by "Select => Execute" with the mouse, or "Execute last form" with keyboard shorcut. $ txr
This is the TXR Lisp interactive listener of TXR 299.
Quit with :quit or Ctrl-D on an empty line. Ctrl-X ? for cheatsheet.
Poke a few holes in TXR with a fork before heating in the microwave.
1> (flow (glob "/proc/*") (take 10))
("/proc/1" "/proc/10" "/proc/101" "/proc/102" "/proc/103" "/proc/104"
"/proc/107" "/proc/1074" "/proc/1086" "/proc/11")
2> (flow (glob "/proc/*") (take 10) (map (op trim-left "/proc/")))
("1" "10" "101" "102" "103" "104" "107" "1074" "1086" "11")
3> (flow (glob "/proc/*") (take 10) (map (op trim-left "/proc/")) (sort @1 : toint))
("1" "10" "11" "101" "102" "103" "104" "107" "1074" "1086")
4> (flow (glob "/proc/*") (take 10) (map (op trim-left "/proc/")) (sort @1 greater toint))
("1086" "1074" "107" "104" "103" "102" "101" "11" "10" "1")
How about we parse digit sequences out of the path names and combine them with the originals, then sort on those digit sequences turned into integers: 5> (defun get-nums (str)
(flow str (tok #/\d+/) (map toint)))
get-nums
6> (flow (glob "/proc/*") (take 20) (csort @1 greater get-nums))
("/proc/11170" "/proc/11169" "/proc/1222" "/proc/1199" "/proc/1198"
"/proc/1194" "/proc/1133" "/proc/1104" "/proc/1086" "/proc/1074"
"/proc/117" "/proc/107" "/proc/104" "/proc/103" "/proc/102" "/proc/101"
"/proc/12" "/proc/11" "/proc/10" "/proc/1")
csort is caching sort: it caches the result of the key function through which the elements are projected to form the argument of the comparison.
In other words, so we don't wastefully call get-nums multiple times for the same path.One that strove to do both WOULD have to do all the things you suggested as well (job management, stdout/stderr redirection etc.) So options like oil shell, nushell, fish etc.
I was just talking about "a commandline tooling language" in general, as far as use-cases. And for that, Lua (and Moonscript, which compiles to Lua but is way nicer and has been featured on HN a few times over the years) seems to fit the bill (1-based indexing aside, ugh)
I was aware of it, but not familiar until I looked at the link you shared. Looks like a Scheme, which for the uninitiated is a different approach for shell scripts.
It's consistent, "infinitely" terse (you can create dsl-like apis for your problem domains), proper programming language.
First time I used it for scripting was when I had to process some xmls and do bunch of processing and gluing together – I was impressed how quick and easy it was without knowing anything about it and how readable the whole thing ended up being.
[0] https://www.gnu.org/software/guile/manual/html_node/Scriptin...
I'm one of those folks that still can't do LISPs though. If there was a way to replace some or all of the parens with significant indentation instead (which I believe Racket makes possible... but that only works in Racket), I might reconsider it.
With structural editing, no one ever have to match parens or count parens at the end...
YMMV ofcourse.
- Elvish https://elv.sh
- Murex https://murex.rocks
- Oil https://oils.pub
However the other shells are also highly polished. I’d definitely recommend each and all of them.
Perl was originally written as an amalgamation of grep,sed,tr, awk and I am sure a few more unix utilities with their own mini languages. The idea was to use one language instead of tying together half a dozen mini-languages in a shell script. And it worked really well. Perl being a demon with text munging didn't hurt.
Raku keeps this heritage but adds so much more (for better or worse :-) ). It inherits ideas from Lisp and functional programming languages. The thing that impressed me was, how easy it was to use concurrency.
It has similar aims to https://www.nushell.sh/
I've actually already written a lot of the functionality I see here as bash (and other) functions, lol (things like log, filter, debug, retry, repeat, map, fetch (a URL, with retry)... etc.). (things like: enumeration functions, table pretty-printing, etc.)
> I've actually already written a lot of the functionality I see here as bash (and other) functions
That's exactly the reason these are part of stdlib. No reason for everybody to reimplemnt these with slight variations and copy it around.
If you're familiar with Python, give xonsh (https://xon.sh/) a go. It's a Bash-like shell but the syntax is Python. It has the same ease of writing shell scripts as Bash does, sans the insane language.
Been using it since 2018.
(I'm not particularly inclined to Python, unfortunately, due to the dependency issues and some personally negative opinions about its design... but I like the idea of Xonsh if you're a Python person! I wish I had something like that for Elixir!)
Or just text-processing things, where it's most suitable?
(I think Awk is underrated, for sure!)
See, FileUtils for things like rm_rf, rake provides sh which can execute commands, then there's FileList and Pathname. It even comes with its own template engine (erb) if you need that. Regexes as first class citizens are awesome for shell scripting.
I can also recommend using optprase + ostruct for commandline argument parsing.
Like ruby (I guess), python is very pleasant to work with for small scripts and it's portable between linux and windows, which is really cool (at least in my use cases)
In python, glob, Path and subprocess are mostly all I need...
Executing a process and bringing back status, stdout and stderr seem tedious. While in Perl, it is as natural as it is in a bash script (well almost).
Raku on the other hand is _Fun_.
i do all my automation with one tiny 20 line raku script that manages a thousand things. hell, the entire monitoring site for our frog is basically just a pile of mixins with a neat little pipeline interface unifying everything from request profiling and html templating to reading from and filling that cache in parallel whenever it remotely pulls the sensor data off various things and gets the current webcam frame.
You have to reinvent the wheel every other day since the standard library doesn't come with much included
(The intro page may be a bit misleading. You can freely mix-and-match existing, unstructured as well as nushell-built-in structured commands in the pipeline, as long as you convert to/from string streams - its not mandatory to use the structured built-ins. For example if an existing cli tool has json output, you can use `tool | from json` to turn it into structured data. There are also commands like `detect columns` that parses classic column output, and so on - the tools to mix-and-match structured and unstructured data are convenient and expressive)
Some highlights:
- automatic command line arguments and help by defining a main function and adding comments to each argument - e.g. https://github.com/nushell/nushell/discussions/11969
- run commands with controlled parallelism: https://www.nushell.sh/commands/docs/par-each.html
- easy parsing of raw input https://www.nushell.sh/commands/docs/parse.html
- support for a wide variety of data formats https://www.nushell.sh/commands/categories/formats.html
- built-in support for talking to SQLite databases: https://www.nushell.sh/book/loading_data.html#sqlite
edit: it looks like Mitchell Hashimoto was recently impressed too https://x.com/mitchellh/status/1907849319052386577 - rich functional programming library that blends with pipeline syntax https://www.nushell.sh/book/nushell_map_functional.html
Addendum: Its not my login shell. I run it ad-hoc as soon as the command pipeline i'm writing starts getting too complicated, or to write scripts (which of course can be run from outside nushell too, so long as they have the correct shebang)
As a minor example, I hate having to remember (or explain) ${var##pattern}
It is useful for manipulating paths, but it is cryptic.
in comparison, something like:
newfile = os.path.join(newpath, os.path.basename(oldpath))
IMHO is more self-explanatory and robust.Also, argparse is the best.