同一个cgi-bin文件夹下的两个测试程序,一个可以运行,另一个不能运行?

同一个cgi-bin文件夹下的两个测试程序,一个可以运行,另一个不能运行?

test.cgi的源码:

#!/bin/sh


# disable filename globbing

set -f

echo "Content-type: text/plain; charset=iso-8859-1"
echo

echo CGI/1.0 test script report:
echo

echo argc is $#. argv is "$*".

echo

echo SERVER_SOFTWARE = $SERVER_SOFTWARE
echo SERVER_NAME = $SERVER_NAME
echo GATEWAY_INTERFACE = $GATEWAY_INTERFACE
echo SERVER_PROTOCOL = $SERVER_PROTOCOL
echo SERVER_PORT = $SERVER_PORT
echo REQUEST_METHOD = $REQUEST_METHOD
echo HTTP_ACCEPT = "$HTTP_ACCEPT"
echo PATH_INFO = "$PATH_INFO"
echo PATH_TRANSLATED = "$PATH_TRANSLATED"
echo SCRIPT_NAME = "$SCRIPT_NAME"
echo QUERY_STRING = "$QUERY_STRING"
echo REMOTE_HOST = $REMOTE_HOST
echo REMOTE_ADDR = $REMOTE_ADDR
echo REMOTE_USER = $REMOTE_USER
echo AUTH_TYPE = $AUTH_TYPE
echo CONTENT_TYPE = $CONTENT_TYPE
echo CONTENT_LENGTH = $CONTENT_LENGTH


可以在浏览器中正常运行。

但是另一个程序printenv:

#!/bin/sh

#

#  printenv -- demo CGI program which just prints its environment

#


print "Content-type: text/plain; charset=iso-8859-1\n\n";
foreach $var (sort(keys(%ENV))) {
    $val = $ENV{$var};
    $val =~ s|\n|\\n|g;
    $val =~ s|"|\\"|g;
    print "${var}=\"${val}\"\n";
}


单独在命令行状态下运行printenv正常,在浏览器中就报告500错误,日志中显示:  Premature end of script headers: printenv。

服务器装rh9, Apache/2.0.54 (Unix) DAV/2 PHP/4.4.0
/bin/sh 支持 foreach 吗?
把#!/bin/sh换成perl路径