2

I'm very new to bash scripting and I hope that someone can help me with that task.

I have a directory filled with *.SVG-files and I want to batch convert them with inkscape to PNG with the following command:

inkscape -f FILENAME.svg -w WIDTH -h HEIGHT -e FILENAME.png

The script also has a parameter $width and $height. If only one parameter is given, the script should create a square image where $width=$height

Example

Directory Content:

file1.svg
file2.svg
file3.svg

./batchscript.sh 1024 2000

Should perform

inkscape -f file1.svg -w 1024 -h 2000 -e file1.png
inkscape -f file2.svg -w 1024 -h 2000 -e file2.png
inkscape -f file3.svg -w 1024 -h 2000 -e file3.png

./batchscript.sh 3000

Should perform

inkscape -f file1.svg -w 3000 -h 3000 -e file1.png
inkscape -f file2.svg -w 3000 -h 3000 -e file2.png
inkscape -f file3.svg -w 3000 -h 3000 -e file3.png

The directory content should look like this afterwards:

file1.svg
file1.png
file2.svg
file2.png
file3.svg
file3.png
4

If you have GNU Parallel try this (based on Letizia's solution):

#!/bin/bash

if [ $# -eq 1 ]; then
   width=$1
   height=$1
else
  if [ $# -eq 2 ]; then
     width=$1
     height=$2
  else
     echo "Error: missing parameters!"
     echo "Usage: myscript.sh width [height]"
     exit 1
  fi
fi

parallel inkscape -f {} -w $width -h $height -e {.} ::: *.svg

All new computers have multiple cores, but most programs are serial in nature and will therefore not use the multiple cores. However, many tasks are extremely parallelizeable:

  • Run the same program on many files
  • Run the same program for every line in a file
  • Run the same program for every block in a file

GNU Parallel is a general parallelizer and makes is easy to run jobs in parallel on the same machine or on multiple machines you have ssh access to.

If you have 32 different jobs you want to run on 4 CPUs, a straight forward way to parallelize is to run 8 jobs on each CPU:

Simple scheduling

GNU Parallel instead spawns a new process when one finishes - keeping the CPUs active and thus saving time:

GNU Parallel scheduling

Installation

A personal installation does not require root access. It can be done in 10 seconds by doing this:

(wget -O - pi.dk/3 || curl pi.dk/3/ || fetch -o - http://pi.dk/3) | bash

For other installation options see http://git.savannah.gnu.org/cgit/parallel.git/tree/README

Learn more

See more examples: http://www.gnu.org/software/parallel/man.html

Watch the intro videos: https://www.youtube.com/playlist?list=PL284C9FF2488BC6D1

Walk through the tutorial: http://www.gnu.org/software/parallel/parallel_tutorial.html

Sign up for the email list to get support: https://lists.gnu.org/mailman/listinfo/parallel

2

Try this:

#!/bin/bash
if [ $# -eq 1 ]; then
   width=$1
   height=$1
else
  if [ $# -eq 2 ]; then
     width=$1
     height=$2
  else
     echo "Error: missing parameters!"
     echo "Usage: myscript.sh width [height]"
     exit 1
  fi
fi

ls *.svg | while read file
        do
           destFile=`echo $file | sed 's/\.svg/\.png/'`
           inkscape -f $file -w $width -h $height -e $destFile
        done
exit 0

First script check the number of parameter and set variable width and height or in case of missing parameters throws an error message and simple usage help.

Than assuming you run this command in directory that contains svg file and performs inkscape command as you need.

2

After some research I did it ;)

Here is the code for svg2png.sh

#!/bin/sh
# Saves all *.svg as *.png
# Check if SVG files are in this folder else exit
indir=$(ls -1 *.svg | wc -l)>/dev/null 2>&1;
if [ "$indir" -gt "0" ]
then
# Check if width-parameter is given else state an error and exit
if [ -z "$1" ]
then
echo 'Script usage: ./svg2png width [height]';
echo 'Where [height] is optional. If not given [height] = width';
exit;
>&2; 
exit 1;
fi

# Check if height-parameter is given. If yes: $height=$height If not: $height=$width and process all *.svg-files in the directory.

if [ -z "$2" ] 
then
#echo -en 'Processing file '$c' of '$indir'\e[5m...'
c="0";
for file in *.svg
do
     /usr/bin/inkscape -z -f "${file}" -w $1 -h $1 -e "${file}.png" >/dev/null 2>&1;
     echo -en "\r$i\e[0m";
     echo -en 'Processing file '$c' of '$indir'\e[5m...'
     ((c = c + 1));
done

else
c="0";
for file in *.svg
do
     /usr/bin/inkscape -z -f "${file}" -w $1 -h $2 -e "${file}.png" >/dev/null 2>&1;
     echo -en "\r$i\e[0m";
     echo -en 'Processing file '$c' of '$indir'\e[5m...'
     ((c = c + 1));
done
fi
# Output success message
echo -en "\r$i";
echo '\e[0m\e[1mSuccess: '$c' files have been processed!'

# When there are no SVG files to process abort script
else
echo -e "There are no SVG files to process. Aborting.";
fi

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.