Windows Bashing…with Cygwin

I was in Wales last week, land of my girlfriend ( yes, I have got one, try not to look so shocked).
Wales, land of story and legend….where the rain goes for it’s summer holidays.

Stepping gingerly between the puddles in picturesque ( albeit, soggy) Laugharne, we spent an instructive ( and mainly dry) hour or so at the boathouse once occupied by Dylan Thomas. In the course of this cultural interlude, I learned that the Great Man’s last words were “ I’ve had 18 straight whiskys. I think that might be the record.”

Hmmm, I wonder if he’d been trying to write a Windows batch script ?

Whatever the merits of Windows in terms of it’s ubiquity, one undeniable fact is that the facilities provided for batch scripting on the command line are stone-age compared to those in Unix.
This is something I’ve often reflected on, usually when confronted with a problem that requires a bit more than a simple for loop.

Help is at hand however, in the form of Cygwin – a toolset which enables you to more-or-less run a bash shell on Windows. Sounds good to me. Let’s have a look….

I’ve got a (seldom used) Windows Vista partition on the laptop. Oracle XE is also installed here.

Installation

Downloading and Installing Cygwin is fairly straightforward.
Go to www.cygwin.com and click the “Install or Update Now” link, select a mirror site and away we go.

An installation program ( setup.exe) is downloaded first. When you run this you then get the option to choose which packages you want to install. For the purposes of this exercise, I just accepted the defaults.

Running Cygwin

If you’ve checked the box to create a desktop icon, you can run by clicking that. Otherwise just going to the start menu and looking under Programs \ Cygwin \ Cygwin Bash Shell will have the same effect.

Oh look, a $ prompt. On Windows. Intruiging.
In honour of the great man, let’s deal with the exestential questions first – who am I ? where am I ? what am I ?

Incidentally, your cygwin home directory on Windows is under c:\cygwin\home\your_user.
Setting command line recall works fine :

$ set -o vi

However, vi itself isn’t included in the default install. Not that this is a big problem. One of the benefits of Cygwin is that you can mix and match linux and windows commands :

$ notepad wisdom.sh
If the file doesn’t exist in this directory, Notepad will ask if you want to create it.

Now let’s try a simple shell script (continuing our homage to Wales’ National Poet) :

#!/bin/sh
echo ' Rage against the dying of the light!'
$ . ./wisdom.sh
Rage against the dying of the light!

Mike@Mike-Laptop ~
$

OK. Now, where are my files ?
Cygwin looks for every available drive and mounts it under /cygdrive followed by the relevant drive letter.

$ mount
C:/cygwin/bin on /usr/bin type ntfs (binary,auto)
C:/cygwin/lib on /usr/lib type ntfs (binary,auto)
C:/cygwin on / type ntfs (binary,auto)
C: on /cygdrive/c type ntfs (binary,posix=0,user,noumount,auto)
D: on /cygdrive/d type ntfs (binary,posix=0,user,noumount,auto)

Mike@Mike-Laptop ~
$

The $PATH variable will include everything that’s in your Windows PATH environment variable.
So, what if you’d rather use TextPad than Notepad for editing. Well, we could try just adding it to the $PATH “linux” environment variable :

export PATH=$PATH;cygdrive/c/”Program Files”/”TextPad 5”

This doesn’t quite do the trick. When you try to invoke TextPad, you get :

bash: TextPad: command not found

To do this, you need to add it to the Windows PATH environment variable itself from the Windows Start Menu (Control Panel / System / Advanced system settings ). Click the Environment Variables button and add the following to the PATH :
;c:\Program Files\ TextPad 5

Obviously, you’d need to change this to the path for your TextPad installaion, if different.
If we try again now :

$ textpad wisdom.sh

The file is opened in TextPad.
It’s worth noting that Windows commands seem not to be case sensitive, even when launching from the $ prompt.

Now to have a look at some other files on my C drive.
I’ve created a directory C:\my_cygtest_dir and put some files there.

To get to it, as you’d expect really :

cd /cygdrive/c/my_cygtest_dir

This directory contains a PL/SQL package definition, pkg_thomas.pks.

CREATE OR REPLACE PACKAGE thomas AS
	PROCEDURE pr_milkwood( p_drink IN whisky%TYPE);
	PROCEDURE pr_rage;
	FUNCTION fn_dying_light RETURN VARCHAR2;

Now let’s try a quick test of grep :

Mike@Mike-Laptop /cygdrive/c/my_cygtest_dir
$ grep PROCEDURE pkg_thomas.pks
   PROCEDURE pr_milkwood( p_drink IN whisky%TYPE,
   PROCEDURE pr_rage;

Mike@Mike-Laptop /cygdrive/c/my_cygtest_dir
$

To finish off this whistle-stop tour, I’ve delved into the dim and distant past for a shell script which is close to my heart.
Many years ago, when the Millenium Bug was still a source of anxiety to IT Managers the world over ( and the source of a nice little nest egg for innumerable COBOL programmers), I was a DBA.

As a matter of course, I’d have the Production Databases alert log tailed in a Terminal window on the desktop. Moving from Unix to VMS it was still possible to do this. On Windows it was sadly impossible.
Let’s see if Cygwin can finally bring this functionality to me :

#!/bin/sh
#
# db_alert.sh
#
# script to tail -f the Oracle XE alert log
#
tail -f /cygdrive/c/oraclexe/app/oracle/admin/XE/bdump/alert_xe.log

This is the same script as the one I use in Linux. Only the location of the file has been changed. And it works !

So, next time you’re faced with Windows Batch Script misery, don’t reach for the bottle like poor Dylan, reach for Cygwin.

Author: mikesmithers

Back in 1993, I discovered that I could get paid money for doing fun stuff with computers. Over the years, I've specialised in Oracle Databases as a developer, a DBA and sometimes, an architect. It's my evil alter-ego - The Antikyte - who writes a blog about my various technical adventures. Yes, that is his Death Star parked in the Disabled Bay. I currently live in the South-West of England with Deb, my long-suffering wife.

One thought on “Windows Bashing…with Cygwin”

  1. I’ve used cygwin for a while now and thought I’d leave a tip… I’ve never liked the command window that comes with Windows, and bash makes use of the same be default. To replace it, you can download a program called PuTTYcyg hosted on google code:

    http://code.google.com/p/puttycyg/

    This is a modified version of PuTTY, so it has all of PuTTY’s features, but it adds a feature to connect to a local cygwin terminal. You can use PuTTY to connect to localhost, but then you have problem when launching GUI programs as they no longer know where to display since you’re technically connected over the network (through the loopback interface). PuTTYcyg solves that problem without taking away any of the power that cygwin gives you.

    Like

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.