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 help :-)
#!/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,"=");print
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 :D .. I
removed all the text/blank spaces near the ifs and then re-wrote the
code there.. got my fingers crossed.
-A
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.
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,"=");print 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"