Wednesday, January 18, 2012

Inserting single quote character inside a single-quoted string

“Strings” (not the same as strings in languages like C or Python) in shells (bash and zsh especially) can be enclosed within either single quotes (') or double quotes (").  Double quotes escape (i.e. remove special meaning from) characters like space and single quotes, but single quotes remove meaning from almost every special character including spaces, $, !, new lines, etc.  (Learn more)

You can use \ escape to insert a double quote character (") inside a double-quoted string, but you cannot do the same for inserting a single quote character (') inside a single-quoted string.  Because \ has no special meaning inside a single-quoted string.
% echo "abc \" def"
abc " def
% echo 'abc \' def'                   
quote> blah'
abc \ def
blah
%
But I often find myself needing to insert apostrophes inside single-quoted strings.  I use the following syntax then:
% echo 'There'\''s always a way out'
There's always a way out
Shells usually let you concatenate strings by just writing them together without any space between them: "abc""def" is the same as "abcdef" which is the same as "abc"'def' which is the same as abcdef (without quotes).  We use the same technique here: we have two different single-quoted strings: 'There' and 's always a way out'.  In between them we have a single-quote character escaped with \ to mean that we want a literal apostrophe character inserted.

Saturday, January 14, 2012

Running Ubuntu on a Toshiba Z830

I bought a Toshiba Z830 recently.  This post is to document how I am running Kubuntu on it.  First, let me show you how fast it boots :)

  1. The machine has only 128GB storage, which cannot be upgraded yet, which means having both Windows and Linux on the machine may not be realistic.  But there’s a way to back up Windows!  Toshiba bundles a program with the computer that creates recovery USB drives for the Z830.  I bought a new 16GB USB drive and made it a recovery drive.  The plan is to use this for restoring Windows when I am giving this laptop away to somebody in future.
  2. I downloaded 64-bit Kubuntu 11.10 installer from kubuntu.org.  Since I have a backup copy of Windows, I removed all NTFS and recovery partitions from the disk and created new partitions for installing Kubuntu.
  3. After the installation, almost everything worked out of the box.  I had no issues with the laptop sleeping or waking up; 3D graphics worked fine; audio, bluetooth, webcam... everything worked.  Except the following one issue.
  4. Screen brightness controls sometimes worked, but sometimes they didn’t.  The technique that helped in 2008 helped me this time too, except I needed to make a minor modification to the program: the file that required changing was /sys/class/backlight/intel_backlight/brightness (i.e. intel_backlight instead of acpi_video0).  And the brightness value seem to be in the range of 100 through 4400.
  5. Battery life was bad.  More worrying than battery life was the heat generated by the laptop.  I found some tips to reduce power usage on Putokaz blog, and tried it (thank you, voloder!).  While it did reduce power consumption, it also resulted in some screen update issues.  After some trial and error, I found that frame buffer compression was causing issues, so I left that one out.
    Update: Kubuntu 12.04 provides a very good battery life out of the box.
  6. ZDNet recommends disabling “elevator” I/O scheduler by passing elevator=noop to kernel.  Based on tips from both sources, I have updated my /etc/default/grub to have this line:
    GRUB_CMDLINE_LINUX_DEFAULT="quiet splash elevator=noop"
    GRUB_CMDLINE_LINUX_DEFAULT="quiet splash pcie_aspm=force i915.i915_enable_rc6=1 i915.lvds_downclock=1 elevator=noop"
    (Power management flags may work on other computers with the same Core i5-2467M Sandy Bridge processor as well.)
  7. Based on the same ZDNet story mentioned before, I updated my /etc/fstab as well.  I added noatime flag to my ext4 partitions and mounted /tmp on RAM using tmpfs.  (I didn’t change the caching parameters as that sounds a bit scary.)
    UUID=dbccce1d-8e31-428e-a25a-227afc54651f /      ext4  noatime  0  1
    UUID=720c576c-2c1a-435f-ae90-9151acda2005 /home  ext4  noatime  0  2
    tmpfs /tmp tmpfs nodev,nosuid,noexec,size=1G 0 0
    
Update: Someone was asking about battery life, so here's a screenshot of expected battery life.  With Ubuntu 12.04, you'd get more than 5 hours of battery life on a full charge.

Saturday, January 7, 2012

Partitioning disk for installing Linux

Are you installing Ubuntu on a new machine and you’re not sure how much storage space the installation would need or how to partition your disk?  I have a few suggestions:
  • Create separate partitions for Linux installation (mounted at /) and data (mounted at /home).  This way, you can completely wipe and reinstall your OS or switch to a different distro without worrying about data in your home directory.
  • A typical Linux installation doesn’t need a lot of space.  I allocated 20GB for my root (/) partition purely based on some guess work, but I’m using only about 7.2GB in that partition.  If I’m starting over again, I’d go with a 12 or even a 10GB partition.  (You’re a lot more likely to eventually run out of space on your home partition than on the root partition.)
    % df -hl
    Filesystem            Size  Used Avail Use% Mounted on
    /dev/sda1              19G  7.2G   11G  40% /
    udev                  2.9G  4.0K  2.9G   1% /dev
    tmpfs                 1.2G  996K  1.2G   1% /run
    none                  5.0M     0  5.0M   0% /run/lock
    none                  2.9G  2.8M  2.9G   1% /run/shm
    /dev/sda6              93G   78G   11G  89% /home