[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.
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.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
Distribution: Devuan, while feeble. Bedrock, when awesome.
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:
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:
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).
# 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.
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?).