#!/bin/bash

    # This script takes any number of image filenames as arguments and
    # creates a thumbnail image for each one. The thumbnail image has the
    # same name as the original file, except with _thumb inserted before
    # the file extension.

    # some pictures of me to use with this script are posted as pictures.tar

    # if no arguments were given, print a usage message to stderr and exit
    if [ $# -eq 0 ]
    then
        echo "usage: $0 FILE [FILE...]" 1>&2
        exit 1
    fi

    for pic in $@ # loop over the arguments
    do
        if [ ! -e $pic ] # check if the image file exists
        then
           echo "error: $pic does not exist" 1>&2
        continue # just like continue in Java
        fi

        # ${pic%.*} gives us pic with everything from the final . to the
        # end deleted. For example, "flower.png" becomes "flower", and
        # "green.lake.jpg" becomes "green.lake". This is because .*
        # matches a . followed by any number of characters, and % deletes
        # the shortest such match from the end.

        # ${pic##*.} gives us pic with everything from the beginning to
        # the final . deleted. For example, "flower.png" becomes "png",
        # and "green.lake.jpg" becomes "jpg". This is because *. matches
        # any number of characters followed by a ., and ## deletes the
        # longest such match from the beginning.

        convert -thumbnail 100x100 $pic ${pic%.*}_thumb.${pic##*.}
    done