views:

84

answers:

6

I want to know if there is any way to use Bash(Shell Script) to make web pages, like Perl, C++ and others through CGI. And if there is any way, where I can find a good free hosting for it?

+3  A: 

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.

Pointy
Then you do this using CGI? Why you're using `cat`. Can't I use `echo`?
Nathan Campos
Sure you can use `echo`, but the syntax to echo something which contains newlines is weirder.
David Zaslavsky
Nathan Campos
@David: You can replace `cat <<banana` with `echo '` and the final `banana` with `'` and it would work exactly the same as **Pointy's** example. You must be referring to something like `echo -e '<html>\n <head>\n <title>...'`. `printf` would be better than `echo` anyway, since you can do something like a template more easily (although that's easy enough with here docs).
Dennis Williamson
You'll want to add something like "echo Content-type: text/html; echo" before the other output. If your script doesn't output some headers followed by an empty line, the webserver will send out a 500 error instead of your content.
slowdog
@Dennis: interesting, I thought I remembered trying unsuccessfully to embed newlines in a quoted string like that.
David Zaslavsky
@slowdog Oh yes, you're right; you want at least that in the header. I'll edit the answer!
Pointy
Pointy
@David: If you set a variable with embedded newlines and then `echo` it without quoting the variable, you'll lose the newlines. `test=$'line one\nlinetwo'; echo $test; echo "$test"` The first `echo` will put everything on one line, the second one will preserve the newlines. You could just as easily do `test='line one[press enter]line two'` but the `$'...'` fits better in these comments and in some cases is more readable in a script.
Dennis Williamson
+1  A: 

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).

arunabhdas
Well it's not really that much different than using PHP or Python.
Pointy
Well... kinda. Perl and Python can be "patched" into the web server using their own modules (`mod_perl` and `mod_wsgi` respectively), so that the interpreter actually runs as part of the server, and it winds up being quicker than having to start a separate process for each request.
David Zaslavsky
The "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" is this: http://en.wikipedia.org/wiki/Common_Gateway_Interface
slowdog
+2  A: 

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.

nikc
+1  A: 

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.

Dennis Williamson
+1  A: 

Maybe have a look at http://nanoblogger.sf.net (as a starting point)!

juno
+1  A: 

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"
joe