There exist a number of good resources books, and such, that
can answer this question much better for you than I can, but
here are a couple simple rules to keep in mind.
make sure that the script is executable.
always start out your scripts with a "Content-type: <something>\n\n"
line.
If a script runs in a command-line window, then it should
also work as a cgi.