LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-15-2010, 07:06 AM   #1
m4rtin
Member
 
Registered: Sep 2007
Posts: 261

Rep: Reputation: 16
argument relations in CGI script


I have made fallowing script for CGI scripting learning purpose:

Code:
#!/bin/sh
echo "Content-type: text/html"
echo

CMD=`echo "$QUERY_STRING" | sed -n 's/^.*cmd=\([^&]*\).*$/\1/p' | sed "s/%20/ /g"`

echo "<html>"
echo "<head><title>Hello CGI</title></head>"
echo "<body>"

function testing {
/bin/pwd
}

if [ $CMD ]
then
  case "$CMD" in
    ifconfig)
      echo "Output of ifconfig :<pre>"
      /sbin/ifconfig
      testing
      echo "</pre>"
      ;;

    uname)
      echo "Output of uname -a :<pre>"
      /bin/uname -a
      echo "</pre>"
      ;;

    *)
      echo "Unknown command $CMD<br>"
      ;;
  esac
fi

echo "Choose which command you want to run"
echo "<form method=get>"
echo "<input type=radio name=cmd value=ifconfig checked> ifconfig <br>"
echo "<input type=radio name=cmd value=uname> uname -a <br>"
echo "<input type=submit>"

echo "</body>"
echo "</html>"
Could somebody explain, what relations are between those lines:

CMD=`echo "$QUERY_STRING" | sed -n 's/^.*cmd=\([^&]*\).*$/\1/p' | sed "s/%20/ /g"`

if [ $CMD ]

case "$CMD" in

echo "<input type=radio name=cmd value=ifconfig checked> ifconfig <br>"
echo "<input type=radio name=cmd value=uname> uname -a <br>"


This script works fine, but actually I quite don't understand it's inner working about those radio buttons Any explanations?
 
Old 02-15-2010, 06:51 PM   #2
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,790

Rep: Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653
The first is obtaining the arguments passed to the cgi

eg. http://localhost/myscript.cgi?cmd=ifconfig

The second is testing to see whether the argument is present, probably not the best way to test this, maybe '-z' to test for zero-length string.

The third is a case statement that compares the argument to several possibilities.

The radio buttons are part of the form that is displayed, which allows the user to easily send arguments to the cgi.

Here's a link so you can brush up on html forms - http://www.w3schools.com/html/html_forms.asp


cheers
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
calling CGI script in CGI script ravi_chobey Programming 3 03-19-2009 12:36 PM
a script to give me the last argument !!! rameshvl Linux - General 18 12-29-2008 01:02 PM
i get an error message running php script inside a cgi script. repolona Linux - Software 0 02-22-2007 09:10 PM
Directory listing - Calling shell script from a CGI script seran Programming 6 08-11-2005 11:08 PM
python cgi script and premature end of script headers Neruocomp Programming 1 07-28-2005 11:43 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:54 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration