News for hackers. Stuff that matters.
Re:Show HN: A pure bash web server. No netcat, socat, etc. by spicybright on Tuesday January 04, 2022 @09:53AM (#29795361)
Definitely scary, but I love things like this. Takes a lot of creativity!

There was a post a bit ago that had a one liner for a basic web server using net cat:

  while : ; do cat page.html | nc -l 80; done
Re:Show HN: A pure bash web server. No netcat, socat, etc. by hericium on Tuesday January 04, 2022 @10:21AM (#29795673)
or even

    while :; do nc -l 80 < page.html; done
Re:Show HN: A pure bash web server. No netcat, socat, etc. by skrtskrt on Tuesday January 04, 2022 @12:57PM (#29797696)
what is the difference in these two approaches?

I always use the pipe, it is intuitive to me to think left-to-right, output of this becomes input of next. But I always see stackoverflow answers using the other approach.

Re:Show HN: A pure bash web server. No netcat, socat, etc. by arbitrage on Tuesday January 04, 2022 @01:36PM (#29798255)
The difference is in the first code example, you're launching both cat and nc.

In the second block, you're just launching one program that reads input, not two. It's slightly more efficient.

I also tend to build large *NIX pipelines using `cat $foo | [...]`, but it's some times regarded as bad form. See: "useless use of cat" for more examples.

Re:Show HN: A pure bash web server. No netcat, socat, etc. by skrtskrt on Wednesday January 05, 2022 @01:37PM (#29813060)
Aren't you just using another built-in to read the file contents then?

Maybe it's lighter than cat, but whatever code path is triggered by the `<` can't be zero-cost

Re:Show HN: A pure bash web server. No netcat, socat, etc. by aidenn0 on Tuesday January 04, 2022 @01:41PM (#29798309)
If you want to do left-to-right, you can do that:

    while :; do <page.html nc -l 80 ; done
Re:Show HN: A pure bash web server. No netcat, socat, etc. by dzove855 on Tuesday January 04, 2022 @09:54AM (#29795383)
Yeah sure. I have see it, but basically this one use bash builtins instead of external commands.