linux shell cgi post,linux下shell处理cgi的方法--post get

本文介绍了如何在Linux Shell环境中实现CGI脚本处理网页表单的GET和POST方法。对于GET方法,通过环境变量QUERY_STRING获取值,并展示了如何解析多个参数。对于POST方法,利用CONTENT_LENGTH读取标准输入来获取数据,并进行了修正后的处理方法。文章提供了详细的示例代码,帮助理解CGI脚本在实际Linux环境中的应用。
摘要由CSDN通过智能技术生成

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的处理存在错误之处,这里纠正出来。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值