![]() ![]() Creating CGI Programs with Bash: Getting Started<-- Page 1: Introduction | Page 3 - Handling POST Data -->Beginning a Bash ScriptShell scripts start with a line identifing the file as a script and telling the operating system what script interpreter to use. Since we're using Bash, we'll tell the system to use Bash as the script interpreter: #!/bin/bash
The #! on the first line tells the system that the file is a shell script, and everything after the ! is the path to the interpreter to use to process the script. On most systems, the path to Bash is /bin/bash, but it may be something else on your particular system. If /bin/bash doesn't work, try /usr/bin/bash, and if that doesn't work, ask your system administrator or host provider what the path to Bash is on your system. The rest of a Bash script consists of either Bash code or other programs. While programming in Bash, you will use many other programs found on the system to accomplish tasks not built into Bash, such as text parsing or sending HTML back to the browser. Variables in BashVariables in Bash can be created at any time and have no specific type. The following code creates several variables:#!/bin/bash
In this example a variable VARIABLE is created which holds "This is a variable" while a variable named ANOTHER is created
that holds the output of the command wc -l /tmp/exampleVARIABLE="This is a variable" ANOTHER=`wc -l /tmp/example` Capturing the output of a command and saving it in a variable is a very important tool in Bash. To do so, use VARIABLE_NAME=`command` To reference a variable (ie get its value), put a dollar sign in front of the variable, as in the example below: echo "VARIABLE=$VARIABLE"
Which outputs
VARIABLE=This is a variable"
In this example, the command echo is used to output text. The outputted text is VARIABLE= and then the content of the variable VARIABLE, which is referenced with $VARIABLE. Creating an Extremely simple CGI in BashBelow is an extremly simple CGI program in Bash. All it does is show the user a webpage that says "Welcome to your Bash CGI!" #!/bin/bash
This Bash program, when run by the web server, will send a page that says "Welcome to your Bash CGI!" to a browser window.
The output looks like the screenshot below:echo "Content-type: text/html" echo "" echo "<html><head><title>Welcome</title></head>" echo "<body>" echo "Welcome to your Bash CGI!" echo "</body></html>" ![]() The most important lines in this file are: echo "Contant-type: text/html"
These two lines tell your browser that the rest of the content comming from the program is HTML, and should be treated as
such. Leaving these lines out will often cause your browser to download the output of the program to disk as a text file
instead of displaying it, since it doesn't understand that it is HTML!echo "" This program uses the echo program to send text to a browser. Normally, echo prints whatever comes after it to the screen, but in Bash CGIs, it instead sends whatever is after it to the browser. Using echo you can send any text to the browser. Rembmer that the browser will treat it as HTML, so you can also include HTML formatting tags to make the output of your CGI look however you want. Creating a Form in HTMLNow that handling variables is covered, we'll create a simple CGI program in Bash that gets some data from an HTML form. CGI programs are often started by a user submitting HTML form data, with the CGI program then processing the result and giving some kind of output. First, lets start by creating a simple HTML form, which will submit data to our CGI program, which is /cgi-bin/example.sh <form action="http://www.example.com/cgi-bin/example.sh" method="get">
Enter a username: <input type="text" name="username"></input><br> <input type="radio" name="whatToDo" value="remove" checked="checked">Value<br> <input type="radio" name="whatToDo" value="create">Create<br> <input type="submit" name="subbtn" value="Submit"> <form> For those unfamilar with HTML forms, here's a breakdown of the above:
username=John Doe
Assuming the user entered "John Doe" into the text box and selected the "create" radio button.
whatToDo=create subbtn=Submit Getting Form Data in a Bash CGIWe've created a great HTML form that submits data to a CGI program. Now all we need to do is create a CGI program that does something with this data. Below is a simple Bash program that will tell the user what they entered: #!/bin/bash
This will give the person who clicked submit in our form a webpage that tells them what they entered into the form.
For example, if the person enterd "John Doe" as their username and clicked the "Create" radio button, they would get
a webpage that says:
echo "Content-type: text/html" echo "" USERNAME=`echo "$QUERY_STRING" | sed -n 's/^.*username=\([^&]*\).*$/\1/p' | sed "s/%20/ /g" WHATTODO=`echo "$QUERY_STRING" | sed -n 's/^.*whatToDo=\([^&]*\).*$/\1/p' | sed "s/%20/ /g" echo "<html><head><title>What You Said</title></head>" echo "<body>Here's what you said: echo "You entered $USERNAME for username and wanted the action to be $ACTION. echo "</body></html>" You entered John Doe for username and wanted the action to be create.
Lets look at how this works, line by line:
The lines that actually get the form input from the form into the CGI are copied below, and color coded:
USERNAME=`echo "$QUERY_STRING" | sed -n 's/^.*username=\([^&]*\).*$/\1/p' | sed "s/%20/ /g"
How does this work?
WHATTODO=`echo "$QUERY_STRING" | sed -n 's/^.*whatToDo=\([^&]*\).*$/\1/p' | sed "s/%20/ /g" When your user hits the submit button in your HTML form, the web browser sends everything they put into the form to the CGI program referenced in the form. Since the form uses the GET method to transmit data, the web server takes the data it receives from the form and puts it in an enviromental variable named QUERY_STRING. It then starts the CGI program referenced in the form, which is our Bash program. Our Bash program begins with the normal initialization of a Bash CGI program. Eventually, it needs to read in the form data. Remember that this is stored in an enviromental variable. Lines 3 and 4 of our Bash CGI, color coded above, do the work of pulling the data out of the enviromtal variable and into variables in our Bash program. First, a variable in Bash is created (colored red). The variable is created by parsing reading the QUERY_STRING enviromental variable set by the web server (green). The QUERY_STRING variable is sent through the sed program, which looks for a pattern matching the name of a form element (blue) and collects any data belonging to that form element. Finally, the result is sent to another sed program (orange), which replaces any occurances of %20 with a space, since when the form data is transfered from the browser to the server, spaces are turned into %20 (so "Hi there" becomes "Hi%20there"). You can use any Bash variable you like, and to get different form elements, simply change the name sed looks for, which in this example is hilighted in blue. <-- Page 1: Introduction | Page 3 - Handling POST Data -->TigerTronics is primarily sponsored by ![]() |
||