我有一小部分代码,由于某些原因,在从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运行时,script...the、html标记以及命令的输出都会显示出来。
内容类型: text/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>发布于 2017-11-18 22:03:55
打印内容的echo命令在发布的代码中缺少结束"。在任何情况下,双引号字符在HTML内容中都很常见,这种打印方式可能会在以后引起麻烦。使用here-document会更容易:
#!/bin/bash
cat <<EOF
Content-type: text/html
... (HTML content here)
EOF另请注意,为了能够运行此脚本,文件必须是可执行的(chmod +x script.sh),并且web服务器必须配置为执行脚本,而不是打印其内容(这是常见的默认设置)。
发布于 2017-11-19 01:26:53
为什么是回声?!
print "Content-type: text/html\n\n";
打印“废话”,记住第二句“你没有它”。
https://stackoverflow.com/questions/47366787
复制相似问题