Jay Winfrey
Jay Winfrey

Reputation: 11

server errors or blank pages with CGI scripts

I have this tiny bit of code, that for some reason, is not able to execute when run from http://localhost/cgi-bin/sysinfo.sh. I'm on a VM of CentOS 7. I'll take all the help I can get. I can’t seem to get these indenions right but they are correct in 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>

When I run it as a shell script...the html tags all show up along with the output of the commands.

Content-type: 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>

Upvotes: 0

Views: 265

Answers (2)

masterlir
masterlir

Reputation: 21

Why echo???!

print "Content-type: text/html\n\n";

print "blah blah", and remember of second " you dont have it.

Upvotes: 0

janos
janos

Reputation: 124656

The echo command that prints the content is missing a closing " in the posted code. In any case, the double-quote character is very common in HTML content and this way of printing is likely to cause headaches later. It will be easier to use a here-document:

#!/bin/bash

cat <<EOF
Content-type: text/html

... (HTML content here)

EOF

Note also that to be able to run this, the file must be executable (chmod +x script.sh), and the web server must be configured to execute the script, instead of printing its content, which is a common default.

Upvotes: 2

Related Questions