在linux中生成一个目录列表作为网络服务器索引页
我用来
python -m SimpleHTTPServer 8080
从 Linux 目录启动网络服务器。我正在尝试编写一个 shell 脚本来生成包含以下信息的 index.html 页面: 文件名(作为超链接)、文件大小、上次修改日期。所以实际上是一个自定义目录列表。
这是当前获取正确信息的脚本,但将所有信息显示在一行上,并且不超链接文件名。谁能引导我走向正确的方向?
TITLE="Latest Logs"
echo "<html>" > index.html
echo "<title>$TITLE</title>" >> index.html
echo "<body>" >> index.html
echo "<h1>$TITLE</h1>" >> index.html
ls -ctgGh | awk '{print $3 " " $4 " " $5 " " $6 " " $7}' \
| grep -v index.html \
| sed 's/[^0-9\s]+[.][^0-9\s]+/<a href="&">&<\/a><br\/>/g' \
>> index.html
echo "</body></html>" >> index.html
所以我要输入 sed 的是:
374,11 月 6,04:03,generate.sh
7.5M,11月6日04:00,管理服务器.log
46M,11 月 6,03:48,run1.log
528K,11 月 4 日,15:03,build.log
我想得到:
375,Nov 6,04:14,<a href=generate.sh>generate.sh</a>
7.5M,Nov 6,04:09,<a href=Admin-Server.log>Admin-Server.log</a>
46M,Nov 6,03:48,<a href=run1.log>run1.log</a>
528K,Nov 4,15:03,<a href=build.log>build.log</a>
I am using
python -m SimpleHTTPServer 8080
to start a webserver from a linux directory. I am trying to write a shell script that generates a index.html page containing the following information :
File Name( as a hyperlink ), File Size, Last Modified Date. So really a custom directory listing.
Here is the script which currently gets the correct info but displays all of it on a single line and doesn't hyperlink the filename. Can anyone direct me into the right direction?
TITLE="Latest Logs"
echo "<html>" > index.html
echo "<title>$TITLE</title>" >> index.html
echo "<body>" >> index.html
echo "<h1>$TITLE</h1>" >> index.html
ls -ctgGh | awk '{print $3 " " $4 " " $5 " " $6 " " $7}' \
| grep -v index.html \
| sed 's/[^0-9\s]+[.][^0-9\s]+/<a href="&">&<\/a><br\/>/g' \
>> index.html
echo "</body></html>" >> index.html
So what i am piping into sed is :
374,Nov 6,04:03,generate.sh
7.5M,Nov 6,04:00,Admin-server.log
46M,Nov 6,03:48,run1.log
528K,Nov 4,15:03,build.log
and i want to get :
375,Nov 6,04:14,<a href=generate.sh>generate.sh</a>
7.5M,Nov 6,04:09,<a href=Admin-Server.log>Admin-Server.log</a>
46M,Nov 6,03:48,<a href=run1.log>run1.log</a>
528K,Nov 4,15:03,<a href=build.log>build.log</a>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您的第一个更改不应该将输出文件名硬编码到脚本中。只需正常输出,到标准输出,调用脚本时,使用重定向。
例如,您的行
应该是,
但是当您调用脚本时,请像这样调用它:
shell 会将输出放入正确的文件中。 (如果您想将输出放入测试文件中,这在测试时也很有帮助。)
假设您使用的是
sh
,您可能应该以“shebang”开始脚本:作为第一个线。有关详细信息,请参阅高级 Bash 脚本指南。
一旦你这样做了,你就不需要
在你的脚本中使用 ,因为它不应该出现在脚本的环境中。
在您的示例输入中,您有逗号,但在现实世界中,它们将是空格。不是问题,只是对问题的更正。
使用
awk
,您可以使用逗号而不是" "
来分隔带有空格的字段。它会更具可读性。并且不要使用sed
;没有必要。您可以使用 awk 进行所有更改:应该可以解决问题。
因此,脚本中的引擎将仅由通过管道传输到
awk
的ls
组成,并使用echo
为输出提供某种结构;当您使用该脚本时,重定向将负责将其放入文件中。I think your first change should be not hard-coding the output filename into the script. Just output normally, to standard output, and when you call the script, use redirection.
For example, your line
should just be
but when you call the script, call it like this:
and the shell will put the output into the right file. (This is helpful when testing, too, if you want to put output into a test file.)
Assuming you're using
sh
, you should probably start your script with a "shebang":as the first line. See the Advanced Bash Scripting Guide for details.
Once you do that, you shouldn't need the
in your script, because it shouldn't be present in the script's environment.
In your example input, you have commas, but in the real world, they would be spaces. Not a problem, just a correction to the question.
With
awk
, you can probably separate the fields with spaces by using commas, not" "
. It would be more readable. And don't usesed
; it's not necessary. You can make all your changes withawk
:should do the trick.
So the engine in your script will consist only of
ls
piped intoawk
, withecho
giving the output some structure; and when you use the script, redirection will take care of getting it into a file.上面的代码生成一个index.html文件,当从浏览器页面调用该文件时,该文件将显示目录中的所有文件。您可以将 IP 地址替换为您的 Web 服务器 IP 地址和端口。
The above code generates an index.html file which when called from a browser page will show up all the files in the directory. You can replace the ip address with your web server ip address and port.