The simplest shell script--broken


#1

Hi,

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

#!/bin/bash
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?

Thanks,
grimholtz


#2

Have you read the wiki on it?
http://wiki.dreamhost.com/index.php/KB_/Web_Programming/_CGI,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.

-Scott


#3

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 --------
#!/bin/bash
echo "Content-type: text/plain"
echo
set

More good cgi stuff on headers is found here
–rlparker


#4

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?

Thanks!


#5

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.

thanks.


#6

You can also do something like this:

#!/bin/bash
echo 'Content-type: text/html

Hello World!!'

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


#7

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

#!/bin/bash
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.