Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

cgi using shell script : unexpected behavior

411 views
Skip to first unread message

newbiegalore

unread,
Mar 14, 2008, 8:26:52 PM3/14/08
to
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 :D ..

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

newbiegalore

unread,
Mar 14, 2008, 9:35:32 PM3/14/08
to
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 :D ..
>
> 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

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

Bill Marcum

unread,
Mar 14, 2008, 10:51:04 PM3/14/08
to
On 2008-03-15, newbiegalore <banerjee...@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.

newbiegalore

unread,
Mar 14, 2008, 11:51:15 PM3/14/08
to
On Mar 14, 7:51 pm, Bill Marcum <marcumb...@bellsouth.net> wrote:

Hey thanks man! :-)

mop2

unread,
Mar 15, 2008, 12:40:03 AM3/15/08
to

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,"=");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"

0 new messages