LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 08-29-2014, 07:42 PM   #1
Dafydd
Member
 
Registered: Oct 2008
Posts: 344

Rep: Reputation: 29
Remove extension from image


I would like to strip the extension from the end of the image name for use elsewhere in an html file. There a very large number of pictures and I want to use this in a scrip that will also write the HTML code.

This code snippet dose not perform as I understand the example on Stack Overflow.
Code:
 http://stackoverflow.com/questions/125281/how-do-i-remove-the-file-suffix-and-path-portion-from-a-path-string-in-bash
Two days I've read the internet and tried differenet things, nothing has worked. Can someone help?

Thanks
Dave

Code Snippet:
Code:
#! /bin/bash
for image_name in *.jpeg *.jpg *.JPG *.gif *.GIF *.png *.PNG; 
	do
			echo "$image_name"; ##trouble shoot
		image_type=${image_name%.}
			echo "$image_type"; ##trouble shoot
	done;
This is the screen output I get:
Quote:
dave@dave-EL1600:~/projects/web-gallery$ ./looptest.sh
*.jpeg
*.jpeg
View From Above.jpg
View From Above.jpg
Virgil1.jpg
Virgil1.jpg
wading.jpg
wading.jpg
WaterColor.jpg
WaterColor.jpg
WCR_9871.jpg
WCR_9871.jpg
WCR_9878.jpg
WCR_9878.jpg
*.JPG
*.JPG
*.gif
*.gif
*.GIF
*.GIF
*.png
*.png
*.PNG
*.PNG
dave@dave-EL1600:~/projects/web-gallery$
 
Old 08-29-2014, 08:02 PM   #2
jefro
Moderator
 
Registered: Mar 2008
Posts: 21,980

Rep: Reputation: 3624Reputation: 3624Reputation: 3624Reputation: 3624Reputation: 3624Reputation: 3624Reputation: 3624Reputation: 3624Reputation: 3624Reputation: 3624Reputation: 3624
I'm sure there are a million ways.

Might look at this while you are at it.

Your script appears to have added files instead of removing them.
 
Old 08-29-2014, 09:02 PM   #3
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,126

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
Quote:
Originally Posted by Dafydd View Post
This code snippet dose not perform as I understand the example on Stack Overflow.
That's because you transcribed it incorrectly.
Code:
		image_type=${image_name%.*}
 
Old 08-30-2014, 02:31 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
You may also wish to look into the following as an item to place in your script:
Code:
shopt -s nullglob
 
1 members found this post helpful.
Old 08-31-2014, 04:53 PM   #5
Dafydd
Member
 
Registered: Oct 2008
Posts: 344

Original Poster
Rep: Reputation: 29
Quote:
Originally Posted by syg00 View Post
That's because you transcribed it incorrectly.
Code:
		image_type=${image_name%.*}

This solved my problem. I feel embarrested for staring at that for hours and not noticing I typoed it.

Thank you.
 
Old 09-10-2014, 02:54 AM   #6
peterscot423
LQ Newbie
 
Registered: Sep 2014
Location: united kingdom
Distribution: nito
Posts: 5

Rep: Reputation: 0
Your script appears to have added files instead of removing them. There are so many ways to improve.
 
  


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] php GD image extension conflicker Linux - Newbie 3 04-19-2012 12:40 PM
rm: cannot remove 'filename.extension' Operation not permitted shinystuffrox Linux - Software 8 03-07-2007 05:09 AM
Remove files w/ extension txt recursively spiri Linux - General 4 12-14-2005 03:52 AM
remove all files with same extension? scratchnz Linux - General 1 11-04-2005 03:40 AM
How to remove a php zend extension docbrazen Linux - Software 0 01-19-2004 02:17 PM

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

All times are GMT -5. The time now is 06:26 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