From: Cameron Simpson To: redhat-list@redhat.com Cc: stefano.passarella@caboto.it Bcc: Subject: Re: SHELL & CGI Reply-To: cs@zip.com.au In-Reply-To: <00dd01c08a08$30168e90$2501670a@caboto.it>; from stefano.passarella@caboto.it on Mon, Jan 29, 2001 at 04:19:51PM +0100 On Mon, Jan 29, 2001 at 04:19:51PM +0100, Stefano Passarella wrote: | I have a shell ( grep and many echo command) , which prints in the | stdout the HTML tabs. It works well, if it runs as comman ('shell_name | $1'). | | I would use it via WWW. I have readen that I can use CGI and I get | some informations from cgi.resourceindex.org. Unfortunately, I have | found only one example. Shell works just fine for many simple CGIs, and also for some complex CGIs if you have the right tools. The main thing you need to remember is that a web page (i.e. the output of your shell script) is a MIME document, so there are headers denoting its type. For example: echo Content-Type: text/html echo echo '

Here is some HTML

' or echo Content-Type: text/plain echo echo 'This is plain text.' echo 'See how the < angle brackets > dont do anything?' or echo Content-Type: application/octet-stream echo cat some-binary-file-the-user-will-download Another thing is that the web server must know that these scripts are CGI scripts and not mere data files. On many servers it is enough to have the script's extension be ".cgi". The #! line will arrange for the right interpreter to run the script. Thus: % cat myscript.cgi #!/bin/sh echo Content-Type: text/html echo echo '

Here is some HTML

' The script (like all scripts) must be be public readable (so the interpreter - /bin/sh - can open it) and executable (so the OS doesn't refuse to treat it as executable): % chmod 755 myscript.cgi If your script takes requests, like: http://some.where.com/.../myscript.cgi?X=1&Y=2 then these are available in the environment variable $QUERY_STRING, which I routinely decode into normal shell variables with query_string2sh: http://www.zipworld.com.au/~cs/scripts/query_string2sh which also needs hexify: http://www.zipworld.com.au/~cs/scripts/hexify You use it like this: eval "`query_string2sh`" echo "X is $PARAM_X" echo "Y is $PARAM_Y" (It stuffs PARAM_ onto the front of the variable names so that it doesn't trash your normal variables). -- Cameron Simpson, DoD#743 cs@zip.com.au http://www.zip.com.au/~cs/ Carpe Datum - John Sloan