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.



5 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.

    • This morning I modified my random image script to copy the image file to a temp folder so I can view it before deciding to accept or reject it. If it’s rejected, the script deletes the file from the temp folder and selects another file. I always have the temp folder open on the desktop when I use the two scripts.

      [Added] I added a test to determine if the selected file was a valid file. If the file is valid, it goes through the process. If it’s not, then it’s rejected and goes through the loop again. I noticed that if the file name contains spaces, the cp and rm commands in the script will fail. I set a variable to store the name of the temporary folder and I enclosed path and filename variables used in quotes (“”) when used with the cp and rm commands. That seems to be working.

      • I noticed that if I pressed any key but y or n, the file wouldn’t be accepted and the loop would continue but the unaccepted file would remain in my tmp folder. I modified the prompt routine so that if any characters other than the upper or lower case y were pressed, the selected image would be considered to be rejected and it would be removed from the tmp folder and a new file would be selected. Basically, I made rejecting the file as the default, acceptance must be specifically chosen.

  3. I added a few lines to the script to have it copy the journal file (html) to my Dropbox folder so I’d have an updated copy there. Eventually, I’d like to wright a script to update or synch both the html and odt files between the work folders and Dropbox.

Comments are closed.

%d bloggers like this: