News for hackers. Stuff that matters.
Re:Show HN: A pure bash web server. No netcat, socat, etc. by pmarreck on Tuesday January 04, 2022 @10:58AM (#29796187)
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/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)

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... and just sucking up the extra run time (or compiling it into an executable with something like 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, or my fork of it https://github.com/pmarreck/es-shell/

Re:Show HN: A pure bash web server. No netcat, socat, etc. by dzove855 on Tuesday January 04, 2022 @11:01AM (#29796228)
Bash 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.
Re:Show HN: A pure bash web server. No netcat, socat, etc. by ramses0 on Tuesday January 04, 2022 @11:16AM (#29796443)
This is my favorite "reference" bash script: 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".

Re:Show HN: A pure bash web server. No netcat, socat, etc. by istjohn on Tuesday January 04, 2022 @12:05PM (#29797030)
5500 lines of bash. Wow.
Re:Show HN: A pure bash web server. No netcat, socat, etc. by pmarreck on Tuesday January 04, 2022 @11:13AM (#29796403)
I 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...
Re:Show HN: A pure bash web server. No netcat, socat, etc. by dzove855 on Tuesday January 04, 2022 @11:16AM (#29796450)
Well 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.
Re:Show HN: A pure bash web server. No netcat, socat, etc. by carlhjerpe on Tuesday January 04, 2022 @11:53AM (#29796892)
You 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".
Re:Show HN: A pure bash web server. No netcat, socat, etc. by foxhop on Tuesday January 04, 2022 @02:32PM (#29799011)
Here is a fun one I made:

https://github.com/russellballestrini/bash-kira/blob/master/...

Kira (bash-kira): a small bash script for killing programs which run too long.

Re:Show HN: A pure bash web server. No netcat, socat, etc. by wgjordan on Tuesday January 04, 2022 @01:44PM (#29798358)
> 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.

Re:Show HN: A pure bash web server. No netcat, socat, etc. by pmarreck on Tuesday January 04, 2022 @02:57PM (#29799430)
BATS 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 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...