Programming This 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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
09-07-2011, 02:08 AM
|
#1
|
|
Member
Registered: Nov 2004
Location: the internet
Distribution: trx, #!, slack, gentoo, arch, witch, etc
Posts: 139
Rep:
|
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
|
|
|
|
09-07-2011, 03:07 AM
|
#2
|
|
Senior Member
Registered: Sep 2009
Location: Philly, PA
Distribution: Kubuntu x64, RHEL, Fedora Core, FreeBSD, Windows x64
Posts: 1,061
|
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.
Last edited by sag47; 09-07-2011 at 03:17 AM.
|
|
|
|
09-07-2011, 03:22 AM
|
#3
|
|
Guru
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 6,317
|
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.
|
|
|
|
09-07-2011, 03:27 AM
|
#4
|
|
Senior Member
Registered: Sep 2009
Location: Philly, PA
Distribution: Kubuntu x64, RHEL, Fedora Core, FreeBSD, Windows x64
Posts: 1,061
|
Quote:
Originally Posted by grail
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).
|
|
|
|
09-07-2011, 04:32 AM
|
#5
|
|
Guru
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 6,317
|
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)
|
|
|
|
09-07-2011, 04:55 AM
|
#6
|
|
Member
Registered: Nov 2004
Location: the internet
Distribution: trx, #!, slack, gentoo, arch, witch, etc
Posts: 139
Original Poster
Rep:
|
"i had the boys downstairs construct a prototype, so our discussion here could have a little focus"
Quote:
Originally Posted by sag47
ultimate goal
|
see elaborations bellow.
Quote:
Originally Posted by sag47
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 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: 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?).
|
|
|
|
09-07-2011, 07:02 AM
|
#7
|
|
Guru
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 6,317
|
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
|
|
|
1 members found this post helpful.
|
09-07-2011, 03:59 PM
|
#8
|
|
Member
Registered: Nov 2004
Location: the internet
Distribution: trx, #!, slack, gentoo, arch, witch, etc
Posts: 139
Original Poster
Rep:
|
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! 
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 07:52 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|