web server获取网页上HTML的DOM对象信息发送来的信息,主要有get和post方法。
介绍原理的文章很多,但介绍实际应用到linux的shell中的文章很少。
1、get方法
①表单源码如下,有几个HTML对象作为传值对象。
CGI Test
Text field
Radio button
1
2
3
②cgi get脚本,使用环境变量QUERY_STRING传值。
#!/bin/sh
echo "Content-type: text/html"
echo ""
echo "Sample CGI Output"
echo ""
echo ""
#echo "Environment variables"
#echo ""
env
#echo ""
#echo "========================================================="
#echo ""
#echo "Form variables :"
#echo ""
str=$QUERY_STRING
echo $str
# [ "${str%&}" = "$str" ] && str="$str&"
# pair=${str%%&*}
# var=${pair%=*}
# val=${pair#*=}
pair=${str##*&}
var=${pair%=*}
val=${pair#*=}
str=${str#*&}
echo "$pair"
echo "$var"
echo "$val"
echo "$str"
echo ""
#echo $val
if [ $val = "lighton" ];then
echo "select to open light"
fi
if [ $val = "lightoff" ];then
echo "select to close light"
fi
if [ $val = "rolling" ];then
echo "select to rolling light"
fi
echo ""
echo ""
echo ""
2、post方法
①表单源码
CGI Test
Text field
Radio button
1
2
3
Some text
②post传值时,使用环境变量CONTENT_LENGTH表示传输数据长度。
而实际内容需要从标准输入设备接收,这里使用命令“read pos -n CONTENT_LENGTH”,注意,接收个数需做限制。
#!/bin/sh
echo "Content-type: text/html"
echo ""
echo "Sample CGI Output"
echo ""
echo ""
echo "Environment variables"
echo ""
env
echo ""
echo "========================================================="
echo ""
echo "Form variables :"
echo ""
#echo $CONTENT_LENGTH
read pos -n CONTENT_LENGTH
#echo $pos
tmp=${pos%%&*}
#echo $tmp
tmp=${tmp#*=}
echo "Text_Field=$tmp"
tmpcur=${pos#*&}
#echo $tmpcur
tmp=${tmpcur%%&*}
tmp=${tmp#*=}
echo "Radio_Button=$tmp"
tmpcur=${tmpcur#*&}
tmp=${tmpcur%%&*}
tmp=${tmp#*=}
echo "Text_Area=$tmp"
echo ""
echo ""
本文参考http://blog.chinaunix.net/uid-21025382-id-168762.html,非常感谢,但原作对于post的处理存在错误之处,这里纠正出来。