Updating the watermarking script

A while ago I had written a post about the script that I used in Linux to apply the watermark to all of my photos and now I’ve enhanced the script so that it allows for more control without having to modify the whole script and also add other options to the script.

The script can be downloaded from: http://files.codebeta.net/addmark2

I added a whole section on handling the options that are passed when executing the script so that the user can specify the watermark to be used, the size that the longest side of the photo will be resized to, whether to force the watermarking or not and even whether to resize the image or not.

I read the IBM article on Bash parameters and parameter expansions to learn how to handle the options and still be able to send the file names of the images to be watermarked.

The script has defaults which are stored on variables and are replaced with new values in case the user starts the script with the options. For example:

-w     This allows the user to use an especific image as watermark.

For example: addmark -w newwatermark.jpg photo.jpg

The above example would place the image newwatermark.jpg within photo.jpg

There are other options which allow the user to modify what the script does and you can read about them on the script or just execute the script without any parameters and it’ll give you the options.

At first I thought that there was a way that would allow me to go through the loop and with if statements look for the options but it essentially wouldn’t allow me to move ahead one position on the array, well if that can be called an array :P

There is a command called getopts which basically looks for options, which are those parameters that start with a dash (-), in the $@. A while loop is used for this so that all of the parameters are searched. The while loop would be as follows:

while getopts “w:s:l:t:fn” optname

The options that you would like to make available are to be placed within the quotes and the colon after the letter means that the option receives a value so that getopts expects to find a value after the option and not simply another option. For example:

The letter w has a colon (:) thus it expects a value, thus “addmark -w photo.jpg -f” is correct and “addmark -w -f” would actually cause an error condition

The options that are going to be used must be within the quotes, so for example the following command:

addmark -r

Would actually return an error since there is no r within the quotes.

The word “optname” in the while statement above is actually the variable that will be used within the while loop to know what the option is. In this case we would use the case statement using the “optname”

case “$optname” in

And then the options would follow in the following fashion

“w”)
echo “Watermark to be used: $OPTARG”
WATERMARK=$OPTARG
;;

The option letter goes with quotes and then the commands to be executed in case that option is found in the parameters. Remember to use the double semi-colon (;) at the end of the case function, if you don’t include them it cause for the script to continue with the next set of commands, for example

“w”)
echo “Watermark to be used: $OPTARG”
WATERMARK=$OPTARG
“s”)
echo “Size is: $OPTARG”
SIZE=$OPTARG
;;

This would mean that when the option w is encountered the script would execute the commands for option s even if that option wasn’t called for.

Also remember to include the options ? (question mark), : (colon) and * (asterisk) as these would be used in case of encountering an error or when an option doesn’t exist. When getopts encounters an option that is invalid, for example it was never established before, it will return a question mark as the value of the variable optname. The colon (:) will be used in the event that the user forgets to give a value for an option that expects a value, in the case of the addmark script it would enter this situation if the user, for example, forgets to give a path to the photo when using the option -w.

The asterisk (*) will be encountered in any other error condition, though this shouldn’t happen.

There is one other variable that should be kept in mind and that is the $OPTIND which basically returns the position within the parameter array. When would one use this? When you, for example, need to get the rest of the parameters that were used, so in this case

addmark -w photo.jpg -n image1.jpg image2.jpg

I would need to get the image1.jpg and image2.jpg out of the whole string because those are the photos that will be watermarked. Running the following command within the script

PHOTOS=”${@:$OPTIND}”

Would store “image1.jpg image2.jpg” in the variable PHOTOS and skip the options that were also given in the parameters.

-LM