LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   use bash and sed to generate multipage gallery from ls output (http://www.linuxquestions.org/questions/programming-9/use-bash-and-sed-to-generate-multipage-gallery-from-ls-output-901538/)

Siljrath 09-07-2011 02:08 AM

use bash and sed to generate multipage gallery from ls output
 
ok... i think i've reached the point where i'm stuck... time to ask. :/

i have a template html file, which i want to make a copy of for every file in the working dir.

the template has the word "filename" in it, where it needs to be replaced with the file's name.

i have the basics of a sed command that i'm confident enough will take the template, output to a new file with the appropriate filename, and replace all instances of "filename", with the required filename, currently just a placeholder variable though.

something like this:
Code:

sed s/filename/$filename/g template.html $filename.html
what i'm struggling with, is how to get the output of ls to be used, so it creates an html for/from each.

i had earlier considered starting the whole script with:

Code:

ls . > qalist && sed -i s/^/$/ qalist
but then realised i didnt have a clue where i was going from there.

my earlier version of this script generated only a single html file from the output of ls. i'm completely stumped how to get this new version to create a separate html page for each image.

eventual goal, it will also populate each page with a forward and backward button... but that's for another day.

how can i use the output of ls, to create/touch a (html)file for each (image)file listed...?

or is there some smarter simpler method of achieving my goal?

link to original single-page version of the gallery generator.
http://www.wastedartist.com/scripts/...e/qalator.html

sag47 09-07-2011 03:07 AM

Before I go about giving you an answer to your question I would like to ask you what you're ultimate goal for this is? If it's a class project and you're restricted to those tools then I'll certainly help you. If this is for a personal project then there's a lot easier ways of doing this such as using php or another server side language like python.

Anyway I'll wait and see what you say and then answer based on that.

Also if this is just an obsession for learning post some more information such as the contents of your template (in BB code tags). From there I think it would be easier to grasp the situation. Try to be as verbose as you can providing more information.

grail 09-07-2011 03:22 AM

Well the first suggestion is do not use ls ... reasons found here

The simplest way forward would be to use a simple loop and have your sed act on that.

sag47 09-07-2011 03:27 AM

Quote:

Originally Posted by grail (Post 4463681)
Well the first suggestion is do not use ls ... reasons found here

The simplest way forward would be to use a simple loop and have your sed act on that.

The method used by the original author doesn't encounter that problem because they're using the -m option in ls. I'm not saying it's elegant... just that it's not an issue (there's way better ways to do it). Such as sed with an inline option (or using bash for loops like you say).

grail 09-07-2011 04:32 AM

Quote:

The method used by the original author doesn't encounter that problem because they're using the -m option in ls.
This might be true for the original script, but he is not using -m in the example above.

May I also say that the original code on the link you provided is unnecessarily over the top. I actually see no need for either ls or sed at all.
Example:
Code:

#!/bin/bash

exec >new.html

echo '<html><body>'

for f in Pictures/email/*.jpg
do
    echo -e "\t<img src=\"$f\"><br>"
done

echo '<\body><\html>'

This created new.html which looks like:
Code:

<html><body>
        <img src="Pictures/email/drunkovision_01.jpg"><br>
        <img src="Pictures/email/drunkovision_02.jpg"><br>
        <img src="Pictures/email/drunkovision_03.jpg"><br>
---snip---
        <img src="Pictures/email/drunkovision_40.jpg"><br>
<\body><\html>

So is the idea you want now to create a single html file per picture? (Sorry I couldn't quite follow what your new requirement is)

Siljrath 09-07-2011 04:55 AM

"i had the boys downstairs construct a prototype, so our discussion here could have a little focus"
 
Quote:

Originally Posted by sag47 (Post 4463677)
ultimate goal

see elaborations bellow.
Quote:

Originally Posted by sag47 (Post 4463677)
restricted to those tools

a self imposed restriction to tools commonly found in linux command line (the more universally available by default, the better).
Quote:

Originally Posted by sag47 (Post 4463677)
post some more information

sorry, i do go bounding along like tim robbin's character in the hudsucker proxy sometimes ("y'know, for kids").

qalator2 question clarification:
~ example code is a hasty trimmed down version to help brevity and clarity~

consider the following...
dir contains:
Code:

                1.gif
                2.gif
                3.gif

template html contains:
Code:

                <html><meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
                <style type="text/css">
                html, body {
                width: 100%; height: 100%; margin:0px;
                }<body
                style="color: rgb(255, 255, 255); background-color: rgb(0, 0, 0); background-image: url(filename);"
                link="#ff0000" vlink="#0000ff" alink="#00ff00">
                filename
                </body></html>

with these^ conditions, running the script in the directory produces:
dir contains:
Code:

                1.gif
                2.gif
                3.gif
                1.html
                2.html
                3.html

1.html contains:
Code:

                <html><meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
                <style type="text/css">
                html, body {
                width: 100%; height: 100%; margin:0px;
                }<body
                style="color: rgb(255, 255, 255); background-color: rgb(0, 0, 0); background-image: url(1.gif);"
                link="#ff0000" vlink="#0000ff" alink="#00ff00">
                1.gif
                </body></html>

2.html contains:
Code:

                <html><meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
                <style type="text/css">
                html, body {
                width: 100%; height: 100%; margin:0px;
                }<body
                style="color: rgb(255, 255, 255); background-color: rgb(0, 0, 0); background-image: url(2.gif);"
                link="#ff0000" vlink="#0000ff" alink="#00ff00">
                2.gif
                </body></html>

3.html contains:
Code:

                <html><meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
                <style type="text/css">
                html, body {
                width: 100%; height: 100%; margin:0px;
                }<body
                style="color: rgb(255, 255, 255); background-color: rgb(0, 0, 0); background-image: url(3.gif);"
                link="#ff0000" vlink="#0000ff" alink="#00ff00">
                3.gif
                </body></html>

note the two locations where it said "filename" in the template html file has been changed to the name of the files in the dir, in a correspondingly named html file for each.




trying to think it through, clearly and concisely, this following code is the jist (though no doubt bork code, and not the best way to go about it)
Code:

cp *.gif *.html
echo templatehtmlcodehere > *.html
sed -i s/filename/theactualfilesveryownfilename(minuslast5char)/g *.html

if you catch my drift?

to elaborate:
Code:

# every image is given it's own html file that it will use to be displayed in, using the same name (yes i know cp is not resource savvy choice here, i'm just conveying the jist)
cp *.gif *.html

# following on from this daft notion of using cp... we of course, need these files to contain the html code, not the image code.  again using "*" to signify all
echo templatehtmlcodehere > *.html

# so since we've already got the files named appropriately and populated with the template html, we just need to swap the placeholder text, for the corresponding image name in all the html files.
sed -i s/filename/theactualfilesveryownfilename(minuslast5char)/g *.html

#not only is it likely inelegant to those who know better, it's also less than ideal, as this method involves including the html template code in the script itself, rather than getting it from a template file.


again, of course, this is not the smart way to go about it. i'm just using it as a rough example to get the jist of the sort of brevity and simplicity of code, and the outcome i'm aiming for.

(as you no doubt can tell) my scripting skills and knowledge arent that vast yet, and so it's a bit like trying to paint a Rembrandt when all one has in one's toolbox is a hammer.

[edit]just seen those other posts, and your suggestions grail, thnx, i'll post this and have a look, if i dont fall asleep first (why do i always get the urge to script stuff several hours after i should be sleeping?).

grail 09-07-2011 07:02 AM

Assuming I understand correctly:
Code:

for f in *.gif
do
    html_file=${f%.gif}.html
    sed "s/filename/$f/g" template.html > $html_file
done


Siljrath 09-07-2011 03:59 PM

woke up this evening and found myself elated.
 
words cannot express the joy i felt upon seeing that. it was like the heavens parted and started playing the most holy victorious revelation glory music.

sooo clean and concise.

thnx (holy) grail.

i had only just barely learned the likes of while, for, do etc. not enough to be able to think and fluently code with loops yet, but enough to be able to read that and get it instantly.

assuming i do indeed correctly understand:
Code:

for f in *.gif
do
    html_file=${f%.gif}.html
    sed "s/filename/$f/g" template.html > $html_file
done

this thread most assuredly is getting marked solved. :)

grail, (again) THANKYOU! :) :) :)


All times are GMT -5. The time now is 08:08 AM.