LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 01-16-2015, 04:42 PM   #1
CriticalAlert
LQ Newbie
 
Registered: Dec 2014
Posts: 17

Rep: Reputation: 1
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.
 
Old 01-16-2015, 05:10 PM   #2
CriticalAlert
LQ Newbie
 
Registered: Dec 2014
Posts: 17

Original Poster
Rep: Reputation: 1
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.
 
Old 01-16-2015, 05:27 PM   #3
SoftSprocket
Member
 
Registered: Nov 2014
Posts: 399

Rep: Reputation: Disabled
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.
 
Old 01-16-2015, 05:56 PM   #4
CriticalAlert
LQ Newbie
 
Registered: Dec 2014
Posts: 17

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by SoftSprocket View Post
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.
 
Old 01-17-2015, 03:04 AM   #5
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
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)
 
Old 01-17-2015, 01:28 PM   #6
CriticalAlert
LQ Newbie
 
Registered: Dec 2014
Posts: 17

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by NevemTeve View Post
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
 
Old 01-18-2015, 07:59 AM   #7
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Hopefully they don't, put if a haxor created a file called a"b.jpg, you would get a HTML-syntax error.
 
Old 01-18-2015, 02:14 PM   #8
CriticalAlert
LQ Newbie
 
Registered: Dec 2014
Posts: 17

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by NevemTeve View Post
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.
 
Old 01-18-2015, 11:57 PM   #9
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
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//\"/&quot;}"
done
For older (or: more POSIX-like) shells:
Code:
for i in *.jpg; do 
    i2="$(printf '%s' "$i" | sed 's/\"/\&quot;/g')"
    printf '<img src="%s">\n' "$i2"
done

Last edited by NevemTeve; 01-19-2015 at 03:37 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] [IMG] tags: Image gets stretched over to the right, in the blog Aquarius_Girl LQ Suggestions & Feedback 1 02-01-2012 10:25 AM
HTML tags like <time> Mr. Alex Programming 3 01-02-2012 11:31 AM
IMG tags not working despite 'Show Images' being ticked. mattst LQ Suggestions & Feedback 3 07-22-2010 08:19 AM
mutt and html tags cizzi Linux - Software 3 03-30-2008 08:21 PM
the site's img tags could use title and/or alt text akaBeaVis LQ Suggestions & Feedback 5 08-03-2003 03:21 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration