• Linux
  • February 2019
    M T W T F S S
    « Jan    
     123
    45678910
    11121314151617
    18192021222324
    25262728  
  • Meta

  • Follow me on Twitter

  • Advertisements

ipinfo.sh

A while back I wrote a little script to display some basic IP information, public IP address, private IP address, default gateway, and DNS addresses. I had used the nmcli command piped through grep to display all of the IP4 information which also included route information which didn’t really interested me.

I didn’t particularly like the way the output looked so I took another look at the script to see if I could improve it. I did a little research and found a couple of different ways to get the local IP address and the default gateway. I still used nmcli to get the DNS addresses but found a way to use awk to get the just the addresses and display them nicely. The output looks so much better than before.

Just before I started on this little project I’d been watching a YouTube video covering some of the basics of using awk and I wondered if I could use awk to extract just the data I needed. I was able to use awk to pull and display all of the IP data except the public IP. Using awk, I didn’t need to use grep at all.

$ ipinfo.sh

IP Information
==============
Public IP:
	xx.xx.xx.xxx
Local IP:
	192.168.0.15/24
Default Gateway:
	192.168.0.1
DNS Servers:
	84.200.69.80
	84.200.70.40
	208.67.222.222

The script is on my GitHub Bash scripts repository.

Advertisements

Script Maintenance

This morning I attended to some script maintenance. I have several scripts that insert header information or licenses into existing files or create templates for new scripts or source files. I thought I’d clean them up and standardize them where I could.

I noticed that there was already a Templates folder in my home directory. It was empty so I figured this would be a good place to put my own templates and get them out of my Work folder. Of course, moving them meant that I had to change the scripts to point to the new location.

In some scripts I had included an option to edit the new or modified files. It made sense to include that option in all of these scripts and then I wondered what other options I should include. I decided that once the data had been processed, there should be options to edit the file, view the file, or quit without doing anything further. I also cleaned up the header comments to make them consistent.

Now that I’ve done that, why shouldn’t I combine some these scripts into a single script in which you could you choose the appropriate licence for your script or source code? I’m already thinking about it.

I added the Templates folders to the scripts I used to keep my files synchronized between systems. Then, as a final bit of cleanup, I made sure that I had the Atom editor, bat, and FnLoC on all of them.

  • The Atom Editor is fast becoming my go-to editor for writing code, scripts, and markdown files. I haven’t gotten beyond the basics yet but it’s very configurable and integrates with GitHub.
  • Bat is described as a cat clone with wings. It supports syntax highlighting for many programming and markup languages, communicaes with git with respect to the index, pipes its own output to less if the file is too large for one screen, and can be used to concatenate files.
  • FnLoC is my own little program that counts logical lines of code in C source files.

Another cleanup script

I’ve written a number of cleanup scriots recently – to clean up my ~/bin directory, to clean up my C programming work folders, and to clean up source code files written on DOS/Windows text editors. And this morning, I wrote another one.

I use HomeBank to keep track of my bank and credit card accounts. A couple of months ago I added the PPA to get the latest version and updates. I noticed that the newer version not only creates a backup copy of the main data file with a tilde at the end, but it also makes another backup appended with the current date and a .bak extension. Naturally, the number of backup files increases every day I use the program so it occurred to me that it would be prudent to delete the older backups after a certain period of time, say 30 days.

The find command has been a mainstay of my other cleanup scripts and it’s the primary command in this script. I also use pushd and popd to move to the HomeBank directory and return to where the script was called. Since the files are only about 170K each, I don’t see much point in adding the script to cron or anacron right now. Maybe later I’ll add it and run it monthly. Running it daily or weekly might be overkill.

Perhaps I should consider putting the previous month’s backup files into a tarball inside a backup folder. That would give me backups beyond 30 days. I’ll look into that.

I love it when I can throw together a script to handle a task.

FnLoC Updates

In the last couple of days, I’ve done some work on the FnLoC project and made several commits to my GitHub repository.

First of all, it dawned on my why FnLoC wouldn’t work properly with a lof of my source code from my Computer Science coursework 20 years ago. I had written much of that code using Dos and Windows text editors and IDEs. These editors would leave carriage return characters throughout the file. These characters wreaked havoc on my FnLoC program which was developed to work with files written in a Linus or Unix environment.

To solve the problem, I wrote a Bash script (dos2linux) to clean out the carriage returns. The script uses sed to remove the carriage return characters and creates a backup of the original file.


    sed -i.bak -e 's/\r//g' source-file.c

There are some other methods, such as the tr command, but this worked well for me and was easily incorporated into a script.

After going through and cleaning up all of my old CS source code files, I copied the code for my original LOC counting program then updated it so it was consistent with the line parsing process of FnLoC. I also added a couple of functions to print the results and to display syntax help. When I finished, I named it LLoC for “Logical Lines of Code.” While I was at it, I tidied up the FnLoc code a bit, just some minor formatting things I’d missed previously.

I also compiled the new code on my Windows 7 machine and updated the installation and removal batch files before placing them in a zipped archive.

This morning I modified the loc2file Bash script to incorporate LLOC by having it process header files. While going through my old code, I found a number of C++ files with .cc and .hh file extensions so I added those extensions to those extensions to be used.

Then I updated the deb package and updated the documentation. When I was satisfied I had everything up to date, I updated the Git repository and pushed the changes to my GitHub repository.

Scripts for Conky Scripts

I’ve been using Conky as a system monitor on my desktops for a number of years. At one time I had an extensive script running on them. Then changes to Conky in the Ubuntu 16.04 repositories broke parts of my scripts so I opted for simpler scripts and finally decided upon a simple script that I could use on both desktops and laptops.

Lately, I’ve been using sed in some of my scripts to insert headers and licenses into my scripts and source code. I even came up with scripts that used templates to create the basic framework for new scripts and code.

Today I got to thinking about applying this to my conky scripts. Back when I had more complex conky scripts, I wanted to to create a script to install and configure conky on my systems but the amount of information about devices I needed was a bit overwhelming.

But now my basic conky script is much simpler as there are only three devices whose device names varied — the battery (for laptops), and the wired and wireless network adapters. I figured that I could easily get the device names in a script and use the sed command to put them into the appropriate places in the script. Then it was a simple matter to create a .conkyrc file.

Once I got the script written and tested, it occurred to me that I could easily to add the commands to install Conky, add a .desktop file to the autostart folder, then start Conky in the background.

BASH Scripts on GitHub

I created a second repository on GitHub for BASH scripts and I uploaded six scripts that might be of interest to others.

getinfo gets system information and places it in a text file located in the user’s home directory. The file includes:

  • Computer manufacturer, product name, version,, and serial number
  • Operating system
  • Amount of physical memory
  • System devices such as CPU model, graphics adapter, and network adapters
  • Battery information
  • Hard disk information to include model, serial number and capacity
  • Partition information

locale-fix.sh fixes an error that sometimes occurs during updates when you get the following warning;

Warning: No support for locale: us_US.UTF-8
The problem is that /usr/share/initramfs-tools/hooks/root_locale
is expecting to see individual locale directories in /usr/lib/locale,
but locale-gen is configured to generate an archive file by default.

The fix purges the existing locales and tells update-initramfs to store locales in individual directories instead of an archive and then updates the locales.

ren-ext renames certain file extensions. Sometimes downloaded file have extensions in all caps or a different extension like .jpeg, JPG, or HTM.

  • The script changes extensions as follows:
  • BAK is renamed to bak
  • GIF is renamed to gif
  • htm, HTM, HTML are renamed to html
  • jpeg, JPT are renamed to jpg
  • MP3 and MP4 are renamed to mp3 and mp4
  • PDF is renamed to pdf
  • PNG is renamed to png
  • TXT is renamed to txt

renspace replaces spaces in filenames with underscores in a directory.

rmtilde removes backup files with a trailing ~ (tilde). I wrote this script after accidentally deleting an entire directory while attempting to type rm *~. While reaching for the ~ key, my finger missed the shift key and hit the Enter key, deleting the entire directory. Fortunately, I had a recent backup and I was able to restore my files.

upper2lower changes capital letters in filenames to lowercase. The script can be run on a specific file or it can be run against an entire directory. If run without an argument, the user will be asked for confirmation before renaming all file names.

A lot of the scripts I’ve written are for specific purposes but these seemd to be general enough to be useful to others. The scripts can be found at my GitHub repository.

Script to create scripts

I’d been playing around with sed in scripts to modify and insert licenses into scripts and source code and even set up a script to insert a basic header into existing scripts. Last night, the idea of writing a script to create the foundation of a script – the hash-bang and a basic header.

This morning I sat down and wrote the script. I started with checks for the arguments. I didn’t want a new script overwriting an existing one and I wanted to make sure that something was passed to give the new script a name.

Then I used sed to modify a header template, filling in information such as the name of the script, the author and the author’s email. I was able to get sed to append the modified header into the file like I did with my license script. I used the cat command to concatenate it. It works.

Once the script foundation is created, I delete the temporary header file and give the new script execute permissions. Then I give the user the option to open the script in nano for editing or displaying it using cat.

The output is as follows:

#!/bin/bash
#####################################################################
# Script Name :testscript.sh
# Description :
# Dependencies :
# Arguments :
# Author :Richard Romig
# Email :rick.romig@gmail.com
#####################################################################

%d bloggers like this: