views:

349

answers:

4

Hello folks,

I have a little apache2 CGI application on the Ubuntu. The CGI handler is bash shell script.
My client application is search.html:

<html>
<body>
<form action="/cgi-bin/search.sh" method="post">
    <input type="text" name="searchKey" size="10"></input>
    <input type=SUBMIT value="search">
<form>
</body>
</html>

firstly, I just want to catch value of "searchKey" parameter in server side. I tried like following, but displaying nothing.
search.sh is:

#!/bin/bash
echo Content-type:text/plain 
echo ""

echo $SEARCHKEY

Guys, can you tell me how to catch value of the parameter in the server side?

UPDATE

thank you for all answers.I understood that to get a value of post request need to read data from STDIN.
i tried as Ithcy suggest like following

#!/bin/bash
echo post=$(</dev/stdin)
echo 'content length:'$CONTENT_LENGTH
echo 'content:'$post

it was displaying only that:

content length:30
content:

why is content nothing? do i need to do more configure on Apache server to read post data? Thanks

A: 

Try

echo $1

instead of

echo $SEARCHKEY
klausbyskov
klausbyskov, thank you for the answer.But it's still displaying nothing
Nyambaa
+1  A: 

The whole querystring is represented in the $QUERY_STRING variable. You can see this by running env without arguments in your shell script.

Example for getting only the searchKey value:

echo $QUERY_STRING | sed 's/searchKey\=\([^&]\+\).*/\1/'

Update: I'm sorry, this only applies if you are using GET to post your form. I didn't read the details =/

If you really need to read POSTs, this page may help you: http://digitalmechanic.wordpress.com/2008/02/21/handling-post-data-in-bash-cgi-scripts/ I didn't get it to work, though.

Emil Vikström
Emil, thank you.The link was useful.
Nyambaa
POSTs just pass the data via stdin, so can be handled just like any other data passed via stdin... which I have no experience doing in shell scripts.
R. Bemrose
+1  A: 

This is good documentation about the CGI protocol: http://hoohoo.ncsa.illinois.edu/cgi/

I'd suggest you consider using a language (such as Perl) with a good CGI library so you don't have to reinvent a wheel that's been perfected years ago.

glenn jackman
+1 for using perl rather than bash to handle CGI.
ithcy
+3  A: 

POSTs will come through STDIN.

#!/bin/bash
POST=$(</dev/stdin)
echo $POST

But you really should look at using perl (or python, PHP, etc) if you can, as Glenn Jackman suggests.

ithcy