Skip to content

Instantly share code, notes, and snippets.

@hearsid
Last active December 9, 2015 10:40
Show Gist options
  • Save hearsid/7b0ee31093ae8c84dc8c to your computer and use it in GitHub Desktop.
Save hearsid/7b0ee31093ae8c84dc8c to your computer and use it in GitHub Desktop.
Get base64 format of a batch of svg files in a folder and get js / json file containing the same as key value pair .
#!/bin/sh
output_file=output.txt
# remove the file if it exists
if [ -f $output_file ] # spaces in square brackets required for proper syntax
then
rm -f $output_file
fi
for i in *.svg; do
filename=$(basename "$i")
extension="${filename##*.}"
filename="${filename%.*}"
image_base64=$( base64 $i )
# append the content to a txt file
#echo $i >> $output_file
printf '"%s":"%s", \n' "$filename" "$image_base64" >> $output_file
done
# put curly braces on the text file
sed -i '1i{' $output_file # 1i (one i) append to begining of the file
echo '}' >> $output_file
# remove whitespaces from output.txt so it is properly visible in the js file
tr -d ' \t\n\r\f' <output.txt >temp.txt # take the new input from tr and put it in same file
# convert the file name from .txt to .js
mv temp.txt output.js
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment