You are here:

Unix Shell Scripting/Running shell script from a web browser

Advertisement


Question
Dear Chris,

I have some Unix Shell Script that needs to be made to run from a web browser. Since many users do not want to use command line interface, I need to build a graphical user interface that will run this Shell Script.

Any information regarding this would be much appreciated.

Thank you.
-Neo

Answer
Given that the web server is set up to allow CGI scripts (most are),
the steps are as follows:

1. Use a shebang on the first line of the script that tells the server
what shell to use to run the script. For example, it could be one of
these:

#!/bin/sh

#!/bin/bash

#!/usr/bin/ksh


2. Send a header declaring what type of output the script creates.
Usually this is HTML. The header must be followed by a blank line:

printf "Content-type: text/html\n\n"


3. Make the script executable, but not writable by anyone other than
the owner of the script:

chmod 755 /path/to/script


4. Install the script in a directory on your web site.


5. Test the script. For testing, it is often useful to redirect the
standard error, either to a file, to the web page it produces, or to
the bit bucket:

exec 2>error-log  ## file
exec 2>&1         ## the web page
exec 2>/dev/null  ## discard all error messages


An example of a simple CGI script can be seen at
http://cfaj.freeshell.org/testing/  

Unix Shell Scripting

All Answers


Answers by Expert:


Ask Experts

Volunteer


Chris F.A. Johnson

Expertise

I can answer any questions on using and programming the Unix shell, including the standard POSIX shell and bash.

Experience

I have been writing shell programs since 1990 and have written two books on the subject, both published by Apress.

Publications
Pro Bash Programming:Scripting the GNU/Linux Shell, Apress 2009 Shell Scripting Recipes: A Problem-Solution Approach, Apress 2005

Education/Credentials
I completed a Unix systems administrator course given by AT&T Canada.

©2016 About.com. All rights reserved.