HTTP GET Data and Bash CGI
Here's a brief look at one method of processing HTTP GET data with a Bash CGI script.
If you use PHP you're probably familiar with how GET variables work in that language. PHP has easy-to-use capabilities for working with multiple parameters in a GET request, for example:
test.php?shortdate=2011-03-10&month=March&day=Thursday
A Bash CGI script lacks these custom functions. Essentially, when you pass data in a GET request, everything is stored in a single environmental variable called $QUERY_STRING. So if we called a Bash CGI script like this:
test.cgi?shortdate=2011-03-10&month=March&day=Thursday
We would have just one variable -- $QUERY_STRING -- with the value:
shortdate=2011-03-10&month=March&day=Thursday
That's not particularly useful. Obviously we need to do more work to load that GET data into useful variables.
When I started experimenting with this, one thing I realized is that the "variable=value" format isn't really necessary for basic GET queries. So instead I just separate data fields with a delimiter (I generally use the pipe symbol but you might want to use something different).
So the GET request above would look like this:
test.cgi?2011-03-10|March|Thursday
When that GET request is submitted, we need to split the query string into appropriate parts. There are various ways of doing this; one method is by using the Bash IFS.
IFS is the built-in Bash Internal Field Separator, used to recognize breaks between data fields. By default it is set to "whitespace" -- a space, tab or newline.
We can change the IFS to our delimiter (a pipe) so that Bash correctly recognizes breaks in the GET data. But before changing the IFS we save the current IFS to a variable ($OLDIFS), so we can reset it to the default after we're done.
To process the example GET request above:
# Save the current IFS:
OLDIFS="$IFS"
# Change IFS to the pipe character:
IFS=$'|'
# Create an array with the GET data.
VARS=($QUERY_STRING)
# Create individual variables from the array.
SHORTDATE=${VARS[0]}
MONTHNAME=${VARS[1]}
DAYNAME=${VARS[2]}
# Reset the IFS to the default
IFS="$OLDIFS"
Now we can use the individual variables -- $SHORTDATE, $MONTHNAME, $DAYNAME in the remainder of the Bash script.
An important thing to note is that this method depends on the position of parameters in the GET request. So you need to be careful and consistent in how you construct the GET requests. For example:
test.cgi?2011-03-10|Thursday|March
Would produce very incorrect results. :)
Btw, I managed to get your status script working as cgi, but I need to modify the temp files and how the stats are written :P