Saturday, October 24, 2009

why I don't write BASH scripts

I'm constantly hounded by GNU/Linux newbies GNUbies about many things relative to scripting; Why don't I use gawk for my awk scripts; Why don't I use '[[' instead of '[' for my condition tests in shell scripts; Why don't I use BASH for my shell scripts?. Such questions rarely come from seasoned UNIX professionals who have worked on multiple flavors of Unix. Explaining the way things work in the real world requires that the audience has an attention span beyond that of the average I'm_pissed_at_society_and_microsoft_sucks-linux-neophyte.

Clarification:


I'm not picking on Linux here. While I'm a BSD guy (B+++(++++)). I greatly understand and respect the contributions made by the GNU/Linux community from which we all benefit. There are many religious wars to be fought in computing and I'm not, here, interested in fueling any such conflicts. The bottom line is that due to its popularity, there are plenty of idiots who think they know everything using Linux. A good and smart linux user is an asset to the unix community as a whole.

The Reasons


It's not installed on my system(s)


While I've been using Linux since ~ 1994, I don't consider myself a "Linux Guy". Its a matter of preference, not experience. I simply prefer to use other operating systems over most linux distributions. As a result its quite frequently the case that BASH is not installed by default. Such is the case for FreeBSD and OpenBSD.

The same could be said of any "enhanced" shell. KSH93 and ZSH are also not installed by default on those systems. All three are available on my Mac, by default.

It didn't do what I wanted it to do


I've been using the korn shell for well over 15 years. When I wanted features beyond the POSIX standard for shell scripting, KSH93 had them. For at least the past 10 years I've been able to do things that are just now available in BASH 4.0. It makes no sense to me to switch to a much less mature code base just to get the same features that are available in the software I've been using for 10+ years.

It still doesn't do what I want it to do


There was much hype surrounding the release of BASH 4.0. Were I a Linux (only) user I'd, likely, have been right there with the crowd cheering the newest version of the shell that still doesn't do everything I can do with my other shells.

To be fair BASH 4.0 and ZSH do have one feature that KSH93 does not: the ability to generate sequences ({001..199}) with leading zeroes.

Poorformance


Bash just doesn't stack up when it comes to performance. The performance of KSH93 is comparable to that of Perl and Python. The performance of BASH (and many other shells like ZSH, MKSH, OKSH, POSH, ASH etc..) is comparatively poor.

Here is a mandelbrot script that I found online somewhere :
inmandelbrot() {
let "mag = $1 * $1 + $2 * $2"
if [ $mag -gt "40000" ] || [ $5 -ge $6 ]; then
echo $5
else
let "r = ($1 * $1)/100 - ($2 * $2)/100 + $3"
let "i = ($1 * $2)/100 * 2 + $4"
let "cnt = $5 + 1"
inmandelbrot r i $3 $4 $cnt $6
fi
}

for y in {-20..20}; do
for x in {-20..20}; do
let "rval = x * 10"
let "ival = y * 10"
val=$(inmandelbrot rval ival rval ival 1 10)
if [ $val -eq 10 ]; then
echo -n ".";
else
echo -n $val;
fi
done
echo
done

I modified it use the {x..y} sequence instead of calling seq(1) as seq is not readily available on non-gnu systems and it improves execution time. The results were pretty stark ksh93 ran the set in under a second while BASH took 6 seconds on my laptop.
[gcw@gcwmbp:~/prog/fractals>
$ time ksh ./mandelbrot.sh > /dev/null

real 0m0.782s
user 0m0.640s
sys 0m0.016s

$ time bash ./mandelbrot.sh > /dev/null

real 0m7.204s
user 0m1.791s
sys 0m2.207s

In (at least) this case KSH93 out performs BASH by roughly 9 times. In the spirit of full disclosure I should note that MKSH and ZSH don't fare much better than BASH when this type of Mandlebrot set. I got times around 90 - 95 percent of those for BASH.

POSIX -> KSH93 -> ZSH


In most of my scripting I try to stick to the POSIX standard for features and tools. I try to avoid using GNU/Linux (only) utilities to make everything as portable as possible. When I do venture beyond the borders of POSIX-Land, I go to KSH93, I've been doing so for so long that it doesn't make sense for me to do anything else, unless... well you see... there is ZSH. The things you can do with ZSH go pretty far beyond standard "shell scripting". While it doesn't have the performance of KSH93 it does do much more than BASH and other "standard" shells and still manages to out perform them (in many cases).

ZSH does have it's peculiarities. It's close to the other shells at the basic level and then diverges significantly from there.

In closing


Yes BASH is a big a bloated piece of useful software, so too are KSH93 and ZSH. When I started scripting in Korn Shell it was because that's what was available to me at the time. Fortunately, KSH93 has some big advantages over the competition when it comes to features and performance. ZSH has it mainly in features. Because I use both of these shell there's really no reason for me to bother with BASH. If you're a Linux only type of person, you may be blissfully unaware that other shells exist and might not care at all that more power is out there just waiting to be wielded by the likes of you. That's fine. I'm not looking to discourage any would be BASH-Hackers. I'm just trying to get wannabe's off my back for not being one myself.

Monday, October 19, 2009

15" MacBook Pro battery is failing after just 9.5 months (damn you Apple)

I'm not an Apple loyalist. I don't think they make either the best or the most innovative products. I do like that they've managed to do certain things that other vendors haven't been apt or adept enough to do. Yes; Apple does incessantly boast about the most minute of accomplishments. This makes it difficult for the layperson to determine which of their marketing fluff points is actually worth all of the hype. Hype is what Apple wants   you to be interested in. They don't even hide it (well).

Recently Apple's been hyping their Laptops and the great battery life. Well today After 9.5 months the battery in my (new Aluminum Uni-Body) 15" MacBook Pro is crashing. By crashing I mean that it's only lasting for about 1/3 of the advertised 5 - 6 hours of  life on a full charge.

It started last week when I'd noticed on the train (during my morning commute) that my battery meter was atypically low. When I got to the office I plugged in my laptop and left it charging for about 9 hours straight. At the end of the work day I left, hoped on the train and noticed that the estimated life on the meter was 1 hour 47 minutes. I decided to deal with it for the rest of the week. On Friday I let the battery drain and did the calibration they recommend on the apple support site. Saturday (after the 8 - 9 hour process) I got the same results as before 1 hour 47 minutes of battery life. So I decided to try it again. No go, still only 1 hour 47 minutes. Now I can either choose to either try to get by online 1 hour 47 minutes at a time or fork over $130 for a new battery hoping that it will live up to the hype.