【问题标题】:server errors or blank pages with CGI scripts服务器错误或带有 CGI 脚本的空白页
【发布时间】:2017-11-18 13:45:17
【问题描述】:

我有一小段代码,由于某种原因,从http://localhost/cgi-bin/sysinfo.sh 运行时无法执行。我在 CentOS 7 的虚拟机上。我会尽我所能获得帮助。我似乎无法正确理解这些缩进,但它们在 Vi 中是正确的。

#!/bin/bash
echo -e "Content-type: text/html\r\n\n"
echo "
<html>
<head>
     <title>Bash as CGI</title>
</head>
<body>
<h3>General system information for host $(hostname -s).             </h3>
<h4>Memory Info</h4>
<pre> $(free -m) </pre>
<h4>Disk Info:</h4>
<pre> $(df -h) </pre>
<h4>Logged in user</h4>
<pre> $(w) </pre>
<hr>
Information generated on $(date)
</body>
</html>

当我将它作为 shell 脚本运行时……html 标记都会与命令的输出一起显示出来。

内容类型:文本/html

<html>
<head>
    <title>Bash as CGI</title>
</head>
<body>
<h3>General system information for host localhost</h3>
<h4>Memory Info</h4>
<pre>               total        used        free      shared  
buff/cache   available
Mem:           1775         994         137          20         643         
540
Swap:          2047           0        2047 </pre>
<h4>Disk Info:</h4>
<pre> Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   47G  9.7G   38G  21% /
devtmpfs                 873M     0  873M   0% /dev
tmpfs                    888M  4.2M  884M   1% /dev/shm
tmpfs                    888M  9.1M  879M   2% /run
tmpfs                    888M     0  888M   0% /sys/fs/cgroup
/dev/sda1               1014M  235M  780M  24% /boot
tmpfs                    178M  4.0K  178M   1% /run/user/42
tmpfs                    178M   44K  178M   1% /run/user/0 </pre>
<h4>Logged in user</h4>
<pre>  21:33:21 up  2:01,  3 users,  load average: 0.06, 0.04, 0.05
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/0    :0               20:45    1.00s  0.14s  0.00s w
root     :0       :0               20:45   ?xdm?   1:11   0.11s
/usr/libexec/gnome-session-binary --session gnome-classic </pre>
<hr>
Information generated on Sat Nov 18 21:33:21 EST 2017
</body>
</html>

【问题讨论】:

  • 你的网络服务是什么,你是如何配置它来执行.sh脚本的?
  • 您的文件是否包含右双引号(您粘贴的内容没有)?
  • “无法执行”不是对您的问题的非常有用的描述。您究竟看到了什么意外行为?将什么写入 Web 服务器错误日志?
  • 这和 Perl 有什么关系?
  • @simbabque: 所有 CGI 与 Perl 有关系。我很惊讶你不知道:-)

标签: bash perl cgi


【解决方案1】:

打印内容的echo 命令在发布的代码中缺少结束"。 任何状况之下, 双引号字符在 HTML 内容中很常见,这种打印方式很可能会在以后引起头疼。 使用 here-document 会更容易:

#!/bin/bash

cat <<EOF
Content-type: text/html

... (HTML content here)

EOF

还要注意,为了能够运行它, 该文件必须是可执行的(chmod +x script.sh), 并且必须将 Web 服务器配置为执行脚本, 而不是打印其内容,这是一种常见的默认设置。

【讨论】:

    【解决方案2】:

    为什么要回声???!

    打印“内容类型:文本/html\n\n”;

    打印“blah blah”,并记住第二个“你没有它。

    【讨论】:

      猜你喜欢
      • 2016-02-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-30
      • 1970-01-01
      • 2017-07-25
      • 2012-05-05
      • 2015-03-11
      相关资源
      最近更新 更多