Click to See Complete Forum and Search --> : printing the ls output to an HTML file
Davno
07-15-2007, 03:10 AM
Hi, I would like to have a script that would write the output of the ls command to an HTML files, it would even be better if it showed the time in the html file when i run it. I would then make it a cron job to back up that list at specific time. :)
Calipso
07-15-2007, 06:22 PM
well its not pretty but something as simple as this would do it:
#!/bin/bash
date >> ls.html
echo "<br>" >> ls.html
for i in $( ls ); do
#echo item: $i
echo $i "<br>" >> ls.html
done
Davno
07-15-2007, 11:47 PM
Thank you for your reply, it seem to work except that the .html result shows one word per line, like this:
Albinoni
Tomasso
Concerto
Opus
7
-
Allegro.ogg
These files originaly have empty space in the title. Is there a way to format the .html file in the script to disregard (just one empty space) so it would and up like this:
Albinoni Tomasso Concerto Opus 7 - Allegro.ogg
:confused:
bwkaz
07-16-2007, 06:22 PM
Sure: get rid of the Useless Use Of Backticks (TM), and then put the filename in double quotes. :p
#!/bin/bash
date >> ls.html
echo "<br>" >> ls.html
for i in * ; do
#echo item: $i
echo "$i" "<br>" >> ls.html
done
Davno
07-17-2007, 09:30 AM
Hi Thank you both, the script works nice now.
But when i was running the script instead of overwriting with a new stuff.html file, it was just adding to the same file. So i added something like this to overwrite with a new file every time i run the script.
[ -d /home/normand/temp/stuff.html ] || rm /home/normand/temp/stuff.html It work but i suspect it's not the proper clean way to overwrite a file if it exist.
Any suggestions ?
Thanks
Calipso
07-17-2007, 03:56 PM
sure, change the line:
date >> ls.html
to
date > ls.html
This way the file will be overwritten when the date commands outoput gets written and then the output of the ls command gets added to the bottom of this file.
JRefL5
07-17-2007, 03:59 PM
try changing
date >>ls.html
to
date >ls.html
the single ">" will redirect at the start of the file while ">>" appends to an existing file
Icarus
07-17-2007, 10:23 PM
Sure: get rid of the Useless Use Of Backticks (TM), and then put the filename in double quotes. :p
What, no link this time? You're getting lazy bwkaz ;)
bwkaz
07-18-2007, 07:24 PM
:)
Oh, OK, fine. See here:
http://partmaps.org/era/unix/award.html#backticks
and here:
http://partmaps.org/era/unix/award-example-backticks.html
:p
(On a side note, it seems that this page has moved since the last time I posted it. Hmm.)