2017-11-18 209 views
0

我有这么一小撮代码,出于某种原因,从http://localhost/cgi-bin/sysinfo.sh运行时无法执行。我在CentOS 7的虚拟机上,我会尽我所能的帮助。我似乎无法得到这些指示,但是他们在Vi中是正确的。服务器错误或空白页与CGI脚本

#!/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    [email protected] 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> 
+0

什么是您的网络服务?你如何配置它执行'.sh'脚本? – janos

+0

请问您的文件包含右双引用(你粘贴的东西没有) –

+0

“不能执行”对于你的问题并不是一个非常有用的描述你究竟看到了什么意外的行为?写到Web服务器错误日志中的内容 –

回答

2

echo的命令,打印内容中缺少张贴代码闭合"。 无论如何, 双引号字符在HTML内容中非常常见,这种打印方式可能会在以后引起头痛。 它会更容易使用此文档:

#!/bin/bash 

cat <<EOF 
Content-type: text/html 

... (HTML content here) 

EOF 

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

0

为什么回应???!

print“Content-type:text/html \ n \ n”;

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