Automating with Simple Shell Scripts

Here’s a couple of small, simple Shell Scripts that I wrote to automate the resizing and adding of images to this blog.

The first takes an image prefix (or full name) as a variable, and uses the ImageMagick program convert to create a thumbnail, with a tn. prefix in front of the original filename, then scales the original image.

#!/bin/bash

for file in $1*
do
convert -scale 250 $file tn.$file
convert -scale 1024 $file $file
done

The second script takes two variables – the first is the file prefix and the second the path underneath images (as all my blog images reside under /images/) where the files are located. it then creates a table layout, with two thumbnails per line linking to the full-size image.

#!/bin/bash

opencounter=1
closecounter=1

echo '<table align="center">' >> imagelinks.html
echo '<tr><td><em>Click thumbnail for full-size image</em></td></tr>' >> imagelinks.html

for file in $1*
do
opencounter=$((opencounter+1))
if [ $opencounter -gt 2 ]
then
opencounter=1
else
echo '<tr>' >> imagelinks.html
fi

echo '<td><a href="/images/'$2''$file'"><img src="/images/'$2'tn.'$file'" alt="'$file'" title="'$file'" /></a></td>' >> imagelinks.html

if [ $closecounter -gt 2 ]
then
echo '</tr>' >> imagelinks.html
closecounter=1
fi

done

echo '</table>' >> imagelinks.html

So, nothing special, neither script does any error checking – but isn’t that the beauty of writing stuff for yourself? Just two little examples of how a little bit of scripting can automate an otherwise laborious task.

This entry was posted in Computing. Bookmark the permalink.

Leave a comment