cgi 调用system来调用.sh脚本,如何屏蔽echo 输出?
时间: 2023-06-19 17:10:24 浏览: 68
可以使用以下两种方法来屏蔽echo输出:
1. 重定向输出到/dev/null
在调用.sh脚本时,将echo输出重定向到/dev/null中即可实现屏蔽。示例代码如下:
```
system("/path/to/script.sh >/dev/null 2>&1");
```
其中,`>/dev/null`表示将标准输出重定向到/dev/null,`2>&1`表示将标准错误输出重定向到标准输出。这样就可以将所有输出都屏蔽了。
2. 使用shell命令屏蔽echo输出
在调用.sh脚本时,可以使用shell命令来屏蔽echo输出。示例代码如下:
```
system("sh -c '/path/to/script.sh >/dev/null 2>&1'");
```
其中,`sh -c`表示执行shell命令,`>/dev/null 2>&1`同上。这样也可以将所有输出都屏蔽了。
相关问题
C语言实现调用cgi.h,post实现开发实现登录验证
要实现 C 语言调用 CGI 接口,并通过 POST 方法实现登录验证,可以按照以下步骤进行:
1. 使用 C 语言编写客户端代码,调用 cURL 库进行网络请求。
2. 在客户端代码中,设置请求的 URL,POST 方法,请求头信息等参数。
3. 在请求体中,设置需要提交的表单数据,包括用户名和密码等信息。
4. 发送请求,并等待服务器返回响应。
5. 解析服务器返回的响应,判断登录是否成功。
下面是一个简单的代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>
int main(void)
{
CURL *curl;
CURLcode res;
struct curl_slist *headerlist = NULL;
char *postdata = "username=test&password=123456";
char *url = "http://localhost/cgi-bin/login.cgi";
curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();
if (curl) {
headerlist = curl_slist_append(headerlist, "Content-Type: application/x-www-form-urlencoded");
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postdata);
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, strlen(postdata));
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
res = curl_easy_perform(curl);
if (res != CURLE_OK) {
fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
}
curl_easy_cleanup(curl);
curl_slist_free_all(headerlist);
}
curl_global_cleanup();
return 0;
}
```
在上述代码中,我们使用了 cURL 库来发送 HTTP 请求,并设置了请求的 URL、POST 方法、请求体等参数。同时,我们还定义了一个请求头信息的链表和一个请求体字符串。需要注意的是,上述代码中的“http://localhost/cgi-bin/login.cgi”应改为实际的 CGI 脚本的 URL。同时,需要根据实际情况修改表单数据中的用户名和密码等信息。
linux中怎么将.sh文件结果展示在网页上
要将Linux中一个.sh文件的结果展示在网页上,可以使用CGI(公共网关接口)技术。具体步骤如下:
1. 在Linux服务器上创建一个CGI脚本文件,可以使用Shell或其他编程语言编写。
2. 在CGI脚本中调用.sh文件并获取其输出结果。
3. 将输出结果以HTML格式返回给客户端,即网页。
4. 将CGI脚本文件放置在Web服务器的CGI目录下,并设置CGI脚本文件的执行权限。
5. 在网页中通过表单或超链接等方式调用CGI脚本文件。
例如,使用Shell编写一个CGI脚本文件(假设文件名为test.sh):
```
#!/bin/sh
echo "Content-type: text/html"
echo ""
echo "<html><body>"
echo "<h1>test.sh output</h1>"
echo "<pre>"
./test.sh # 调用.sh文件并获取输出结果
echo "</pre>"
echo "</body></html>"
```
将该脚本文件放置在Web服务器的CGI目录下,并设置执行权限:
```
$ chmod +x test.sh
$ sudo mv test.sh /usr/lib/cgi-bin/ # 假设CGI目录为/usr/lib/cgi-bin/
```
在网页中通过超链接或表单调用该CGI脚本文件,例如:
```
<a href="/cgi-bin/test.sh">View test.sh output</a>
```
当用户点击该链接时,CGI脚本会被调用并将.sh文件的输出结果以HTML格式展示在网页上。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)