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.


Learning Bash

Over the years I’ve written a lot of batch files in MS-DOS and whatever they call the current command line interface in Windows these days. I guess I’ve gained a bit of proficiency with it. In a former job I created a number of batch scripts to automate configuration of the computers I was deploying as well as utility scripts. Now that I’m using Linux more, it makes sense for me to write scripts in that environment too.

The catalyst to learn more about Bash scripts was to streamline a little task I wanted to do on my system. For years I’ve been keeping a journal in html format and I save interesting images to a folder for inclusion in my journal. In my file browser, I like to view the directory by modification date in reverse order so that the most recent files are a the beginning.

Occasionally, I find some images that I’d like to keep near the beginning of the directory listing for easy retrieval. I’d open a terminal window and manually touch each file individually but that became a tedious process after a while and the idea of writing a script for it made sense.

I entered the appropriate file names into a text file and wrote a simple script to use a while loop to read the file names from the list and run the touch command against them. That worked pretty well. Then, as I delved more into some tutorials I found ways to make my script more robust and reusable. Instead of a hard-coded target filename, I set it up to take a command line argument and then as it read the list, test to make sure the file existed, just in case I’d forgotten to remove I filename I no longer needed.

Out of the plethora of Bash tutorials out there on the Internet, I found a couple targeted toward a beginner but even they seem to presume intimacy with Linux commands so when I attempt some of the suggested exercises, I find myself searching for information on commands or ways to accomplish tasks. Quite often I find the information a little above my current level of geek fluency. There is so much that I’ve forgotten from my days as a Computer Science student.

Maybe I haven’t found the right tutorial yet or I need to find a good Bash for Dummies book. Even a classroom environment might be helpful in that I could ask questions, get immediate feedback, and learn from others.

%d bloggers like this: