PDA

View Full Version : Shell-Script: I want to rip off the 4 last characters of a var.



Repgahroll
November 11th, 2009, 04:45 PM
I'm making a shell script to efficiently "thumbnalize" lotsa PNG images and convert them into JPG and also watermark their respective PNG sources. The problem is, after some "runtime", i need to delete the ".png" at the end of the variable "$img" (ls *png) , so i can save it as "something.jpg" and not "something.png.jpg".
I tried to use cut and sed but they don't work with variables (i think), a workaround is to "rename" the jpg images after saving them... but i would like to just remove the last 4 char of the variable at runtime and save them correctly.

Thank you in advance.

Sacker
November 11th, 2009, 04:51 PM
You might be able to use something like

basename filename.png .png

Repgahroll
November 11th, 2009, 05:17 PM
Exactly what I need! Thank you very much!!

ghostdog74
November 11th, 2009, 05:42 PM
no need to use external command. in shell


for filename in *.png
do
echo ${filename%.png}
done

Repgahroll
November 11th, 2009, 05:55 PM
no need to use external command. in shell


for filename in *.png
do
echo ${filename%.png}
done


COOL!!

Many thanks! ;)

John Bean
November 11th, 2009, 06:01 PM
I tried to use cut and sed but they don't work with variables (i think)
If you want to use cut you could do this sort of thing:

for pngfile in *.png; do
echo $(echo $pngfile | cut -d. -f1).jpg
done

ghostdog74
November 12th, 2009, 12:21 AM
If you want to use cut you could do this sort of thing:

for pngfile in *.png; do
echo $(echo $pngfile | cut -d. -f1).jpg
done
no need to use external tools. since the shell can do this, using the shell is much faster ( although to some, they do not care.)

John Bean
November 12th, 2009, 09:52 AM
no need to use external tools. since the shell can do this, using the shell is much faster ( although to some, they do not care.)
I'm perfectly aware of that. My reply was in the context of the part of the OP I quoted, to demonstrate the error in his assumption that "cut doesn't work with variables". Although it's not needed or desirable in this case, the use of echo output piped into a filter like cut is a valid (if often abused) technique.

This sort of query deserves a number of possible solutions in order to learn anything from it. Choice is good.

raffaele181188
November 12th, 2009, 12:14 PM
@John Bean
But that doesn't work if your png is named


dotted.image.filename.png

John Bean
November 12th, 2009, 01:00 PM
@John Bean
But that doesn't work if your png is named


dotted.image.filename.png

Sigh. Let me say it one more time: the OP wrote "I tried to use cut and sed but they don't work with variables", so I used an example to show him that his assumption was incorrect. He may really need to use a similar construct at some point, so it's useful to know how to use variables with filters like cut.

The most appropriate solution to his problem using the shell's inbuilt functions had already been given.