LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Code help create img html tags with printf (https://www.linuxquestions.org/questions/programming-9/code-help-create-img-html-tags-with-printf-4175531256/)

CriticalAlert 01-16-2015 04:42 PM

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

CriticalAlert 01-16-2015 05:10 PM

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.

SoftSprocket 01-16-2015 05:27 PM

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.

CriticalAlert 01-16-2015 05:56 PM

Quote:

Originally Posted by SoftSprocket (Post 5302121)
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

NevemTeve 01-17-2015 03:04 AM

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)

CriticalAlert 01-17-2015 01:28 PM

Quote:

Originally Posted by NevemTeve (Post 5302278)
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

NevemTeve 01-18-2015 07:59 AM

Hopefully they don't, put if a haxor created a file called a"b.jpg, you would get a HTML-syntax error.

CriticalAlert 01-18-2015 02:14 PM

Quote:

Originally Posted by NevemTeve (Post 5302869)
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/

NevemTeve 01-18-2015 11:57 PM

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



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