Binary?!

So, my son asked me yesterday about how computers work, and we finally got to binary, which he had heard about in school. Then he asks me how numbers look like in binary, and stuff like that. Instead of looking them up, I came up with a perl script to show him any number he would want to see in binary. It’s nothing earth-shattering, but I thought someone may want get a kick out of it, and it really shows how powerful perl is. So, here goes:

#!/usr/bin/perl -w

use strict;

# Get the number from the command line or use default.
my $number = shift || 42;

printf "%b\n", $number;

bash prompts….

…I use.

My regular bash PS1:
export PS1='\[\e[0;32m\]\u\[\e[m\] \[\e[1;34m\]\w\[\e[m\] \[\e[1;32m\]\$\[\e[m\] \[\e[1;37m\]'

My “fancy” bash PS1 for Linux:
export PROMPT_COMMAND='history -a;echo -en "33[m33[38;5;2m"$(( `sed -n "s/MemFree:[\t ]\+\([0-9]\+\) kB/\1/p" /proc/meminfo`/1024))"33[38;5;22m/"$((`sed -n "s/MemTotal:[\t ]\+\([0-9]\+\) kB/\1/Ip" /proc/meminfo`/1024 ))MB"\t33[m33[38;5;55m$(< /proc/loadavg)33[m"'
export PS1='\[\e[m\n\e[1;30m\][$$:$PPID \j:\!\[\e[1;30m\]]\[\e[0;36m\] \T \d \[\e[1;30m\][\[\e[1;34m\]\u@\H\[\e[1;30m\]:\[\e[0;37m\]${SSH_TTY} \[\e[0;32m\]+${SHLVL}\[\e[1;30m\]] \[\e[1;37m\]\w\[\e[0;37m\] \n($SHLVL:\!)\$ '

Processes by uid in perl.

This is just a quick hack to find which uid owns how many processes on your system. Obviusly, this can be altered to just about anything, but it’s a good example of how perl can use Unix commands inside a script.

#!/usr/bin/perl -w

# Who owns how many processes

@pslist = qx/ps -ef/;

# Remove the header line
shift @pslist;

foreach $line (@pslist)
{
@splitline = split(" ",$line);
# Count usernames
$count{$splitline[0]}++;
}

foreach $user (sort keys (%count))
{
print "User $user owns $count{$user} processes \n";
}

Mirroring a ZFS root pool.

A quick guide to mirror a ZFS root pool:

1. Attach the new device to your root pool:
# zpool attach

2. Install the bootblock on the new device:
Linux:
# installgrub /boot/grub/stage1 /boot/grub/stage2

Solaris:
# installboot -F zfs /usr/platform/`uname -i`/lib/fs/zfs/bootblk

3. Make sure your OS of choice knows about this. On Sparc you need to add the device to the OBP variable “boot-device”, and on x86 to the BIOS settings.