while (<>) {
print;
}
Still Awk is shorter: { print }
And if you are allowed change the switches of the command line to Perl for your program that Perl program can have exactly 0 bytes.By default, unless you use the BEGIN block or something, awk will run your program on each line of stdlin. This is useful for programs of the type:
(/some regular expression/) { some action}
The default action if you don't specify one is "print $0" (the whole matching line). If your condition is a plain-ol' expression rather than a regular expression, and it always evaluates truthy, you thus get every line.
Still the shortest Perl is 0 bytes when the command line switch is allowed to be -p
Update: That's actually called "the sed mode of Perl." Moreover these two command lines behave similarly:
perl -pe 's/search/replace/g'
and sed 's/search/replace/' sed < input > output
:)[1] http://robertkotcher.com/sed.html
[update] s/set/sed/
perl -pe '' <input >outputI was referring to an empty SED-program that would then be run via 'sed -f empty-file.sed'. Of course you need >0 bytes to invoke the SED program.
Your example is very much just a special shortcut for one special condition. It's not generalizable in the same way. And let's be honest - most of today's code is not writing to standard output, it is writing to some iOS or Android GUI or outputting JSON to be used in some javascript webapp. That kind of shortcut just doesn't make sense anymore and is a relic of a different time.
http://computer.howstuffworks.com/cgi3.htm
The server opens the socket, redirects the stdout to his socket while calling your program fully unmodified.
As for serving files, you'd normally let nginx or apache take care of the sockets for you and not use output redirection.
In Plan9 it's actually viable to use network sockets as files which is pretty nice, but for Linux it's not really how people do it.
Please explain that to all the people who wrote CGI scripts for years. Seen the link at all?
The ability of a language to easily write to std out is simply not important anymore apart from shell scripting which is mostly used these days for setting up the runtime environment and building other languages to handle the grunt work.
I don't know of any companies that still pipe std output directly to sockets anymore, but I guess there must be some. If your company still does this then a language feature like that makes sense. For most people, having the ability to write code that can directly write to sockets without piping through a shell is better practice.
The major advantage that CGI gives you is the fact that you get a new, clean process on each request. This means you have fewer and simpler failure cases. This can be helpful when you have a line of business web application though it is certainly suboptimal for public-facing scalable systems.
I wouldn't say cgi is dead. It still has a role to play. It is not the ideal way for doing many things though. I.e. they are no longer the go-to tool, but rather one tool for relatively lower-volume applications where performance matters less than some other considerations.
$ printf 'binarydata'|awk '{ print }'|hexdump -C
00000000 62 69 6e 61 72 79 64 61 74 61 0a |binarydata.|
0000000b grep $
That reads lines from stdin and echoes to stdout. #!/bin/sh
catThat post, maybe?