How to run script through CGI?

Hi

I have written a script and I want it to be run from web with the help of CGI. can you please guide me .

below script is working fine if run from backend but not sure how I should run through web.

 
 #!/bin/bash
 
string1=look
string2=0
string3=.sdn.dnb
 
echo -n "enter the number"
 read string2
 mystring="$string1 $string2"
ustring="$mystring$string3"
 
echo $ustring
echo $ustring >s.txt
sed 's/^\(.\{20\}\)/\1./' s.txt >p.sh
./p.sh >v.sh
cat v.sh | grep -i "Name:" 
 

This won't work right in CGI for a few reasons; first, CGI has no keyboard input and can't read a number from the user. Second, you usually can't - and definitely shouldn't - write to the current folder in a CGI script.

Third, you never, ever want a CGI script to be running self-generated code.

How to run it from CGI also depends on your web server. What is your CGI folder?

under /var/www

---------- Post updated at 03:16 PM ---------- Previous update was at 03:15 PM ----------

I am able to write simple CGI script using HTML . however I want to pass the argument from web to get the o/p.

A CGI script can't ask for input, it has to receive it in the first place, i.e. given by an HTML page web form. If it doesn't receive the input it wants, it should print a warning and quit.

<html><body>

<form action="http://mysite/cgi-bin/myscript.cgi" method="get">
<input type='text' name='somevalue1'>
<input type='text' name='somevalue2'>
<input type='submit'>
</form>
</body></html>

Hitting submit on this webpage will cause it to load a URL like http://mysite/cgi-bin/myscript.cgi?somevalue1=asdf&somevalue2=qwerty . The method="get" is important, since POST requests work differently.

The entire string somevalue1=asdf&somevalue2=qwerty will be found in QUERY_STRING which you can process as you please. I often do something like:

OLDIFS="$IFS"
IFS="&="
set -- $QUERY_STRING
IFS="$OLDIFS"

...which will leave $1 as "somevalue1", $2 as "asdf", $3 as "somevalue2", and $4 as "qwerty". set -- is a special, ancient syntax which sets the shell's $1 $2 ... argument variables, and IFS is a special variable which controls what characters the shell splits on - usually spaces, but here we use it to split the CGI string on ampersands and equals.

You can process that in a loop like

OLDIFS="$IFS" ; IFS="&=" ; set -- $QUERY_STRING ; IFS="$OLDIFS"

# HTML header must come before any other text is printed
echo "Content-type: text/plain"
echo

echo "QUERY_STRING was ${QUERY_STRING}"

while [ "$#" -gt 0 ] # Loop until $#, number of arguments, is zero
do
        case "$1" in
        somevalue1) echo "somevalue1 got $2" ;;
        somevalue2) echo "somevalue2 got $2" ;;
        *) ;;
        esac

        shift 2 # Delete $1 and $2, and move $3 $4 down
done