• Linux
  • July 2018
    M T W T F S S
    « Jun    
     1
    2345678
    9101112131415
    16171819202122
    23242526272829
    3031  
  • Meta

  • Follow me on Twitter

  • Advertisements

Tweaking scripts

Linux-works-for-youI fiddled around with a couple of my scripts yesterday. I made a couple of minor tweaks to my Getinfo script. I changed the variable that holds the filename that the information gets written too so that the file gets written to the logged in user’s home directory. Originally it was written to the current working directory. Rather than build the path and the filename using the += operator, I assigned it all to the variable at one go: infofile=”/home/$USER/$HOSTNAME.info”.

I also tried to write a more elegant version of my randpic.sh script which randomly selects a sub-directory in my Pictures directory and then picks a random file from that directory. I do have a few image files (wallpapers) in the root of the Pictures directory so now and then one of them will get selected as a directory and when the next part of the script tries to grab a file from this “directory” I get an “invalid file” message and the script runs through the loop and tries again. It really doesn’t bother me since these particular files are wallpaper images that can be disregarded for my purposes.

I thought about ways to test whether or not the selected directory was, indeed, a directory and if it wasn’t, to either process the file or to disregard the selection and try again. I tried nesting either a while or an until loop but I either ended up with an infinite loop or it kept selecting files from the same directory. In the second case, I saw that with each iteration of the outer while loop, it would add another “/” between the path and the filename when it displayed the selected file. I don’t think it actually affected the actual operations on the file, only how it displayed in the terminal.

I finally gave up on the idea although I would like to eventually figure it out. But I did make a couple of tweaks to the original script. In the case statement, I added a choice to quit the script and break out of the loop. Now “Y/y” accepts the selected file and breaks out of the loop while “Q/q” rejects the file and breaks out of the loop. Any other key rejects the file and continues the loop to select another directory and file.

 

Advertisements

SyncAll script & SSH

Tux_MonoI’ve been watching the EzeeLinux videos by Joe Collins at EzeeLinux.com and I’ve been learning a lot from them. I recently watched one in which he talked about a SyncAll script he wrote that uses rsync to synchronize a set of folders among all the computers in his home network. It occurred to me that our network environments were very similar so I reached out to him and asked for a copy of the script.

Modifying Joe’s script to fit my network and situation wasn’t all that difficult. One change I made to the script was to set a variable to hold the first three octets of the IP address so that if my networking environment changed for some reason (i.e., a new router) where it changed from 192.168.0.x to 192.168.1.x, I’d only have to make the change once in script.

It was by watching Mr. Desktop & Mr. Server Episode 5 | Linux Networking & Connect2SSH that it all came together for me. That video helped me to get SSH set up on my Linux computers so that the script would work properly. The video also introduced me to SCP (Secure Copy) and SSHFS (SSH File Share) which will prove to be very useful tools.

I’ve assigned static addresses to all of the wired connections, something I’d been thinking about doing for some time. I’ve still got a couple Windows PCs on the network but for now I can deal with them through SMB shares, Dropbox, or Sneaker-Net. If necessary, I could install Linux on the Windows box I just replaced and set up SAMBA on it.

 

conkyinfo.sh

This evening I wrote a short bash script to pull the necessary information for the battery, CPU, and network interfaces and writes the information to a text file. It combines the commands listed on my Conky Configuration page into a single script and provides the user with a text file from which to copy the device names and paste where necessary.

I had initially planned to include it at the beginning of the script to install my conky configuration but I’m thinking it might work better as a stand alone script. I’ll pretty up conkyinfo.sh and make it available from my Dropbox. I may still incorporate it into myconky.sh. That’s still a work in progress.

cpmvpic script

Last April I created cpmvpic.sh (copy move picture), a bash script that copies files from a temporary folder to my Dropbox so I can access them on my other systems. It also moves the file out of the temporary folder into a particular folder on my main system. At the time I used the images in html documents so later I added functionality to copy the document containing the image to my Dropbox. Then several months later I began creating the documents in LibreOffice (odt) format so I disabled that feature.

Today I made some changes to the directory structure for the Dropbox copies of the files so I modified the script to reflect those changes. I also renamed some of the variables to give them more meaningful names and re-enabled the copying of the target document.

The script uses the name of the image file, the 2-digit month, and the 4-digit year to build the necessary paths and document file names from base path names. It checks for the correct number of arguments and the validity of the paths and file names. If an error is detected, it displays an appropriate error message and the correct syntax, then exits.

I use this script for a specific series of documents which use the same naming convention in a particular set of folders. Although it’s a specialized script, it could be easily adapted for similar file copy operations.

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.

 

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: