请问如何将 Grep 实现到 CGI 脚本中?

发布于 2024-08-31 10:29:59 字数 558 浏览 0 评论 0原文

我很难弄清楚如何将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

白鸥掠海 2024-09-07 10:29:59

strstr() 函数是 C 语言中 grep 的一些非常简单的模拟

some very straightforward analog of the grep in C is strstr() function

┊风居住的梦幻卍 2024-09-07 10:29:59

对于 whatever?query-string 形式的 URL,环境变量 QUERY_STRING 包含完整的 query-string - 在您的情况下 time=1500 或类似的。

您需要记住,可能会传递多个字段/值,并用 & 分隔。通过使用 strtok() 您可以检索以下部分查询字符串一一对应, strcmp() 让您将它们与其他字符串进行比较。

如果找到正确的令牌,则需要使用文件:

For a URL of the form whatever?query-string, the enviroment variable QUERY_STRING contains the full query-string - in you case time=1500 or similar.

You need to remember that there may be more then one field/value getting passed, seperated by &. By using strtok() 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 file
  • fgets() is used to retrieve a line
  • use strstr() to check if a line contains a certain substring
  • use fclose() to close the file when you're done
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文