|
|
|
|
||||||
| comp.unix.shell Using and programming the Unix shell. |
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
Hi everyone,
I'm writing a small shell script to act like a cgi program. I have linked this script to a basic site I have developed via a form. I have attached the code below, and would greatly appreciate any pointers to correct glaring mistakes ..The problem: Everything displays as expected until I hit the second if statement. When I uncomment this statement all I get is a blank page, If I comment out this part ( if [ $flag1 -e 1 ]; then ) then I see what should be displayed until this point. The syntax for the if statement seems correct... I can't figure out what stupid mistake is causing this error To recap: When I comment out the part if [ $flag1 -e 1 ]; then, I can see Your URL is www.hfdjhgjghjgh.com and then the flag1 value. As soon as I enable the if part I see a blank page! arrrrgh! please :-) #!/bin/sh wd=$PWD echo "Content-type: text/html" echo echo "<HTML><HEAD><TITLE>My first cgi</Title></HEAD>" echo " <BODY>" echo " <pre>" input=(`echo "$QUERY_STRING"`) #check for ; and | flag=(`echo $input | awk 'BEGIN{flag=0} /;|%3B/{flag=1} /\||%7C/ {flag=2} END{print flag}'`) if [ $flag -e 0 ]; then echo "Malformed URL: invalid characters used" echo "Please check URL" else burl=(`echo $input | awk '{split($0,a,"&");split(a[1],b,"=") rintb[2]}'`); echo "<H3>Your URL is </H3> $burl"; #push this in psql db and check if we know #about it echo "SELECT sitename FROM urltable WHERE sitename = '$burl';" > $wd/query.sql flag1=(`psql -f $wd/query.sql urldb | awk 'BEGIN{a=0} /(0 rows)/ {a=1} END{print a}'`) echo $flag1 if [ $flag1 -e 1 ]; then #PROBLEM IS HERE else #do something fi #return the formatted page fi rm -f $wd/query.sql echo "</BODY>" echo "</HTML>" exit |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
On Mar 14, 5:26 pm, newbiegalore <banerjee.anir...@gmail.com> wrote:
> Hi everyone, > I'm writing a small shell script to act like a cgi > program. I have linked this script to a basic site I have developed > via a form. I have attached the code below, and would greatly > appreciate any pointers to correct glaring mistakes ..> > The problem: Everything displays as expected until I hit the second if > statement. When I uncomment this statement all I get is a blank page, > If I comment out this part ( if [ $flag1 -e 1 ]; then ) then I see > what should be displayed until this point. The syntax for the if > statement seems correct... I can't figure out what stupid mistake is > causing this error > > To recap: When I comment out the part if [ $flag1 -e 1 ]; then, I can > see Your URL iswww.hfdjhgjghjgh.comand then the flag1 value. As soon > as I enable the if part I see a blank page! arrrrgh! please :-) > > #!/bin/sh > > wd=$PWD > > echo "Content-type: text/html" > echo > > echo "<HTML><HEAD><TITLE>My first cgi</Title></HEAD>" > echo " <BODY>" > echo " <pre>" > > input=(`echo "$QUERY_STRING"`) > > #check for ; and | > flag=(`echo $input | awk 'BEGIN{flag=0} /;|%3B/{flag=1} /\||%7C/ > {flag=2} END{print flag}'`) > > if [ $flag -e 0 ]; > then > echo "Malformed URL: invalid characters used" > echo "Please check URL" > else > burl=(`echo $input | awk '{split($0,a,"&");split(a[1],b,"=") rint> b[2]}'`); > echo "<H3>Your URL is </H3> $burl"; > #push this in psql db and check if we know > #about it > echo "SELECT sitename FROM urltable WHERE sitename = '$burl';" > > $wd/query.sql > flag1=(`psql -f $wd/query.sql urldb | awk 'BEGIN{a=0} /(0 rows)/ > {a=1} END{print a}'`) > echo $flag1 > > if [ $flag1 -e 1 ]; then > #PROBLEM IS HERE > else > #do something > fi > #return the formatted page > fi > rm -f $wd/query.sql > echo "</BODY>" > echo "</HTML>" > exit OK things seem to be working!! don't really know why though .. Iremoved all the text/blank spaces near the ifs and then re-wrote the code there.. got my fingers crossed. -A |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
On 2008-03-15, newbiegalore <banerjee.anirban@gmail.com> wrote:
> > > #check for ; and | > flag=(`echo $input | awk 'BEGIN{flag=0} /;|%3B/{flag=1} /\||%7C/ > {flag=2} END{print flag}'`) > > if [ $flag -e 0 ]; I think you men if [ $flag -eq 0 ]; Actually it should be if [ "$flag" -eq 0 ]; Shell variables should be quoted unless you want them to undergo word splitting and filename expansion. |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
On Mar 14, 7:51 pm, Bill Marcum <marcumb...@bellsouth.net> wrote:
> On 2008-03-15, newbiegalore <banerjee.anir...@gmail.com> wrote: > > > > > #check for ; and | > > flag=(`echo $input | awk 'BEGIN{flag=0} /;|%3B/{flag=1} /\||%7C/ > > {flag=2} END{print flag}'`) > > > if [ $flag -e 0 ]; > > I think you men if [ $flag -eq 0 ]; > Actually it should be if [ "$flag" -eq 0 ]; Shell variables should be > quoted unless you want them to undergo word splitting and filename > expansion. Hey thanks man! :-) |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
My test: socket micro_inetd: http://www.acme.com/software/ In a xterm: $ micro_inetd 8888 /tmp/cgi In the browser: http://localhost:8888/ Some changes in your script: $ cat /tmp/cgi #!/bin/sh wd=$PWD #echo "Content-type: text/html" #echo echo -en "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n" echo "<HTML><HEAD><TITLE>My first cgi</Title></HEAD>" echo " <BODY>" echo " <pre>" set -x # for debug only input=(`echo "$QUERY_STRING"`) #check for ; and | flag=(`echo $input | awk 'BEGIN{flag=0} /;|%3B/{flag=1} /\||%7C/ {flag=2} END{print flag}'`) if [ $flag -eq 0 ]; then echo "Malformed URL: invalid characters used" echo "Please check URL" else burl=(`echo $input | awk '{split($0,a,"&"); split(a[1],b,"=") rint b[2]}'`);echo "<H3>Your URL is </H3> $burl"; #push this in psql db and check if we know #about it echo "SELECT sitename FROM urltable WHERE sitename = '$burl';" > $wd/query.$ flag1=(`psql -f $wd/query.sql urldb | awk 'BEGIN{a=0} /(0 rows)/ {a=1} END{$ echo $flag1 if [ $flag1 -eq 1 ]; then echo PROBLEM IS HERE else echo do something fi #return the formatted page fi rm -f $wd/query.sql echo "</BODY>" echo "</HTML>" exit Another example, to play: Basic HTTPD CGI script ############### $ cat /tmp/http #!/bin/bash printf "HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\n\r\n" echo ---------BROWSER: while [ "$REPLY" != $'\r' ];do read -r -t1;[ "$P" ]||P=$REPLY;echo "$REPLY";done echo ---------SERVER noHEADER: echo \$0=$0;echo P=$P P=${P#*/};echo P=$P P=${P% *};echo P=$P P=${P//%20/ };echo P=$P date;ls -l /bin/*sh|grep -- ^- echo ---------END $ micro_inetd 8888 /tmp/http $ lynx "http://localhost:8888/here, my parms" |
|
![]() |
| Outils de la discussion | |
|
|