0

i wanted to convert the format of all image files in a folder using the following shell script. My idea is to use the base name of the filename and using the same for naming the file to be converted. I gathered these lines from internet and could not meet my requirement. Thank u all.

for f in *.eps
do
 echo "converting format....."
 filename=`basename ${f}`   
 fbase=`$filename | cut -d'.' -f1`  
 extn = ${fbase}.png  
   convert -geometry 1000x1000 -density 300 -trim ${f} $extn
done 

1 Answer 1

1

I think you need something more like this:

#!/bin/bash
for f in *.eps
do  
  new=${f/eps$/png}
  echo Converting $f to $new
  convert -geometry 1000x1000 -density 300 -trim "$f" "$new"
done

Note:

The line commencing "new=" is performing a bash substitution in the variable "f", replacing "eps" at the end of the line (i.e. $) with "png".

Sign up to request clarification or add additional context in comments.

3 Comments

don't use backticks, use $(...) instead.
and basename can remove the suffix as well, $ echo $(basename test.eps .eps).png
Ok, I guess they are more trendy and do have advantages. Thank you - I have updated the answer so my, and the OP's, bad habits don't spread ;-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.