Go to content Go to navigation Go to search

Xubuntu 8.10 -- It's Coming · 24 days ago by Dylan Doxey

Countdown to Xubuntu (xubuntu.org) 8.10, by Pasi Lallinaho

Getting Your Date Straight · 33 days ago by Dylan Doxey

It's been bugging me for a while, that my server from System76 was on the Devner Colorado time zone. So, I just set the time ahead by an hour using the date command.

This is simple enough to do:

dylan@dev.doxey.org$: sudo date 1018114508
Sat Oct 18 11:45:00 MDT 2008

But that "MDT" will always remind me, I was taking a shortcut and not bothering to do it the right way.

So, the right way is to read the System76 "Initial Server Setup" page... all the way to the end.


Doing that, I learned that the convenient way to choose your time zone is via an ANSI GUI interface invoked via:

sudo dpkg-reconfigure tzdata

sudo dpkg-reconfigure tzdata


Once you've got your time zone set appropriately, perhaps you'd like to sync up your time with an authoritative clock.
ntpdate is your handle to the Network Time Protocol.

sudo ntpdate us.pool.ntp.org

I chose us.pool.ntp.org from a list of North American time servers I found here http://www.pool.ntp.org/zone/north-america.


Happy computing!

Customizing Your Prompt · 39 days ago by Dylan Doxey

So, you're on your system messing about, and then you realize, "Hey, I thought this was the development machine!" Yes, it happens. You're fiddling about with thing you shouldn't have on your production box.

I thought it would be nice if my terminal would change to a different profile depending on what machine I've initiated an SSH session with. This is certainly feasible, if you're a pretty good C++ programmer, and you feel like spending some time modifying your terminal application. This is not me.

Instead I thought I'd looking into setting up a custom prompt.
By default the prompts on all the machines I work with are like:

dylan@dev: /home/dylan$

So, what's the problem? The machine name is always right there.

I want more.

Here's a nice IBM article on how to use ANSI colors in your prompt.
http://www.ibm.com/developerworks/linux/library/l-tip-prompt/


Here's what I've come up with for myself.

$ export PS1="\[\e[32;1m\]\u@\H\$: \[\e[37;1m\]\w \[\e[0m\]"
dylan@dev.doxey.org$: /usr/home

$ export PS1="\[\e[33;1m\]\u@\H\$: \[\e[37;1m\]\w \[\e[0m\]"
dylan@qa.doxey.org$: /usr/home

$ export PS1="\[\e[31;1m\]\u@\H\$: \[\e[37;1m\]\w \[\e[0m\]"
dylan@prod.doxey.org$: /usr/home



Happy computing.

Mass Search & Replace · 44 days ago by Dylan Doxey

So, there you are with a bunch of files. You want to search and replace a given string in all of them, and you're not sure how to go about it. If you're a former windows user, such as myself, you might be inclined to reach for the nearest GUI editor, open all of the files simultaneously, and do a search and replace on all the files that way. However, I've yet to see a GUI based editor that runs on Linux which can handle this sort of task the way that UltraEdit would.

Perl to the rescue!

Here's an example of replacing "searchme" with "replacement" in all the PHP files in the current directory.

perl -pi -e 's{searchme}{replacement}xmsg' *.php


The beauty is that you just need to supply Perl with a list of files. For example:

perl -pi -e 's{searchme}{replacement}xmsg' `find . -name 'holey*moley*'`

That does the search and replace on all files in the current, and subdirectories of the current directory where the filename contains "holey" and "moley" in that sequence.


May the force be with you.

Your .vimrc and You · 123 days ago by Dylan Doxey

There you are, opening up a file in vim and you realize, "Oh, I've never used vim on this particular machine before." It can be an interesting challenge trying to enter your favorite settings all from memory. But I usually find this to be a time consuming distraction. The next time this occurs, I'm just going to cut & paste the following...

syntax on
let perl_fold = 1
let perl_fold_blocks = 1
set number                      

inoremap # X^H#
inoremap # X<C-H>#
set title            
set background=dark  

set autoindent

set backspace=indent,eol,start

set tabstop=4                  
set expandtab                  
set shiftwidth=4               
set shiftround                 

set matchpairs+=<:>            
nmap <C-right> zo
nmap <C-left> zc
nmap <C-S-left> zM
nmap <C-S-right> zR
nmap <F6> :%s/ \+$// <CR>
nmap <F5> :w<CR>:! perl -Tcw %<CR>

set pastetoggle=<F12>

Your .vimrc lives at:

~/.vimrc

UTF-8 Character Encoding · 198 days ago by Dylan Doxey

UTF-8 character encoding? What does it all mean?!?!

Question: Why does the character "é" sometimes get corrupted into "é"?

Explanation:

UTF-8 binary representation requires a format such as:

        0xxxxxxx -- seven bit characters (ASCII/Unicode values 0 through 127) 
        110xxxxx 10xxxxxx -- 110 indicates a two byte representation, 10 indicates greater than seven bit character 
        1110xxxx 10xxxxxx 10xxxxxx -- 1110 indicates a three byte representation 
        11110xxx 10xxxxxx 10xxxxxx 10xxxxxx -- 11110 indicates a four byte representation 

Therefore:

        é == 233 (ASCII) == 11101001 (binary) 

The UTF-8 representation for:

        11101001 -> (11)(101001) -> [110]000(11),[10](101001) -> 11000011,10101001 

Where the pattern [110]xxxxx,[10]xxxxxx indicates a two byte character with up to eleven bits of data.

Therefore a program unknowingly processing UTF-8 encoded text will mistakenly interpret every 8 bits as a straight forward character encoding.
Specifically:

        Non UTF-8 interpretation of 11000011,10101001 is 11000011 & 10101001 -> Ã & ©. 
        UTF-8 interpretation of 11000011,10101001 is [110]00011,[10]101001 -> 00011101001 -> é. 

Question: What happens to UTF-8 characters in URLs?

The browser will hex encode the string and as a security feature will generally leave it hex encoded on the address bar of your browser. (To prevent malicious web developers from setting up a website on wellsfargo.com spelled with Cyrillic characters, for example.)

UTF-8 strings are easy to spot on your address bar because of the distinctive encoding patter of UTF-8.

        110xxxxx 10xxxxxx                   => [C-D][0-F] [8-B][0-F] 
        1110xxxx 10xxxxxx 10xxxxxx          => E[0-F] [8-B][0-F] [8-B][0-F] 
        11110xxx 10xxxxxx 10xxxxxx 10xxxxxx => F[0-7] [8-B][0-F] [8-B][0-F] [8-B][0-F] 

Consider the URL: http://yukistore.com/日立

This will probably appear in your browser as: http://yukistore.com/%E6%97%A5%E7%AB%8B
That's because this website is UTF-8 as declared in the charset meta tag in the HTML header.

This is clearly a pair of three byte UTF-8 unicode characters because it matches the pattern:

E[0-F] [8-B][0-F] [8-B][0-F]E[0-F] [8-B][0-F] [8-B][0-F]

In Perl you might write:

$url =~ m/ %E[0-9A-F] %[89AB][0-9A-F] %[89AB][0-9A-F] /msx;


Obviously!

Watts Up? · 246 days ago by Dylan Doxey

Ever wonder how much power your computer consumes? The "Watts Up?" power meter can clue you in.

With gas prices going up so dramatically in recent years, I'm beginning to develop a new sensitivity to my energy consumption habits. For example the desktop computer which I used to think of as my Ferrari among computers is now starting to feel more like a Humvee.

Last week I borrowed the "Watts Up?" from the office to see exactly where I stand.


wattsup

Getting started, I plugged the unit into the wall and verified that plugging nothing in doesn't appear to consume any energy.


wattsup

My desktop system is powered via an APC UPS. For this test I limited the items connected to the UPS to be:

Plugging my APC UPS into the unit registered an immediate drain of 1 watt. After a few minutes it climbed to 5 watts. The energy consumption rate continued to climb minute by minute.


wattsup

After about four hours the unit began to indicate some real power draw. These results suggest that after unplugging the UPS it detected a crisis and began supplying energy to my system, and continued to do so in a low-trust mode even after power had been restored. (These results are not what I had expected. I would have predicted that the UPS would draw the enough to supply the system's requirements and more to replenish the reserves drained during the apparent crisis.)


wattsup

When I was ready for the real deal, I shut everything down and removed the UPS from the equation. Using just a regular ol' power strip, I reconnected all of the above mentioned equipment and fired it up again. Wow! Now that's more like what I expected. But not entirely. With a 500 watt power supply, I and a dual core processor known for running hot, and all the other stuff, I expected to see something in the neighborhood of 400+ watts consumption. And that also includes the two monitors, speakers and the printer. Interesting...


wattsup

Then I shutdown the system with the OS shutdown mechanism. This reading indicates the LaserJet 1100, speakers, and the two BenQ monitors in standby mode.


wattsup

Physically unplugging the LasterJet 1100 dropped my consumption by four watts. This is something I'd never thought about because I've just left that thing plugged in 24/7 for the last three years. A crude calculation: three years at four watts continuous consumption is 105.12 kilowatt hours which translates to 157.68 pounds of carbon emission. (According to the 1.5 lb per kwh proposed by the Vermont Earth Institute worksheet.)


Setup: cpan · 313 days ago by Dylan Doxey

Fresh System

Every once in a while I find myself starting up cpan on a fresh machine. The first thing cpan wants to do is refer to its configuration module for information about its dependent utilities such as lynx and ncftpget.

The the configuration module is located at: ~/.cpan/CPAN/MyConfig.pm
Or in some cases: /etc/perl/CPAN/Config.pm

In this module you'll find the definition of a hash ref called $CPAN::Config. Clearly cpan intends to 'do' this module to become aware of where its favorite binaries are located. Among other things, it's specifically interested in:

  'ftp' => q[/usr/bin/ftp],
  'gpg' => q[/usr/bin/gpg],
  'gzip' => q[/bin/gzip],
  'lynx' => q[/usr/bin/lynx],
  'make' => q[/usr/bin/make],
  'ncftp' => q[/usr/bin/ncftp],
  'tar' => q[/bin/tar],
  'unzip' => q[/usr/bin/unzip],
  'wget' => q[/usr/bin/wget],

So, to make the cpan setup process go as quickly as possible you might install any missing dependencies first. For my typical (X)Ubuntu systems, I have only needed:

sudo apt-get install lynx
sudo apt-get install ncftp

Because your mileage may vary, here's a complete list:

sudo apt-get install ftp
sudo apt-get install getcwd
sudo apt-get install gpg
sudo apt-get install gzip
sudo apt-get install lynx
sudo apt-get install make
sudo apt-get install ncftp
sudo apt-get install tar
sudo apt-get install unzip
sudo apt-get install wget

It's also worth noting at this point that if your going to use cpan much, then you will inevitably see the message "install seems impossible" when you're attempting to install some module. The first few times I saw this, I ranted quite a bit regarding the sense of saying something is "impossible" to install from source, and then found some non-cpan or non-Perl workaround.

On [X]?Ubuntu systems you can resolve this by installing the build-essentials package. Looking into the .deb file I found a list of the "essential packages", which are essential for compiling Perl modules.

essential-packages-list

base-files
base-passwd
bash
bsdutils
coreutils
debianutils
diff
dpkg
e2fsprogs
findutils
grep
gzip
hostname
login
mktemp
mount
ncurses-base
ncurses-bin
perl-base
sed
sysvinit
tar
util-linux

Just for the record, you install build-essentials like this:

sudo apt-get install build-essentials

UPDATE: You might need to specify build-essential (singular case) like this:

sudo apt-get install build-essential

How much does it cost to drive a Jeep? · 315 days ago by Dylan Doxey

Driving A Jeep
       22  miles one way to work
      x 2  each way to work
  -------
       44  miles a day
     \ 15  miles for each gallon of gas
  -------
     2.93  gallons each day
  x $3.20  each gallon
 --------
    $9.38  to drive to work
      x 4  days a week ( minus telecommute day )
 --------
   $37.50  each week
      x 4  weeks in a month
 --------
  $150.01  each month for gasoline
     x 12  
 --------
$1,800.19 annually 

Multi-line Strings · 337 days ago by Dylan Doxey

So, there you are, writing a script which sends an email to each of the staff wishing them each a nice day. To ensure that you are having a nice day also, you want your code to look nice and be maintainable by the next guy that comes along to change, debug or fix it later.


my $name = 'Mr. Pibb';

my $message = <<TEMPLATE;
Dear $name,

please have a nice day.

Thank you.

Sincerly, the Management.
TEMPLATE

Well, here's the thing... I personally don't like that syntax for doing multi-line strings. Yet of all the languages I've worked with, this solution offered by Perl seems to be the best thing going. (There is the possibility of keeping the text of the message in a template file to be read an processed without mucking up your code layout. But that's for a different post.)

Pros: Cons:

So, you might be inclined to ask what I'd suggest as an alternative.

my $message = "Dear $name,\n\nplease have a nice day.\n\nThank you.\n\nSincerly, the Management.";

This is the obvious choice.

Pros: Cons:

Let's try breaking it up to make the individual lines stand out.

my $message = "Dear $name,\n\n"
    . "please have a nice day.\n\n"
    . "Thank you.\n\n"
    . "Sincerly, the Management.";

Here's a big improvement over just jamming it all together in a single line.

Pros: Cons:

Let's try something that will better suggest the rectangularity of the intended result.

my $message = "Dear $name,"             . "\n"
            . ""                        . "\n"
            . "please have a nice day." . "\n"
            . ""                        . "\n"
            . "Thank you."              . "\n"
            . ""                        . "\n"
            . "Sincerly, the Management.";

Here's getting a little closer to my vision for a clean multi-line string definition.

Pros: Cons:

How about something more structured than a sequence of concatenated strings.

my $message = join "\n", (
    "Dear $name,               ",
    "                          ",
    "please have a nice day.   ",
    "                          ",
    "Thank you.                ",
    "                          ",
    "Sincerly, the Management. ",
);

Whoa, now we're getting somewhere. This gives us a strong sense of the shape and layout of the target document without compromising our code layout at all.

Pros: Cons:
my $message = join "\n", map { $_ =~ s/(\A [\s]* | [\s]* \z)//msxg } (
    "Dear $name,               ",
    "                          ",
    "please have a nice day.   ",
    "                          ",
    "Thank you.                ",
    "                          ",
    "Sincerly, the Management. ",
);

Nice.

Pros: Cons:
my $message = multiline_string(
    "Dear $name,               ",
    "                          ",
    "please have a nice day.   ",
    "                          ",
    "Thank you.                ",
    "                          ",
    "Sincerly, the Management. ",
);

sub multiline_string {
    return join "\n", map { $_ =~ s/(\A [\s]* | [\s]* \z)//msxg } @_;
}

Better.

Pros: Cons:
my $message =
    +----------------------------+
    | Dear $name,                |
    |                            |
    | please have a nice day.    |
    |                            |
    | Thank you.                 |
    |                            |
    | Sincerly, the Management.  |
    +----------------------------+
;

Now you're talking!
This is a snippet out of my fantasy language.

Pros: Cons:
my $message =
    +----------------------------+
    | Dear $name,                
    |                            
    | please have a nice day.    
    |                            
    | Thank you.                 
    |                            
    | Sincerly, the Management.  
    +----------------------------+
;

Perhaps this variation could suggest no trailing white-space.

Comment

Previous