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>
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
echo "<html>" > index.html
should just be
echo "<html>"
but when you call the script, call it like this:
my-script > index.html
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":
#!/bin/bash
as the first line. See the Advanced Bash Scripting Guide for details.
Once you do that, you shouldn't need the
grep -v index.html
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 use sed
; it's not necessary. You can make all your changes with awk
:
awk '{print $3, $4, $5, $6, "<a href=" $7 ">" $7 "</a>"}'
should do the trick.
So the engine in your script will consist only of ls
piped into awk
, with echo
giving the output some structure; and when you use the script, redirection will take care of getting it into a file.