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.
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.
01-16-2015, 04:42 PM
#1
LQ Newbie
Registered: Dec 2014
Posts: 17
Rep:
Code help create img html tags with printf
Hello LQ fans
I need help in correcting the proper syntax for printf to create basic img tags.
I have five images and I want printf to create img src tags for each one of them. I want it to look like this.
<img src="image1.jpg">
<img src="image2.jpg">
<img src="image3.jpg">
<img src="image4.jpg">
<img src="image5.jpg">
Here is the code I am struggling with.
Code:
for i in *.jpg; do echo printf \<img src="%s>\n" "$i"; done
Below are the results.
Quote:
printf <img src=%s>\n image1.jpg
printf <img src=%s>\n image2.jpg
printf <img src=%s>\n image3.jpg
printf <img src=%s>\n image4.jpg
printf <img src=%s>\n image5.jpg
As you can see, the img src tag code is incorrect.
I appreciate any help with this code. Thanks
Last edited by CriticalAlert; 01-16-2015 at 05:06 PM .
01-16-2015, 05:10 PM
#2
LQ Newbie
Registered: Dec 2014
Posts: 17
Original Poster
Rep:
I solved it by using this code after experimenting.
Code:
for i in *.jpg; do echo "<img src=\"$i\">"; done
<img src="image1.jpg">
<img src="image2.jpg">
<img src="image3.jpg">
<img src="image4.jpg">
<img src="image5.jpg">
However, I'm new to the printf command. I still would like to know if printf can do what I wanted in my first post.
Last edited by CriticalAlert; 01-16-2015 at 05:11 PM .
01-16-2015, 05:27 PM
#3
Member
Registered: Nov 2014
Posts: 399
Rep:
The code in your printf was somewhat mixed up.
Code:
for i in *.jpg; do printf "<img src=%s>\n" "$i"; done
Notice I removed the echo and shifted the quote. You were mixing an echo with a printf.
01-16-2015, 05:56 PM
#4
LQ Newbie
Registered: Dec 2014
Posts: 17
Original Poster
Rep:
Quote:
Originally Posted by
SoftSprocket
The code in your printf was somewhat mixed up.
Code:
for i in *.jpg; do printf "<img src=%s>\n" "$i"; done
Notice I removed the echo and shifted the quote. You were mixing an echo with a printf.
Thanks for the correction of my syntax. I ran you code suggestion and I got this.
Quote:
<img src=image1.jpg>
<img src=image2.jpg>
<img src=image3.jpg>
<img src=image4.jpg>
<img src=image5.jpg>
There are no quote marks around the image file names. I modified the code you give me with the escape character '\' for the image file names.
Code:
for i in *.jpg; do printf "<img src=\" %s\" >\n" "$i"; done
Quote:
<img src="image1.jpg">
<img src="image2.jpg">
<img src="image3.jpg">
<img src="image4.jpg">
<img src="image5.jpg">
Again, thanks SoftSprocket
+1
Last edited by CriticalAlert; 01-16-2015 at 06:00 PM .
01-17-2015, 03:04 AM
#5
Senior Member
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
or even:
Code:
for i in *.jpg; do printf '<img src="%s">\n' "$i"; done
it wont work tough if your files' names contain "double quotes" (well, they shouldn't)
01-17-2015, 01:28 PM
#6
LQ Newbie
Registered: Dec 2014
Posts: 17
Original Poster
Rep:
Quote:
Originally Posted by
NevemTeve
or even:
Code:
for i in *.jpg; do printf '<img src="%s">\n' "$i"; done
it wont work tough if your files' names contain "double quotes" (well, they shouldn't)
my image files don't have double quotes as part of it's name. The double quotes are for html syntax purposes.
Thanks
01-18-2015, 07:59 AM
#7
Senior Member
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Hopefully they don't, put if a haxor created a file called a"b.jpg , you would get a HTML-syntax error.
01-18-2015, 02:14 PM
#8
LQ Newbie
Registered: Dec 2014
Posts: 17
Original Poster
Rep:
Quote:
Originally Posted by
NevemTeve
Hopefully they don't, put if a haxor created a file called a"b.jpg , you would get a HTML-syntax error.
As you cam see from this website tutorial, the double quotes are used with the img tag to load an image.
http://www.littlewebhut.com/html/img_tag/
Last edited by CriticalAlert; 01-18-2015 at 02:19 PM .
01-18-2015, 11:57 PM
#9
Senior Member
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
True, but unrelated. I'll try again. Say you have these four files:
Code:
a.b.jpg
a_b.jpg
a=b.jpg
a"b.jpg
the program will give invalid HTML for the last file
Edit: this works in newer shells:
Code:
for i in *.jpg; do
printf '<img src="%s">\n' "${i//\"/"}"
done
For older (or: more POSIX-like) shells:
Code:
for i in *.jpg; do
i2="$(printf '%s' "$i" | sed 's/\"/\"/g')"
printf '<img src="%s">\n' "$i2"
done
Last edited by NevemTeve; 01-19-2015 at 03:37 AM .
All times are GMT -5. The time now is 11:11 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