请问如何将 Grep 实现到 CGI 脚本中?
我很难弄清楚如何将 grep 实现到我的 CGI 脚本中。基本上我会收到一个值,例如。 1500 个 HTML 页面。然后,CGI 脚本运行并将 1500 与文本文件进行比较。当它找到 1500 时,它会打印整行并将其显示在网页上。我想要一些关于如何做到这一点的提示和指示。我知道这涉及 grep,但我真的不知道如何将其放入。HTML
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char *data;
long m,n;
printf("%s%c%c\n",
"Content-Type:text/html;charset=iso-8859-1",13,10);
printf("<TITLE>Webpage of Results</TITLE>\n");
printf("<H1>Temperatures</H1>\n");
data = getenv("QUERY_STRING");
传递变量 time=1500。我理解(如果我错了请纠正我)QUERY_STRING 将包含 1500?
I am having difficulty figuring out how to implement grep into my CGI script. Basically I will receive a value of eg. 1500 from a HTML page. The CGI script then runs and compares 1500 to a text file. When it finds 1500 it prints the entire line and displays it on the webpage. I would like some tips and pointers on how to do this please. I understand that this involves grep but I don't really know how to put it in.
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char *data;
long m,n;
printf("%s%c%c\n",
"Content-Type:text/html;charset=iso-8859-1",13,10);
printf("<TITLE>Webpage of Results</TITLE>\n");
printf("<H1>Temperatures</H1>\n");
data = getenv("QUERY_STRING");
The HTML passes the variable time=1500. I understand (correct me if I am wrong) that QUERY_STRING will contain 1500?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
strstr() 函数是 C 语言中 grep 的一些非常简单的模拟
some very straightforward analog of the grep in C is
strstr()
function对于
whatever?query-string
形式的 URL,环境变量QUERY_STRING
包含完整的query-string
- 在您的情况下time=1500
或类似的。您需要记住,可能会传递多个字段/值,并用
&
分隔。通过使用strtok()
您可以检索以下部分查询字符串一一对应,strcmp()
让您将它们与其他字符串进行比较。如果找到正确的令牌,则需要使用文件:
fopen()< /code>
用于打开文件
fgets()
用于检索行strstr()
检查一行是否包含某个子字符串,fclose()
完成后关闭文件
For a URL of the form
whatever?query-string
, the enviroment variableQUERY_STRING
contains the fullquery-string
- in you casetime=1500
or similar.You need to remember that there may be more then one field/value getting passed, seperated by
&
. By usingstrtok()
you can retrieve the parts of the query string one by one,strcmp()
lets you compare them to other strings.If you found the right token, you need to work with a file:
fopen()
is used for opening a filefgets()
is used to retrieve a linestrstr()
to check if a line contains a certain substringfclose()
to close the file when you're done