Live data from Hacker News

Web server in one line of bash

razvantudorica.com

31–40 of 69 posts

Re: Web server in one line of bash

#32
I remember trying to write a web server that can respond to requests in bash, several years ago. Since it didn't have sockets support that part was going to be handled by netcat. The problem I ended up with was that bash couldn't read and write to the same pipe. The mkfifo solution was the closest that I got to. It turns out that ksh was perfectly capable of reading and writing to the same pipe at that stage. Not long after I got stuck with bash, awk started to support sockets so I used that. Then bash started to support sockets, and many more features, so I suspect it's possible to write a solution in bash now that doesn't need netcat, but might still need a fifo.

Re: Web server in one line of bash

#33
Since everyone uses the one line of bash to just start their favourite complex network utility, I'll also contribute mine:

while true; sudo /usr/sbin/apachectl start; break; done;

Re: Web server in one line of bash

#35
post #34

You can actually write a web server in pure bash, with no netcat, using /dev/tcp (bash magic). https://www.gnu.org/software/bash/manual/html_node/Redirecti... http://www.linuxjournal.com/content/more-using-bashs-built-d...

That's demonstrating an HTTP client, not a server. I don't think there's a way to make /dev/tcp accept incoming connections.

Re: Web server in one line of bash

#36

$ busybox httpd Works even in an initramfs, and in some embedded systems. Serves up the current directory, and you can even have CGI scripts in cgi-bin.

Thanks! I'd forgotten about busybox, but that's often how your home router is doing things. * Side note: if it is, make sure your router manufacturer provides the GPL source release.

I dont think they usually use it as the web server, as they need some scripting not static pages only. The smallest server I know of that can do cgi is webfsd).
Post reply on HN