Scripting again

Often when I download images from my digital camera or my mobile phone, or other files from the Internet, I’ll often find that the file extensions will be in all caps and my preference is to have them lower case.

I came across the rename command a while back and found it quite useful but found the syntax a bit difficult to remember. I’d been thinking about creating a bash script to make using the command a little easier and realized that it would be good if I could apply it to several file extensions I’m likely to encounter. This morning a wrote a simple script taking the file extension I wished to rename as an argument and using case statements to select the appropriate rename syntax. I also included a function to be called if no argument or an incorrect argument was entered.

After successfully testing it on a series of test files, I ran it in some folders in my ~/Pictures directory that had files with the .JPG extension and all the extensions were changed to .jpg. The script works well and at a later time I’ll probably add checks to handle duplicate file names, should they occur. As it is the rename command won’t change the file name if a duplicate will result.


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.


Conky config page updated

I think I’ve done enough with my conky configuration and installation for now. I successfully installed and configured conky on two Peppermint 7 Linux installations, on a virtual machine on my main desktop and on a PC I’d built a few years ago. I did have to do a little editing of the .conkyrc file on each. In the VM installation, I had to comment out the line for the second CPU core since there was only one core. It displayed an error about trying to access a core that didn’t exist. On the PC, I had to edit the Network section because the system saw the network interface as enp2s6 rather than eth0. The card was a Netgear FA311 Fast Ethernet card. I don’t know if the device name is a peculiarity with the card or with Peppermint.

Another thing I notice with my script in Peppermint was that the time was slightly to the left of center. In my installations with Ubuntu and Mint, it was always perfectly centered. I was able to fix the problem by changing the ${alignc 35} at the beginning of the penultimate line of the script to ${alignc}. I suspect that the 35 was some sort of an offset of some sort. Removing the 35 from the line on a Mint installation moved the time to a bit right of center. That’s something to be aware of, I guess.

I had installed conky on the VM yesterday and when I started the VM today, conky displayed as expected so adding the conky.desktop file to ~/.config/autostart/ is all that’s needed to launch it at boot-up. After installing it on the PC today, I opened the Autostart GUI and confirmed that conky was listed.

Satisfied that the installation commands were correct, I restructured the My Conky Configuration page. Under the Installation heading, I listed the steps to install conky from the command line. I put the commands in bold text. You can highlight and copy to the clipboard. To paste them in the terminal use the Crtl+Shift+v key combination. Before the actual installation steps, I include commands to check for battery, CPU, and network interface information.

I kept some of the old installation information in case anyone wants to do it that way. Be aware that menus may be different depending on the distro you’re using.

After the script itself, I created a Notes heading that includes the notes I’ve made for each update. It’s kind of a changelog, I guess.

I’ve abandoned the idea of creating a bash script for the installation. There are only three commands and it’s easy enough to copy and paste them into the terminal.

In my instructions, I used gedit but you can use whatever editor you feel comfortable with. A graphical editor such as gedit works well if you need to do much cutting and pasting like when you need to change eth0 to something else on several lines. I like to use nano from the command line for small changes.

Latest conky update

I spent several hours working on my conky script, bouncing between three machines to make sure it was displaying everything right. I think I finally have it the way I want it. I did make a new comment to conky modifications but that was before I finished for the day.

I added a line to the script to display the processor make and model information, placing it at the top of the PROCESSOR section. It uses grep, sed, and cut to get a string out of the /proc/cpuinfo file. The line will probably needed to be edited to get a string of the proper length for your processor name.  You’ll need to view the cpuinfo file to see how many characters you’ll need to adjust the ‘cat -c 1-xx’ at the end of the line. Or you can run the full command in a terminal, trying different values. You can open it in gedit and copy the command to the clipboard and paste into your terminal using Ctrl-Shift-V.

I also added a few lines to give a numerical and bar graph display of battery power remaining. In the script I have on the web page and the conkyrc.txt file, these lines are commented out. If you’re running the script on a laptop, just remove the hashtags from the appropriate lines.  I wasn’t able to find a reliable way for the script to determine if a battery existed.

I cleaned up the NETWORK section so that the LAN and WLAN displays were consistent and to generally clean things up. I’ve noticed that if my Internet connection is down, the Public IP line will just drop from the display when conky refreshes. For wireless connections my online script had lines to display SSID and Connection quality but they were missing from the actual script I was running on my laptop. I was endeavoring for consistency among my own systems as well.

As always, I updated My Conky Configuration page with the revised script along with the updated conkyrc.txt file and a new screen shot. Below are the conky displays for my laptop and my main desktop PC. This should give you some idea of how it looks.

conky modifications

conkyI discovered conky several years ago and it has been a staple of my Linux desktop ever since. It displays information about the system, processes, memory, disk usage, and network activity at a glance. Today I looked around to find out ways to enhance my script. There are a plethora of conky scripts out there but I only made a couple of minor changes to mine.

I inserted if_exist statements at the beginning of the network sections of the script to check if the LAN and WLAN interfaces were present or active. Before I had been commenting or deleting the WLAN section on a desktop PC. Now I don’t have to change the script. On a laptop, only the network interfaces that are active at bootup will be shown.

I also added lines to display the time and date at the bottom of the display. Sometimes it’s handy to be able to see the time on the desktop.

I have instructions for installing the script at Rick’s Tech Stuff. The link is Conky Config . The install instructions are written for a Debian or Ubuntu (Mint) system.

Basically, you install conky , put it in your startup apps and create the script in your home directory. You can cut and paste the script from the page or download it.

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: