Very much so! I wanted to write it in pure bash without nc but couldn't find a way straight away. The main change here is the -c which lets you drop the connection at EOF so you can have psudo-concurrent connections.
And the initial : is there to create a valid executable line where variables and brace expansions are actually evaluated, but without actually doing anything else. : is a synonym for true.
Sort of like a comment in that no statement is executed (well a statement is executed, it's just a almost-no-op statement) but unlike a comment in that the braces are evaluated, and any actions they perform like assignments, are evaluated and performed.
Most brace expansions just expand something in place, they essentially mostly just read and display something, possibly modifying it along the way from memory to output. But a few brace expansions actually do assignments. And that "set to this value if not already set to anything" is very useful. So a common trick is things like
:${DEBUG:=false}
: is just a synonym for "true" ie a valid command that does nothing but also does not produce an error and also is an executable statement not a comment.
Everything after the true is evaluated for real just like if true were instead echo or any other command.
The end result of that debug example is now all through the rest of the script you can put $DEBUG && echo doing step 7...
and never have to worry about the invalid syntax condition if DEBUG had not been set to something. All else being equal, it's always going to be set to false, and so all those debug commands will be valid syntax.
But you can enable verbose mode later at run-time without editing the script by running
DEBUG=true myscript
The := brace expansion means that since DEBUG was set to something, it's not modified, not set to false. it's only set to false if it was unset to begin with.
It's basically the shortest way to define a configurable with a default value at the same time.
Unneccessary $() fork, unnecessary cat, and at best a bit thoughtless to expand the file's contents onto a commandline just to squirt it. Sorry buddy, but, do not be proud of this.
[edit I should not simply say that without showing what is supposedly the better way]
There are several versions of nc, mine uses -N to exit on eof.
From here it's only a little more to read -n 4096 to read in 4k chunks instead of in lines, since a valid html document could actually be all on one line of arbitrary unlimited length, and the final version above would be no different from the "allow large cmdline" version.
What about a systemd socket activated service that passes the network data through stdin? They'll spawn a new bash process for every incoming connection, and technically wouldn't need nc.
A very (very) simple Hello World program in Haskell.
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE TemplateHaskell #-}
module Main where
import Foreign.C.Types
import qualified Language.C.Inline as C
C.include "<stdio.h>"
C.include "<stdlib.h>"
main :: IO ()
main = [C.block| void {
system("python -c 'print \"Hello, World!\"'");
} |]
Better to not announce to the world "Hey everyone! Whaddya think?" If you can only tolerate approving answers. No one made them post a bad version of a one-liner that could already be googled up from 20 other examples already out there and better engineered.
This sort of send-up is actually perfectly on target, and frankly actaully light hearted.
Everyone posting critiques like this and like my own, knows full well that they have their own excrable works of art out there. We've all been figurative 7 year olds and made terrible crayon drawing programs, and even been ignorantly and adorably proud of them at the time.
45 comments
[ 3.6 ms ] story [ 103 ms ] thread/dev/tcp can only connect to a listening socket, not create one.
http://git.savannah.gnu.org/cgit/bash.git/tree/examples/load...
https://en.wikipedia.org/wiki/Netcat#Performing_an_HTTP_requ...
ref: https://news.ycombinator.com/item?id=1873386
Here's a web server in pure bash:
https://github.com/dzove855/Bash-web-server
Edit: I'm not sure what other distros might do with it.
: = does nothing but the := in the variable declariation will assign set the value of BASH_LOADABLE_PATH to /usr/lib/bash if the variable is empty.
So instead of writing: BASH_LOADABLE_PATH=${BASH_LOADABLE_PATH:-/usr/lib/bash}
you can just do : : "${BASH_LOADABLE_PATH:=/usr/lib/bash}"
Sort of like a comment in that no statement is executed (well a statement is executed, it's just a almost-no-op statement) but unlike a comment in that the braces are evaluated, and any actions they perform like assignments, are evaluated and performed.
Most brace expansions just expand something in place, they essentially mostly just read and display something, possibly modifying it along the way from memory to output. But a few brace expansions actually do assignments. And that "set to this value if not already set to anything" is very useful. So a common trick is things like
:${DEBUG:=false}
: is just a synonym for "true" ie a valid command that does nothing but also does not produce an error and also is an executable statement not a comment.
Everything after the true is evaluated for real just like if true were instead echo or any other command.
The end result of that debug example is now all through the rest of the script you can put $DEBUG && echo doing step 7...
and never have to worry about the invalid syntax condition if DEBUG had not been set to something. All else being equal, it's always going to be set to false, and so all those debug commands will be valid syntax.
But you can enable verbose mode later at run-time without editing the script by running
DEBUG=true myscript
The := brace expansion means that since DEBUG was set to something, it's not modified, not set to false. it's only set to false if it was unset to begin with.
It's basically the shortest way to define a configurable with a default value at the same time.
https://github.com/Krowemoh/bash-server
[edit I should not simply say that without showing what is supposedly the better way]
no large cmdline, allow cat
while : ;do { printf 'HTTP/1.1 200 OK\n\n' ;cat index.html ; } | nc -N -l $PORT ;done
no cat, allow large cmdline
while : ;do IFS= read -d '' -r x < index.html ;printf 'HTTP/1.1 200 OK\n\n%s' "$x" | nc -N -l $PORT ;done
no cat, no large cmdline (though not strictly enforced)
while : ;do { printf 'HTTP/1.1 200 OK\n\n' ;while read -r x ;do printf '%s' "$x" ;done <index.html ; } | nc -N -l $PORT ;done
There are several versions of nc, mine uses -N to exit on eof.
From here it's only a little more to read -n 4096 to read in 4k chunks instead of in lines, since a valid html document could actually be all on one line of arbitrary unlimited length, and the final version above would be no different from the "allow large cmdline" version.
A bit patronizing. It's just a fun and simple project. Not something to be all serious about
This is 'Show HN', not code review. You are being excessively negative.
You are a plonker.
I see C and Python/some kind of FFI.
EDIT: Just explaining the criticism, not necessarily subscribing to it - I don't think OP's point was to write something in "pure" Bash
Better to not announce to the world "Hey everyone! Whaddya think?" If you can only tolerate approving answers. No one made them post a bad version of a one-liner that could already be googled up from 20 other examples already out there and better engineered.
This sort of send-up is actually perfectly on target, and frankly actaully light hearted.
Everyone posting critiques like this and like my own, knows full well that they have their own excrable works of art out there. We've all been figurative 7 year olds and made terrible crayon drawing programs, and even been ignorantly and adorably proud of them at the time.
with 30 lines of code it has directory listing and even tried to have something called BHP which is like PHP but bash.
> Shinatra - A web server in five lines of bash