Thanks to the GeekTalk MailList and CoryWebb I now know how to capture form variables using a bash CGI script. Very cool. Here's [ his post] to the list:


So I've been working on a way to make a blog-type thing using nothing but html and bash (no stylesheets, no perl, no java, no databases, etc) and have come up short handed in the past, always requiring at least *some* perl to take input from forms. clog.spack.org/cgi-bin/index.cgi is an example of this.

I was going to try out bashlib (bashlib.sourceforge.net) but Since I don't want potential users of this software to have to install ANYTHING other than a tarball to get rolling, I decided against it.

While playing around, I wondered if I could use $1, $2, $3, etc for input to a bash cgi. While I can't say:

"http://www.webserver.com/cgi-bin/bash.cgi hello" 

in my location bar thing, I found out that I can use something like:

"http://www.webserver.com/cgi-bin/bash.cgi?hello world" 

and everythign after the "?" is interpereted as "$1".

Great, I think, Now I can just point all my html form, using "get" method, to this bash script, and it can parse $1 and do stuff with that. The problem I encountered is that if there is an "=" in your query string, then $1="". suck. After trying to figure out a way to get a form to submit a get method without an "=" (I'm pretty sure it's impossible) I found an easier solution.

While reading webmonkey, I was glancing over the perl/cgi section to get some ideas, when I can across this chuck of perl code:

if ($ENV{'REQUEST_METHOD'} eq "GET") { 
        $in = $ENV{'QUERY_STRING'};
} else {
        $in = <STDIN>;
} 

so... that tells me that a form that uses the "post" method sends the query string to stdin of the script defined by action=.

ha ha! so all I have to do is use post method instead of get, and I don't need any additional libraries, softwares, cookies, etc. just bash and apache. hooray.

check it out:


And for the second round from Cory:

>you could just bundle bashlib with your application :-)

Most of bashlib deals with cookies, that I don't need.

> that's really cool, i always wondered host posts worked.

So did I. I read that webmonkey doc when I made my first cgi-script (was anyone here around when I made drinking buddy?) but at hte time, I didn't know what <STDIN> was, so I used get for everything. When I re-read it this weekend and saw the perl script was using stdin if it wasn't get method... it all just came together.

> how does it delimit form variables?

in your form you specify things like:

Your name: <input type=name name=test align=top><br> 

when you subit, is sends "name=whatever you typed in there" to the receiving script. so I can just do:

name=`cat - | awk -F\= '{print $2}'` 
echo "hello $name!"

of course if the form has muliple inputs, it seperates them with a &, and since the receiving script can do like a tr '&' ' ' on it and a for namepar in $variable ...

> is there a better way to capture stdin then:
>
> var = cat -

well, that's what I'm using. I haven't really thought of what might work better yet.


CategoryProgramming

BashCgi (last edited 2003-11-07 01:10:36 by AdamShand)