PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Serveur - Sécurité et techniques > comp.unix.shell > cgi using shell script : unexpected behavior
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

cgi using shell script : unexpected behavior

Réponse
 
LinkBack Outils de la discussion
Vieux 15/03/2008, 01h26   #1
newbiegalore
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut cgi using shell script : unexpected behavior

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

  Réponse avec citation
Vieux 15/03/2008, 02h35   #2
newbiegalore
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: cgi using shell script : unexpected behavior

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 .. I
removed all the text/blank spaces near the ifs and then re-wrote the
code there.. got my fingers crossed.

-A
  Réponse avec citation
Vieux 15/03/2008, 03h51   #3
Bill Marcum
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: cgi using shell script : unexpected behavior

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.
  Réponse avec citation
Vieux 15/03/2008, 04h51   #4
newbiegalore
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: cgi using shell script : unexpected behavior

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! :-)
  Réponse avec citation
Vieux 15/03/2008, 05h40   #5
mop2
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: cgi using shell script : unexpected behavior


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"

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 05h06.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,11647 seconds with 13 queries