Monday, October 27, 2008

Awk One Line Command to Set Unix Xterm Variable

A co-worker was trying to run her X windows server on her laptop, and then run an xterm window from a unix box.

She used to be able to do it, but something changed and the IP address she hard-coded in her display setting is not working. It looks like her VPN (Virtual Private Network) keeps changing her laptop's IP address.

So, I figured how to look up the address dynamically.

For example, instead of running:

xterm -display 192.184.90.168:0.0

I had her run:

xterm -display `who -m|awk '{print substr($NF,2,length($NF)-2)}'`:0.0

Let's analyze this. First, we are running the who -m command. It has an output like:

user pts/4 Mar 20 09:31 (192.184.90.168)

We pipe it to awk. With awk, $NF is the last field: (192.184.90.168)

We need to strip off the parenthesis. So, we use the substring function to choose the substring that starts with the second character, and is 2 characters shorter than the total string.

So, for example, (192.184.90.168) is 16 characters long. So, by selecting the substring that starts with position 2 and is 14 characters long, we return 192.184.90.168.

0 comments: