Search code examples
htmlbashfilelist

Bash script for file listing (html output)


I've got a problem with writing small bash script for making html list of files based on names. I've got files with a naming pattern INFO1 Bla Bla - INFO2 - INFO3.doc, and I want to have someting like this in a output:

<li id="INFO1Blabla">
<h5>INFO1 Bla bla</h5>
<p>INFO2</p>
<a href="files/INFO1 - INFO2 - INFO3.doc">download</a>
</li>

I'm trying to do this using bash + awk, but i've got some problems even when doing testing in a shell, as you see:

$ ls > list.txt
$ for i in 'cat list.txt'; do awk -F "-" '{print $2}' > list2.txt; done

And loop is probably infinitive, cause for 10min its working..

If somebody can help me, I'll be very happy cause I can't use php in this case and I need to generate this on my side using bash..

Cheers guys and take care


Solution

  • for f in *.doc; do ( 
        IFS=-
        set -- $f
        echo "<li><h5>$1</h5><p>$3</p><a href='files/$*'>download</a></li>"
    )
    done
    

    The part in parentheses is executed in a subshell, so changes to IFS variable are localized. A test:

    $ touch 'info1 - inf2 - info 3.doc'
    $ touch 'First name blabla - Second part bla boa - third part, unimportant.doc'
    $ for f in *.doc; do ( 
    >     IFS=-
    >     set -- $f
    >     echo "<li><h5>$1</h5><p>$3</p><a href='files/$*'>download</a></li>"
    > )
    > done
    <li><h5>First name blabla </h5><p> third part, unimportant.doc</p><a href='files/First name blabla - Second part bla boa - third part, unimportant.doc'>download</a></li>
    <li><h5>info1 </h5><p> info 3.doc</p><a href='files/info1 - inf2 - info 3.doc'>download</a></li>