The simplest shell script--broken


I’m trying to get bash shell scripts working from the web (not from the shell). I have:

echo 'Content-type: text/html\n\n’
echo ‘hello world\n’

I save this as test.cgi. When I navigate to this URL, I get an HTTP 500 (server) error. What am I doing wrong?


Have you read the wiki on it?,PHP&_Databases#What_is_an_internal_server_error.2C_and_why_doesn.27t_my_CGI_work.3F

My first guess was going to be that you need to chmod 755 your file.


It runs fine from the shell command line, but the error log shows:

[Thu Aug 24 17:08:47 2006] [error] [client xx.xx.xx.xx] malformed header from script. Bad header=hello world: /home/xxxx/wwwdirectoryl/test.cgi

[Thu Aug 24 17:08:47 2006] [error] [client xx.xx.xx.xx] File does not exist: /home/xxxx/wwwdirectoryl/internal_error.html

-----try this one --------
echo "Content-type: text/plain"

More good cgi stuff on headers is found here

It wasn’t a permissions issue. I’ve set the permissions to 775. The error in the log is:

Premature end of script headers: test.cgi.

The wiki you linked to says this errors is caused because the header “aren’t being printed correctly. here is a basic working script:”. However, the basic working script is in perl, and I want to remain in bash for this.

Also, I’m editing this in vi… not ftp’ing so CR/LF shouldn’t be an issue. Any other ideas?


OK, I see suexec requires it to be 755. I’ve done that–still get 500 error code. Help!

edit: I figured it out. \n doesn’t work as a newline in bash. i need to use “echo” on its own line to get the required CR after headers are complete.


You can also do something like this:

echo 'Content-type: text/html

Hello World!!'

Check out Gordaen’s Knowledge, the blog, and the MR2 page.

you want to use the “-e” option on the command-line, like so:

echo -e 'Content-type: text/html\n\n’
echo -e ‘hello world\n’

otherwise, the “\n” is displayed literally and not as an actual newline character.