Monthly Archives: April 2009

One-Liner to Display My Computer’s IP Address

Here is yet another way to diplay a list of IP address associated with the current computer.

python -c "import socket; print '\n'.join(socket.gethostbyname_ex(socket.gethostname())[2])"

The socket.gethostname() call returns the host name of the computer. The socket.gethostbyname_ex() call returns a list of three items: The host name, the list of aliases for this host, and a list of IP addresses. Recall that Python’s array starts with index 0, the socket.gethostbyname_ex(…)[2] expression refers to the list of IP addresses. Finally, the print statement prints out the IP addresses, one per line.

The No-Tulip Festival

This year we once again went to the Skagit County Tulip Festival. However, we were in a bit of surprise: the fields are yet to bloom due to the colder-than-usual winter.

Instead of driving around and watch the disappointing fields, we decided to visit the Roozengaarde and enjoyed their spectacular tulip patches. I took most of my photos in this garden.

Although the festival was about tulips, I managed to take a couple of shots of the dafodils, which are underrated in my opinion.

Here is a short video I took that day, just to test drive my Kodak Zi6 pocket HD camcorder.

For more pictures I took that day, visit my gallery.

Changing Directory Listing (ls) Color in Linux and Mac OS X


I practically “live” on the Linux command line and one thing that has been bugging me for so long that now I decided to do something about it. On my company’s Linux systems the ls command produces color output, which makes it easy to distinguish different file types. However, the directories are dark blue which makes it nearly impossible to see against the black background.

My Solution

To fix the problem, my research took me to the LS_COLORS environment variable and a command named dircolors. The solution is to set the LS_COLORS environment variable to control how the ls command chooses its color. I can set the LS_COLORS variable manually, but there is a better way which employes the dircolors command.

The dircolors command output commands to set the LS_COLORS environment. It also output the colors in human-readable format, allowing easy modifications.

To use the dircolors, the first step is to save the current settings into a file. From the terminal, I issued one of the following commands:

    dircolors -p > ~/dircolors.txt

The next step is to edit the file ~/dircolors.txt. This file’s format is easy to understand and self-documented; I had no problem finding the file that begins with “DIR” and change the color to my taste.

Next, I try out the new color scheme:

    eval $( dircolors -b ~/dircolors.txt ); ls # bash shell syntax
    eval `dircolors -c ~/dircolors.txt`; ls # C shell syntax

After finding the color scheme I liked, I saved it to my start up file:

    dircolors -b ~/dircolors.txt >> ~/.profile # bash shell
    dircolors -c ~/dircolors.txt >> ~/.cshrc   # C shell

From this point on, I no longer have to put up with hard-to-see colors.

Solution for BSD Systems

On BSD systems, which includes the Mac OS X, the variable in question is LSCOLOR (note the lack of the underscore character). The format of this variable is different from the Linux’s LS_COLORS. The default is exfxcxdxbxegedabagacad. The value of this variable consists of pairs of characters; the first character is the code for foreground color, and the second is for background. Please consult the man page for ls on BSD for more information.

The Interactive Solution

While I like to do things the hard way to learn more about the inner-working of the OS, there is a more interactive way: point your browser here to an interactive online web application which assist you in setting the colors. After finding your desired color scheme, you still have to cut and paste it to your start up file.

Additional References

Google is your friend: do a search for LSCOLORS or LS_COLORS will result in more information that you ever care to read.

Else If in Popular Programming Languages

Problem Statement

I write program in many languages and the one thing I keep forgetting is how to spell “else if”. OK, stop laughing and think about it: each language spells “else if” differently: else if, elseif, elsif, or even elif.


To put my mind to rest, I create this list which serves as a quick look-up for me. I am sure that someone else might find this useful.

The Languages

Language Keywords Notes
AWK if, else if, else Just like C/C++.
bash if, then, elif, else, fi The fi keyword ends the if construct.
C/C++/C# if, else if, else Technically, these languages does not have the “else if” keyword, but the effect is the same.
Java/Javascript if, else if, else See note for C
Perl if, elsif, else
Python if, elif, else
Tcl if, elseif, else
Visual Basic If, Then, ElseIf, Else, End If Not case sensitive, but the IDE usually spells as presented

Debugging Tcl script with puts

Using puts is the one of the oldest, yet effective debugging technique. For example:

puts "before calling myproc"
puts "myvar = $myvar"
myproc myvar
puts "after calling myproc"
puts "myvar = $myvar"

This technique is good, but it has at least two problems:

  1. To turn off debugging output, I either have to delete or comment out tens if not hundreds of puts lines like those above. To turn them back on, I have to reverse the process for that many lines.
  2. This is not a problem, but I found myself using puts to display value of a variable quite often, therefore it would be nice if I can streamline that process

To answer both questions, I created the following code snippet which I paste at the beginning of my script:

# Output debug only when the user set the environment variable
# DEBUG=1. In Bash:
#     $ export DEBUG=1
# In csh and tclsh:
#     # setenv DEBUG 1
if {[info exists env(DEBUG)] && $env(DEBUG)} {
    proc dbg {msg} { puts $msg }
    proc dbgVar {varName} {
        upvar 1 $varName varValue
        dbg "$varName = '$varValue'"
} else {
    proc dbg {msg} {}
    proc dbgVar {varName} {}

Now, the original block of code would become:

dbg "before calling myproc"
dbg "myvar = $myvar"
myproc myvar; # Now call myproc, which modifies myvar
dbg "after calling myproc"
dbgVar myvar

Here is the instruction for turning debuggin on or off:

	$ export DEBUG=1 # Debug on  for sh and bash
	$ unset DEBUG    # Debug off for sh and bash
	# setenv DEBUG 1 # Debug on  for csh and tcsh
	# unsetenv DEBUG # Debug off for csh and tcsh 

Now, I don’t have to go over my code to comment or uncomment lines after lines of code to turn on/off debugging output. I also have at my disposal a simple way to display the value of a variable. Life is good.

In the future, I will probably put this snippet into a package to further streamline the process.

Boolean logic in Tcl

Many people who write Tcl script get confused when it comes to boolean logic, especially within if, while construct. To clear up some of these confusions, let’s take a look at the following interactive session:

	(tcl) 66 % # What is true?
	(tcl) 66 % set var -1
	(tcl) 67 % string is true $var
	(tcl) 68 % # So, -1 is not considered a token for true. Let see now:
	(tcl) 68 % if {$var} {puts "$var is true"}
	-1 is true
	(tcl) 69 % # It seems the expression in the if statement only care if var is zero or not
	(tcl) 69 % # Try a couple of other tokens:
	(tcl) 69 % string is true yes
	(tcl) 70 % string is true no
	(tcl) 71 % string is false no
	(tcl) 72 % # So yes=true, no=false, so far so good
	(tcl) 72 % set var yes
	(tcl) 73 % if {$var} {puts "$var is true"}
	yes is true
	(tcl) 74 % set var neither
	(tcl) 75 % if {$var} {puts "$var is true"}
	expected boolean value but got "neither"
	(tcl) 76 % # Now that is the behavior I expected
	(tcl) 76 % set var no
	(tcl) 77 % if {!$var} {puts "$var is false"}
	no is false
	(tcl) 78 % # Worked as expected
	(tcl) 78 %

Normally, Tcl defines true as 1 and false as 0, but as line 68 shows, Tcl considers any non-zero numerical value as true, not just 1.

Lines 69-77: Tcl’s boolean vocabulary also extends to other tokens such as true/false, y/n, yes/no, and on/off. These tokens are case insensitive, which means Yes, yes, and YES are the same.

Line 74-75: If Tcl does not understand a token, it will flag as an error.

Here are the summary points:

  1. For numeric values 0=false and non-zero=true
  2. For non numeric values, Tcl understands true/false, yes/no, y/n, and on/off. The language might understand more tokens than that, but those are the ones I tested.
  3. Anything other than 1 and 2 will raise an error

Email Settings for Popular Email Services

I often need to look up information to setup my mail clients such as POP3, IMAP, or SMTP servers and ports. Today, I decided to create a spreadsheet with these information which should help me and others. Feel free to edit the spreadsheet to include other services such as comcast, verizon, …

Here is the URL to the spreadsheet: