A little scripting

About a month ago I created a little bash script to copy an image file from a temporary folder to my journal folders on my PC and Dropbox. The original script worked well enough but it lacked error checking plus I knew I’d have to eventually make changes to use it with next years folders.

The original script took two arguments from the command line, the file to be copied and moved, and the month which designated the appropriate folder. I added a third argument, the four-digit year used in the parent folder. Since the year would be incorporated with in the folder names and the month at the end, I changed the method used to build the folder names using the += operator.

In creating the tests to check the validity of the file to be manipulated and the existence of the created folders, I found it useful to create a function that would print the proper syntax should any of the tests fail.

I also developed a test that after the copy and the move operations checks for the existence of the file in each of the destination folders and displays the operation’s success or failure.

This was a good scripting exercise for me and the first time I’ve used a function in a utility script. I’d like to improve my scripting skills but the tutorials I have have given an elementary understanding but provide little in the way of practical examples and the usage of functions. I’ll be looking for other tutorials to advance my knowledge.



2 Responses

  1. I thought I’d tested it pretty well but the first time I used it “in production”, I kept getting an error message that the file name was invalid. I tried with the original file I’d used for testing and it seemed to work.

    Then looking closely at the script, I saw that I hadn’t put the path to the file in the test to check the file. The reason it worked with the test file was because I had a copy of it in my ~/bin folder and that’s where it was looking. The correct path was in the lines that did the copy and move operations. The problem was easily fixed and worked perfectly.

    Today I cleaned the script up a bit and made a couple more little tweaks, nothing that should affect it operationally.

  2. I had a script to pick a random image file from a folder and copy it to a temporary work folder to be processed for inclusion in an HTML-based personal journal. Yesterday, I modified the script to randomly select an image from any folder in ~/Pictures. Sometimes I don’t wish to use the selected image so I put the whole process into a while loop and set up a prompt using a case statement to accept or decline the image. If the image is declined, it runs through the loop again to grab another image. If the image is accepted, the job is done and the script exits.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: