(Even More) Advanced Bash Completion

In my previous post, I described how to set up tab completion for many common commands using these Bash completion files. This works well for established commands, but it doesn’t work so well for commands that I have written myself.

I use a command called “hc12-console” to connect to 68HC12 microcontrollers over a serial port. The command takes two arguments: the name of a microcontroller to connect to and a file to load. I only have two microcontrollers called “dragon1” and “dragon2”. Therefore, I want to be able to tab complete the first argument to one of those values only. The second argument should be the name of a file that ends in “.load”.

I do this with a function that checks the argument number and then completes it based on a specified list or by limiting the types of files that will be listed.

_hc12console ()
{
	local cur

	COMPREPLY=()
	cur=${COMP_WORDS[COMP_CWORD]}

	# First argument completes with either dragon1 or dragon2
	if [[ $COMP_CWORD -eq 1 ]] ; then
		COMPREPLY=( $( compgen -W "dragon1 dragon2" -- $cur ) )
		return 0
	fi

	# Second argument completes with only files matching *.load
	if [[ $COMP_CWORD -eq 2 ]] ; then
		COMPREPLY=( $( compgen -f -X '!*.load' -- $cur ) )
		return 0
	fi

	# All other arguments will not auto-complete
	return 0
}
complete -F _hc12console hc12-console

I added the script above to a file that my bashrc sources. It associates the function with the shell command, and then instead of manually typing out:

hc12-console dragon1 file.load <enter>

I can type:

hc12<tab> <tab>1 <tab> <enter>

Run a Shell Command in a Makefile

I had a directory of BMP image files that I wanted to convert to EPS (Encapsulated PostScript). Since I was planning on adding files to the directory, I did not want to hard code the names of the files into the makefile that would do the conversion. Therefore, the trick was to run a shell command inside of the makefile. The first line lists all of the BMP files and assigns it to “BMP_FILES”. The next line replaces the “bmp” extension with “eps”. The complete makefile is below.

BMP_FILES = $(shell ls *.bmp)
EPS_FILES = ${BMP_FILES:%.bmp=%.eps}

all: ${EPS_FILES}

%.eps : %.bmp
	convert $*.bmp $*.eps

All that is left is to type “make” in the directory, and all of the BMP files will be converted to EPS.

ZigVersion

I prefer to do all of my Subversion work from the command line, but I realize that not all people are like me. Clarissa and I have been using a wiki to keep track of most of our shared information (including our wedding preparations), but our wiki does not work well for content like spreadsheets. Therefore, I decided to try out a Subversion repository. After some initial searching for graphical clients, I found ZigVersion. It is a simple graphical Subversion client for Mac OS X. It took a little bit to explain how it worked and how to use it to Clarissa, but now we have been sharing and editing non-wiki-friendly files for a few weeks.

Extract Email Attachments With Procmail and Munpack

I regularly use the Marquette University PrintWise Canon copiers to scan paper documents into PDF files. These files are delivered to me via email where I manually save each PDF attachment, rename it, and move it to the appropriate directory. Using Procmail and Munpack, I was able to eliminate some of the tedium of this problem.

I inserted the following block of code into my .procmailrc file. It looks for emails that were sent from me and to me by a Canon copier. When it finds one, it copes the email and pipes it to Munpack which extracts the attachments into my attachments directory.

ATTACH=`echo $HOME/attachments`
:0 c
* ^To:.*[email protected]
* ^From:.*[email protected]
* ^X-Mailer: Canon imageRUNNER
| munpack -q -C $ATTACH

After the attachment has been extracted, I can rename it and move it to the appropriate directory, and I still receive the email in my inbox so I know I have attachments to deal with.

iTunes Web Application Search Results

My iTunes Web Application project shows up on the first page of results for most relevant search. More than 60 people clicked on search results for it on Google in September 2009. I would say this is a good thing. However, I doubt it is what most people are actually looking for. I really do recommend Subsonic or even Sockso over my own project. Unless someone is looking to continue work on my project. I would recommend that, too.

PDF to PNG Conversion on Mac OS X

I had a several page scanned document stored in PDF format that I needed to convert to PNG so I could upload it to a PayPal account to resolve a limited access problem. I found several solutions for Linux that produced inadequate image files. When I switched to my Mac OS X laptop, I opened the PDF in Preview. I selected “Save As…” from the File menu, selected “PNG” from the drop down menu and set the resolution to 300 pixels per inch. I hit the Save button, and the newly created PNG opened up. The quality was adequate, and the problem was solved.

Time Warner Internet in my Studio Apartment

I moved into my apartment just over a week ago. Time Warner came out and set up my Internet in my studio apartment this morning. The process was pretty smooth. However, it would appear that my unit had the only unmarked line in the box in the basement. The cable disaster made finding the line quite the challenge.

Time Warner Cable Box

Once the technician found the correct line, the rest was easy, and the speeds are what I expected.

Time Warner Speakeasy Speet Test

Time Warner Speedtest.net

Since I just need Internet for one more semester, hopefully, I won’t have any problems.

eBooks on BlackBerry

Up until recently, I have read most of my novels and books on a Palm TX handheld using TextDrive. I used PorDiBle to convert text files into PDB files that TextDrive could read.

This method worked much better when I always carried my Palm handheld with me and used it as my primary calendar and contact management device. The appeal of electronic books is that I always have it with me. Once I started using my BlackBerry to maintain everything, my Palm fell into disuse, and I no longer carried it with me. I started reading less. I needed a new solution.

I have started using Mobipocket on my BlackBerry and Stanza to convert texts. Mobipocket has a whole online store for purchasing texts, but the reader itself is free and can read non-purchased texts that I already have. Stanza can convert between plain text and the Mobipocket format. Unfortunately, Stanza is only available for Mac OS X and Windows. There are other options, but Stanza worked best.

Now I once again always carry the device with my current novel, and I can spend my free time reading instead of playing games on my BlackBerry.

Bayes Filtering in SpamAssassin

The Bayesian classifier in SpamAssassin began tagging emails a few days ago. I found this out because while messages were not marked as spam, my procmail rule started diverting all messages to my spam folder. The old rule was not particular about where the yes was and since BAYES contains yes, all emails looked like spam. The new rule only looks for the yes at the beginning.

# Old Rule
:H
* ^X-Spam-Status:.*Yes
$MAIL/spam
# New Rule
:H
* ^X-Spam-Status: Yes
$MAIL/spam

Now incoming spam messages contain an additional score in the spam report.

X-Spam-Report:
        *  3.5 BAYES_99 BODY: Bayesian spam probability is 99 to 100%
        *      [score: 1.0000]

I was surprised that it took the Bayes filter three months to gather enough email to begin scoring incoming email. It is a nice addition because it bumps up the spam scores enough to ensure that more messages that are spam get marked as such.

AT&T contracts are meaningless

I had a great experience on the phone with an AT&T representative when I called to transfer my number from an existing corporate plan to my own personal plan. I selected my options, and the representative explained to me that because it was a transfer, I would get a standard zero-month contract. He explained to me that I was free to change my service or transfer my number to another provider whenever I wanted to. This all went through successfully, and I was set.

Two weeks later, I noticed that my account had been slapped with an 18 month contract. When I called, AT&T had no idea why I would have had no contract, and they said that a standard transfer of service contract is 11 months. They told me that the first representative I spoke with was mistaken and that there is no record of my zero-month contract. Luckily I got my contract bumped down to 11 months; however, it doesn’t explain what happened to the zero-month contract.

Edit: 11 months is the standard length for a transfer of service. However, I did not agree to that over the phone. If they can slap me with a contract without any change in service or without my acceptance, their word and their contract are meaningless.