3 ms·
The other day I wrote this bash code after getting frustrated with the password generators available to me, just so I had something to use right in the shell:
by pmarreck 5y ago
The other day I wrote this bash code after getting frustrated with the password generators available to me, just so I had something to use right in the shell:
https://gist.github.com/pmarreck/f4dbb02396c4532762a7a64511cd8e52 https://gist.github.com/pmarreck/f4dbb02396c4532762a7a64511c...
It actually took an entire afternoon and went through a couple iterations, and I hadn't even written a test for it yet (and it just so happens that I started an MVP shell-script testing library for just such an occasion when I ALSO got frustrated with the bash-accessible testing libraries available to me, also in Bash: https://github.com/pmarreck/tinytestlib https://github.com/pmarreck/tinytestlib)
But then I immediately felt bad. I could have written all of this in less than an hour in a language like Elixir, with test coverage (and the testing/assertion lib is built-in, so it would have been free).
It is impressive and code-golfish that someone can "write a webserver in pure Bash" but Bash is a crap language to script in at this point, and adding more Bash code to the world just seems wrong now. Bourne shells were invented in the 70's (!), long before many language advancements and understandings, and they thus have MANY warts (many of which we're familiar with).
The next time I feel the urge to code anything more than a one-liner in Bash, I'm writing it in elixir script https://thinkingelixir.com/2019-04-running-an-elixir-file-as-a-script/ https://thinkingelixir.com/2019-04-running-an-elixir-file-as... and just sucking up the extra run time (or compiling it into an executable with something like https://github.com/spawnfest/bakeware/ https://github.com/spawnfest/bakeware/ and sucking up the extra megabytes), and then MAYBE writing a wrapper function for my .profile
Incidentally, if you DO want a shell that "tastes like" Bourne but is functional and thus sucks a lot less, check out https://github.com/wryun/es-shell https://github.com/wryun/es-shell, or my fork of it https://github.com/pmarreck/es-shell/ https://github.com/pmarreck/es-shell/
- dzove855 5y agoBash is not mean for kind of projects like this. I even develop on other languages. But i like bash. A good bash script, is like reading a novel, because it's challenging to have a clean script.
- pmarreck 5y agoI get it. The challenging part is fun. You can learn all the corner cases, the clever bash-isms, and then show off. But the code you end up with is essentially unmaintainable, especially since you don't have any test coverage, so...
- dzove855 5y agoWell that's true. I took me two hours to get this done. Since accept was not clear at the beginning. But it will be cleaner in the future, i'm still not convinced how it works currently.
- lillecarl 5y agoYou could be a doctor! "I don't really know why or how this works, but I'll prescribe enough for you to last a month".
- ramses0 5y agoThis is my favorite "reference" bash script: https://git.einval.com/cgi-bin/gitweb.cgi?p=abcde.git;a=blob;f=abcde;hb=HEAD https://git.einval.com/cgi-bin/gitweb.cgi?p=abcde.git;a=blob... ...it feels like "if it's possible to do in bash, this does it".
- istjohn 5y ago5500 lines of bash. Wow.
- wgjordan 5y ago> and it just so happens that I started an MVP shell-script testing library for just such an occasion when I ALSO got frustrated with the bash-accessible testing libraries available to me I found Bats straightforward and simple to use, never felt the need to reinvent the wheel there.
- pmarreck 5y agoBATS is 1) bloated 2) doesn't use a very nice syntax (hint: bashisms are not a very nice syntax). Compare BATS assertions with this for example (the test of my test library, which uses the library's own functions): https://github.com/pmarreck/tinytestlib/blob/yolo/test https://github.com/pmarreck/tinytestlib/blob/yolo/test Call me crazy but I think `assert_equal "4" "$result"` is a lot more readable than `[ "$result" -eq 4 ]` 3) doesn't let you assert on all of (return code, stdout, stderr) from running a single command. I created a `capture` function to do that and bring those all into named variables that you can then assert on. But overall I wanted something tiny that you could easily include alongside a bash script instead of establishing as a new dependency you'd have to install separately. Honestly, I feel that functions like assert_equal, assert_not_equal, assert_success, assert_failure, assert_match and assert_no_match (which all raise on failure) should be built into every programming language's standard library. I know that you can sort of get by just using `set -e` and just do a comparison operation, but...
- foxhop 5y agoHere is a fun one I made: https://github.com/russellballestrini/bash-kira/blob/master/kira.sh https://github.com/russellballestrini/bash-kira/blob/master/... Kira (bash-kira): a small bash script for killing programs which run too long.