将环境回显到简单表

时间:2019-04-30 23:50:57

标签: apache environment-variables

我有这个cgi脚本:

echo "Content-type: text/html"
echo ""
echo "<html><head><title>Demo</title></head><body>"
echo "env: $(env)"
echo "Current directory is $(pwd) <br>"
echo "Shell Script name is $0"
echo "</body></html>"

浏览器中的输出字面意思是:

Today is Tue Apr 30 23:49:57 UTC 2019 
env: HTTP_HOST=localhost:8080 CONTEXT_DOCUMENT_ROOT=/usr/local/apache2/cgi-bin/ HTTP_USER_AGENT=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.108 Safari/537.36 SERVER_ADMIN=you@example.com CONTEXT_PREFIX=/cgi-bin/ SERVER_PORT=8080 SERVER_NAME=localhost QUERY_STRING= SCRIPT_FILENAME=/usr/local/apache2/cgi-bin/test.sh PWD=/usr/local/apache2/cgi-bin HTTP_ACCEPT=text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3 REQUEST_METHOD=GET SERVER_SIGNATURE= SCRIPT_NAME=/cgi-bin/test.sh HTTP_CONNECTION=keep-alive REMOTE_PORT=51322 DOCUMENT_ROOT=/usr/local/apache2/htdocs SHLVL=1 SERVER_PROTOCOL=HTTP/1.1 HTTP_COOKIE=JSESSIONID.c1af0e57=node0j2yur9lnchlk1oemfz24op8nu0.node0; JSESSIONID.6b181414=node09a51z5416bi511cxypxbqkiz41.node0; JSESSIONID.c718069a=node016jx557didnmp12hvsc9ozzonr2.node0 REQUEST_URI=/cgi-bin/test.sh HTTP_ACCEPT_LANGUAGE=en-US,en;q=0.9 HTTP_UPGRADE_INSECURE_REQUESTS=1 PATH=/usr/local/apache2/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin SERVER_ADDR=172.17.0.2 GATEWAY_INTERFACE=CGI/1.1 HTTP_ACCEPT_ENCODING=gzip, deflate, br REQUEST_SCHEME=http REMOTE_ADDR=172.17.0.1 SERVER_SOFTWARE=Apache/2.4.39 (Unix) _=/usr/bin/env Current directory is /usr/local/apache2/cgi-bin 
Shell Script name is /usr/local/apache2/cgi-bin/test.sh here we go.

有没有一种方法可以将每个环境变量对回显到新行?

1 个答案:

答案 0 :(得分:0)

不需要表,只需使用<br>,就像这样:

env | sort | while read line; do echo "$line <br>"; done