2004-12-02

I run 3, count them, 3 shells in MS Windows

Very important. Mustn't forget.

Today I ran across a short .inf file that will automatically add the option to open a MSH prompt in the directory of your choice. Neat stuff, especially since the DosHere powertoy has been around since Windows 95 and I have lived by it every damn day for almost the entire time.

So it got me to thinking. Why have I never been able to get this working for ksh?

Microsoft Windows runs a seldom-used POSIX subsystem to support some free software that the company has purchased from Interix: "Services For UNIX". In truth, this is completely ass-backwards, as it's actually UNIX software for Windows. I guess "SFU" has much nicer connotations that "USW".

The SFU ksh is a poor implementation of a Korn shell, yet, being a Korn shell, it's still pretty nice as far as shells go. There are a great many things that /bin/ksh can do on a Windows system that cmd.exe can't. Sure, sure. Microsoft is going to swear up and down that MSH will fix everything. And maybe it will. But new is different, and different is strange. ksh is old and familiar and oh so powerful. For the longest time, I could never finesse a Korn shell to have the ease-of-use of the DosHere powertoy.

Until now.

I reinvestigated the problem with one simple tenet: it must be possible to do. After accepting this idea as gospel truth, I started breaking ksh features a little at a time until, like Edison inventing the light bulb, I'd learned enough about what didn't work to make a functional fix.

I had gotten exactly this far many moons ago and stopped.

DosHere creates a registry key:

 Key: HKLM\SOFTWARE\Classes\Directory\shell\DosHere\command
Type: REG_SZ
Data: C:\WINDOWS\system32\cmd.exe /k cd "%1"

I'd created the following:

 Key: HKLM\SOFTWARE\Classes\Directory\shell\KshHere\command
Type: REG_SZ
Data: cmd /c "cd %1 & posix /u /c /bin/ksh -l"

Scratching my four-day stubble, I could never get beyond this point. I just couldn't figure out how to tell ksh not to change its directory (to $HOME, natch) and, simultaneously, run the /etc/profile script needed to convert pathnames from "C:\WINDOWS\" to "/dev/fs/C/WINDOWS/".

It was practically by accident that I found out that a simple one-line edit would fix the problem:

  1. Open a normal ksh window, either by clicking the Korn Shell program in "Start > Programs > Windows Services for UNIX" or by running "posix /u /c /bin/ksh -l" from a command prompt.

  2. vi /etc/profile

  3. Edit line 250 from cd "$HOME" to cd "$PWD".

Now save the file, close the ksh window, and test the KshHere shortcut. You're done. It's so easy, I feel shame for having lived so long without this feature.

No comments: