Shell function to convert .png to .svg file I needed to convert some custom icons that were in .png format to .svg format and did not want to use an online converter for it. 

I could not find an out-of-the-box solution online so I created my own with what I could find.

Out of curiosity and a willingness to learn, as well as to maybe serve the needs of others trying to accomplish the same task I am uploading it here as my first post. 

My knowledge on image formats is very minimum at best. My knowledge of shell scripting is at a beginner's stage. My knowledge of coding overall is at hobby level.

I am hoping any comments will help to:


Improve on the overall code quality &amp; readability (also to benefit future maintenance)
Improve on the quality of the actual conversion, make it less lossy.
If possible remove any unneccessary dependencies, as currently potrace is used which wasn't installed by default on my arch system. 


My current working setup:

function png_to_svg() {

    ### First use convert to convert to .pnm format ###

    number_of_dots_in_filepath=$(echo "$1" | grep -o "\." | wc -l)
    pnm_filepath=""
    for i in $(seq 1 $number_of_dots_in_filepath); do 
        pnm_filepath="$pnm_filepath.$(echo "$1" | cut -f $i -d '.')"
    done

    remove_leading_space=${pnm_filepath#?}
    pnm_filepath="$remove_leading_space.pnm"
    command="convert \"$1\" \"$pnm_filepath\""

    eval "$command"

    ### Second use potrace to convert .pnm to .svg file ###

    svg_filepath="$remove_leading_space.svg"
    command="potrace \"$pnm_filepath\" -s -o \"$svg_filepath\""
    eval "$final_command"

    ### Third and last: remove temporary .pnm file ###

    eval "rm \"$pnm_filepath\""

}


Edited setup after first reply:

function png_to_svg() {

    ### First use convert to convert to .pnm format ###

    pnm_filepath=${1%.*}.pnm
    convert "$1" "$pnm_filepath"

    ### Second use potrace to convert .pnm to .svg file ###

    svg_filepath=${1%.*}.svg
    potrace "$pnm_filepath" -s -o "$svg_filepath"

    ### Third and last: remove temporary .pnm file ###

    rm "$pnm_filepath"

}
You don’t need eval. For example,

eval "rm \"$pnm_filepath\""


is the same as

rm "$pnm_filepath"


but the latter is more readable.

The same is true for the other two cases where you use eval.

I’m not sure what the purpose is of looping over the dots in the file path. Note that directory names can have dots too. The best way to get a file name with a different extension is to use ${x%.*} (see here):

pnm_filepath=${1%.*}.pnm


(I’m assuming you use Bash).