bash 如何在 Apache2 CGI 中捕获参数的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2041494/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to catch value of parameter in Apache2 CGI
提问by Nyambaa
I have a little apache2 CGI application on the Ubuntu. The CGI handler is bash shell script.
My client application is search.html:
我在 Ubuntu 上有一个小的 apache2 CGI 应用程序。CGI 处理程序是 bash shell 脚本。
我的客户端应用程序是search.html:
<html>
<body>
<form action="/cgi-bin/search.sh" method="post">
<input type="text" name="searchKey" size="10"></input>
<input type=SUBMIT value="search">
<form>
</body>
</html>
firstly, I just want to catch value of "searchKey" parameter in server side. I tried like following, but displaying nothing.
search.shis:
首先,我只想在服务器端捕获“searchKey”参数的值。我试过喜欢以下,但什么也没显示。
search.sh是:
#!/bin/bash
echo Content-type:text/plain
echo ""
echo $SEARCHKEY
Guys, can you tell me how to catch value of the parameter in the server side?
UPDATE
thank you for all answers.I understood that to get a value of post request need to read data from STDIN.
i tried as Ithcy suggest like following
伙计们,你能告诉我如何在服务器端捕获参数的值吗?
更新
感谢您的所有回答。我知道要获得 post 请求的值需要从 STDIN 读取数据。
我按照 Ithcy 的建议尝试如下
#!/bin/bash
echo post=$(</dev/stdin)
echo 'content length:'$CONTENT_LENGTH
echo 'content:'$post
it was displaying only that:
它只显示:
content length:30
content:
why is content nothing? do i need to do more configure on Apache server to read post data? Thanks
为什么内容什么都没有?我需要在 Apache 服务器上做更多配置来读取发布数据吗?谢谢
采纳答案by glomad
POSTs will come through STDIN.
POST 将通过 STDIN。
#!/bin/bash
POST=$(</dev/stdin)
echo $POST
But you really should look at using perl (or python, PHP, etc) if you can, as Glenn Hymanman suggests.
但是如果可以的话,你真的应该考虑使用 perl(或 python、PHP 等),正如格伦Hyman曼所建议的那样。
回答by Andrew
Sorry no one answered your question all these months. This works:
抱歉,这几个月没有人回答你的问题。这有效:
#!/bin/bash
echo
echo post=$(</dev/stdin)
echo 'content length:'$CONTENT_LENGTH
echo 'content:'$post
You must insert a blank line after /bin/bash (if not echo, printf "\n"will do)
您必须在 /bin/bash 之后插入一个空行(如果不是 echo,printf "\n"就可以了)
回答by Emil Vikstr?m
The whole querystring is represented in the $QUERY_STRINGvariable. You can see this by running envwithout arguments in your shell script.
整个查询字符串在$QUERY_STRING变量中表示。您可以通过env在 shell 脚本中不带参数运行来查看这一点。
Example for getting only the searchKey value:
仅获取 searchKey 值的示例:
echo $QUERY_STRING | sed 's/searchKey\=\([^&]\+\).*//'
Update: I'm sorry, this only applies if you are using GET to post your form. I didn't read the details =/
更新:对不起,这仅适用于您使用 GET 发布表单的情况。我没看细节=/
If you really need to read POSTs, this page may help you: http://digitalmechanic.wordpress.com/2008/02/21/handling-post-data-in-bash-cgi-scripts/I didn't get it to work, though.
如果你真的需要阅读帖子,这个页面可能对你有帮助:http: //digitalmechanic.wordpress.com/2008/02/21/handling-post-data-in-bash-cgi-scripts/我没看懂工作,不过。
回答by glenn Hymanman
This is good documentation about the CGI protocol: http://hoohoo.ncsa.illinois.edu/cgi/
这是关于 CGI 协议的很好的文档:http: //hoohoo.ncsa.illinois.edu/cgi/
I'd suggest you consider using a language (such as Perl) with a good CGI library so you don't have to reinvent a wheel that's been perfected years ago.
我建议您考虑使用具有良好 CGI 库的语言(例如 Perl),这样您就不必重新发明多年前完善的轮子。
回答by Klaus Byskov Pedersen
Try
尝试
echo
instead of
代替
echo $SEARCHKEY
回答by thomas
Try this script to list content of your input:
试试这个脚本来列出你输入的内容:
#!/bin/bash
echo 'content length:'$CONTENT_LENGTH
read StringInBox
echo $StringInBox

