[SOLVED] use bash and sed to generate multipage gallery from ls output
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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.
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).
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>'
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)
# 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?).
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.