How to run script through CGI?

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers How to run script through CGI?
# 1  
Old 05-26-2017
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.

Code:
 
 #!/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:"

# 2  
Old 05-26-2017
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?

Last edited by Corona688; 05-26-2017 at 01:22 PM..
# 3  
Old 05-29-2017
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.
# 4  
Old 05-29-2017
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.

Code:
<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:

Code:
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
Code:
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


Last edited by Corona688; 05-29-2017 at 12:54 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to convert my /bin/sh script with cgi and html to run it on browser!??

Hello, I want to run this script on my CentOS 6 via browser : ________________________________________________________________________________________________ #!/bin/sh echo Username? read MY_NAME echo Provisional file name? read MY_FILE echo File NAME you want to save? read MY_FILE2... (16 Replies)
Discussion started by: juta2020
16 Replies

2. Shell Programming and Scripting

Run command through html+cgi in bash

Hi everyone, I want to kill process through the web, so I create html page with single bottom that run kill command in shell script with CGI. Here is html code: <td><form METHOD="GET" action="http://IP:port/cgi_bin/script.cgi" > <input type="submit" value= "Submit" > <INPUT name="q"... (7 Replies)
Discussion started by: indeed_1
7 Replies

3. Shell Programming and Scripting

Run SCP in backgroung using Perl CGI

Hi I am Run Perl CGI Script. In which i am running SCP Command. But I want that command to be run into background and exit the script. But Still Web page waiting for Finish the script. I m doing like : system ("scp -r machinename:/path/to/file/for/copy/ /path/for/ destination/directory/ &");... (3 Replies)
Discussion started by: Navrattan Bansa
3 Replies

4. Homework & Coursework Questions

Run Program from Bash CGI-Script

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: This is a problem I am having with my 2 semester senior project. I have a LAMP server running Ubuntu 9.10 with... (8 Replies)
Discussion started by: JMooney5115
8 Replies

5. Web Development

Apache, cgi script run twice when ssl, once when not ssl

I have interesting problem. https:/host/some/x.cgi - this script has run twice when I call this url But http:/host/some/x.cgi work fine, only once. Output is text/plain. If I change output format to the Content-type text/html, then both urls works fine - executed only once. (2 Replies)
Discussion started by: kshji
2 Replies

6. Shell Programming and Scripting

Run system command in perl cgi

Hi guys, got a problem with a perl cgi script over here. I need it to run a system command to get the status of a process. Unfortunately the process is owned by a specific user and only this user can get its status. So i tried running the command from the perl cgi with "su", but then i get the... (12 Replies)
Discussion started by: polki
12 Replies

7. Shell Programming and Scripting

how to run cgi -script on Cygwin ?

All, I would like to run a cgi script in cygwin which i have installed in WinXP. My CYGWIN directory structure is /var/www/ drwxrwx---+ 2 user Users 0 Nov 23 16:24 cgi-bin drwxrwx---+ 3 user Users 0 Oct 22 17:21 htdocs drwxrwx---+ 3 user Users 0 Oct 22 17:22 icons and another... (1 Reply)
Discussion started by: jambesh
1 Replies

8. UNIX for Dummies Questions & Answers

Run ksh script from cgi

Hi, I'm developing a system which requires me to run a ksh script from within a cgi script. What sort of syntax will I need to do this, I'm sure it's simple but can't find out how anywhere! Thanks. (2 Replies)
Discussion started by: hodges
2 Replies

9. Shell Programming and Scripting

CGI passing arrays/hashes to another CGI script

If I have a Perl CGI script (script01), which fills an array(s) with information and outputs a HTML page with a link to another CGI page (script02); is there anyway to pass the array(s) from "script01" to "script02" when the page visitor clicks the link? Hope that makes sense! :) (2 Replies)
Discussion started by: WIntellect
2 Replies

10. Cybersecurity

Run CGI As User..?

Hi, My server is set up so that all cgi scripts are run as user nobody, instead of the user's username. Now I know you can use cgi-wrappers, but someone please give me detailed information, on how to make ALL scripts run on the server to by default, run as the user? And not to run as user... (1 Reply)
Discussion started by: jason6792
1 Replies
Login or Register to Ask a Question