From: Cameron Simpson To: Kenneth Chin Cc: Bcc: Subject: Re: shell script question Reply-To: cs@zip.com.au In-Reply-To: <227721641DCBD311924300508B6FA44405BD40@arky.the-ark.com>; from klchin@the-ark.com on Tue, Mar 21, 2000 at 07:10:28PM -0500 On Tue, Mar 21, 2000 at 07:10:28PM -0500, Kenneth Chin wrote: | I'm just a beginner at writing cgi programs and was trying to create a | script | that will take form data from a web page and save it to a flat file. | | If you have time, I just need to know how does the shell script read the | data coming from the form? There are two ways, depending on the form. HTTP requests may be either GET or POST requests. For a GET request the form values are appended to the CGI URL: GET http://blah.com/path/to/cgi?field1=value1&field2=value2&... HTTP/1.0 For a POST request the HTTP request is just: POST http://blah.com/path/to/cgi HTTP/1.0 and the form parameters are in the body of the request after the MIME headers: POST http://blah.com/path/to/cgi Content-Type: form/url-encoded ... other headers ... field1=value1 field2=value2 ... This isn't that pertinent, as the CGI interface says that in the former case the environment variable $QUERY_STRING will contain the values and in the latter case the values are available on stdin. There's an environment variable $HTTP_METHOD (I think - should check...) which says which method is in use, but I just check the length of $QUERYSTRING: if [ -n "$QUERY_STRING" ] then ... must have been a GET ... ... parse $QUERY_STRING to get values ... else ... I guess it was a POST ... ... read values from stdin (the headers have already been consumed and placed in environment variables) ... fi Now, I tend to write all my forms to use the GET method because it lets the user bookmark the results (since the form parameters are part of the URL). Given that assumption, I point you to the script: http://www.cskk.ezoshosting.com/cs/scripts/query_string2sh which I use to parse the query string. Here's an example CGI script we have here: #!/bin/sh # # CGI query utilising script, "/usr/local/script/askcisco" # at the moment, merely takes in the switch name # and port number, to show the status # note: also utilises script: "/usr/local/script/query_string2sh" # - Jared Berghold 10aug99 # PATH=$PATH:/usr/local/script:/usr/local/bin export PATH echo "Content-Type: text/html" echo if [ -n "$QUERY_STRING" ] then eval `query_string2sh "$QUERY_STRING"` sw=$PARAM_SWITCH pt=$PARAM_PORT echo "

Port status for $pt on $sw

" echo "
"
	    askcisco "$sw" show port status "$pt" 2>&1
	    echo "
" fi exit 0 The relevant lines are: eval `query_string2sh "$QUERY_STRING"` sw=$PARAM_SWITCH pt=$PARAM_PORT This script expects to be the action of a form with two fields: SWITCH and PORT. query_string2sh generates shell statements to place those values, decoded, into the variables $PARAM_SWITCH and $PARAM_PORT respectively. So you just run that inside an eval and there you go. Cheers, -- Cameron Simpson, DoD#743 cs@zip.com.au http://www.zip.com.au/~cs/ A clean desk is the sign of a blank mind.