Shell Script For The Web
I want to know if there is any way to use Bash(Shell Script) to make web pages, like Perl,开发者_StackOverflow C++ and others through CGI. And if there is any way, where I can find a good free hosting for it?
This is definitely possible, and just about any Linux-based (or BSD-based, I guess) shared hosting would be able to do it. I've done it for little test pages etc on my cheap 1&1 account.
On my host, I just call a file "foo.cgi", and I put the #!/bin/sh
line at the top. Works fine. Of course, it's kind-of a mess for anything complicated.
#!/bin/sh
cat <<banana
Content-Type: text/html
<html>
<head>
<title>Hello, World!</title>
</head>
<body>
<h1>Hello, World!</h1>
</body>
</html>
banana
Obviously a real script could do other stuff. HTTP info is generally in environment variables, but I can't clearly recall exactly how that works and it might vary depending on the web server involved.
It is very possible, and at the time I started in the business, it was the way things were done. Although, instead of bash scripting, I used perl or C.
The real difference being, is that PHP provides a very convenient way to access input data, as where as in bash (although I assume there are utilities to help you) you'd need to parse and handle raw input instead of "cooked" variables.
Depending completely on what you're trying to accomplish, it is either convenient or the other extreme. I, myself, use a bash script to compile my home page with daily news and comics, but that one requires no input, but merely a cron routine.
I would recommend that you consider creating a Python or PHP wrapper around your shell script if you'd prefer not to do a total rewrite. Let the shell script do the backend stuff and the wrapper do the rest.
Even though it is possible to do, it would not be practical to try to use shell scripts to serve as a pre-processor for web-pages. I suppose you could always create an Apache handler which would trigger your shell script and Apache would take the output from your shell script and toss it back to the client. However, using a shell script in this manner would pose many security challenges (none of which are insurmountable but they would be challenging nonetheless).
Maybe have a look at http://nanoblogger.sf.net (as a starting point)!
Here's how you can embed a newline character, ... in a (double) quoted string:
NL="
"
CR="$(printf "\r")"
echo "abc${NL}def"
IFS=""
echo abc${NL}def
printf "%q\n" "abc${CR}${NL}def"
精彩评论