Friday, July 22, 2005

How to make DOS look like Unix

How to make DOS look like Unix, Unix in the Enterprise 7/19/2005

Sandra Henry-Stocker,

Ok, well maybe the subject line of this column is stretching it just a
wee bit. There's no way that you can emulate more than a small
fraction of Unix power at the DOS Command Prompt. But DOS has grown
quite a bit since the days when it was an operating system in its own
right. With pipes and redirection, a pile of commands that are roughly
equivalent to their Unix counterparts (dir is to ls what type is to
cat) and a little finesse, you can make your time working in DOS seem
a little less like a detour through the dark ages and a little more
like home.

To begin our little makeover, let's look at the typical DOS command
prompt. When I open a command prompt on my Windows XP Professional
laptop, I see something like this:

C:\Documents and Settings\shenry-stocker\Desktop>

Not so bad, you might be thinking, but any prompt that stretches more
than halfway across MY screen is too much prompt for me. Can I change
this the way I can change my prompt on a Unix system (e.g., setting
PS1="\h> ")? Yes, in fact I can. For a minimalistic prompt, I like
using something like this:


This will change the DOS prompt to "> ". The $G represents the
greater-than and $S is a space.

The default prompt on DOS is usually $P$G -- the current path followed
by a > and you can change it back as easily:


Many other variables can be also be used in the prompt string:

$Q = (equal sign)
$$ $ (dollar sign)
$T Current time
$D Current date
$P Current drive and path
$V Windows version number
$N Current drive
$G > (greater-than sign)
$L & (less-than sign)
$B | (pipe)
$H Backspace (erases previous character)
$E Escape code (ASCII code 27)
$_ Carriage return and linefeed

Some of these (such as $V) make a perfectly hideous and worthless
prompt, but others can be advantageous at times -- such as $T$G$S when
you're watching the clock on a Friday afternoon.

Defining Macros

To clear the screen in DOS, use the cls command. It works just like
clear on Unix. And, if you don't like translating between Unix and DOS
commands just to clear your screen, you can turn clear into a macro.
Macros work like aliases in the Unix shells. To make clear equivalent
to cls, for example, you would do this:

doskey clear=cls

Once you type this at the command prompt, you can use "clear" to clear
your screen instead of cls -- at least until you open a new DOS
command prompt.

So now let's tackle some other annoying near-equivalents. I often find
myself typing "ls" at the command prompt when I meant to type "dir".
It runs, but it takes a while to get started. If I create a macro to
make ls equivalent to dir, on the other hand, the response is quick
and I don't feel like such a klutz for not being able to keep my
operating systems straight.

Another macro that I find useful is "date=date /t". Every time I type
"date" at the DOS command prompt, my intention is to find out what day
it is -- NOT to reset the date. By adding the /t, I avoid being
prompted for a new date and having to control-C my way out of it. For
the same reason, I like to add a macro that makes "time" invoke the
"time /t" command.

Though DOS lacks a pwd command, it is possible to trick it into
displaying the current directory by typing the "cd" command without
parameters. Unlike its Unix counterpart, the cd command in DOS does
not return you to your home directory when you type it without
parameters, but it does do one useful thing -- it displays the current
path. With a "pwd=cd" macro, I can change my prompt to one that
doesn't irritate me and still find out where I happen to be within the
file system.

> pwd C:\Documents and Settings\shenry-stocker\My Documents

Severe Limitations

Before we get too carried away with our goal of making DOS more
Unix-like, I have to mention the biggest drawback to DOS macros --
they don't accept parameters. For example, you can try to make "man"
equivalent to "help" and it will work, more or less, but typing "man
dir" will not give you the same result as typing "help dir". Instead,
it will give you the same result as typing "help" by itself.

Making Your Macros Reusable

Once you have defined a set of macros to make it a little easier to
work at the DOS command prompt, you'll undoubtedly want to save them
so that you can use them every time you open up a DOS command prompt.
The first step in this process is to save your macros in a file. You
can do this with this doskey /m command.

doskey /m > macros.txt

The doskey /m command lists your macros. The remainder of this
command, as you'd expect, redirects that listing to a text file. To
invoke your saved macros at a later time, you can issue this command:

doskey /macrofile=macros.txt

Of course, your version of this command might actually turn out to be
more like this:

doskey /macrofile=C:\Documents and Settings\jdoe\macros.txt

Don't forget all those backward slashes!

Of course, even this one command can be something of an annoyance if
you need to type it every time you open up a DOS command prompt. To
reduce my level of annoyance, I put both my preferred prompt and the
doskey command to invoke my macros in a batch file which I call

> type customize.bat
@echo off
:: set up macros and custom prompt

doskey /macrofile=C:\batfiles\macros.txt
prompt $G$S/

I then put this customize.bat file somewhere on my search path (I
actually added C:\batfiles to my search path). Now, anytime I open a
DOS command prompt, I type "customize". Immediately, my prompt changes
and my macros take effect.

If I make any changes to my macros.txt file (which I edit using the
edit command and use control-F to access the edit command's menus), I
have to type "customize" again. This is like sourcing a dot file and
feels normal enough, so it doesn't bother me.

Here's a list of the macros I've set up so far:

> doskey /m
time=time /t
date=date /t
ls=dir /w
alias=doskey /m

No earth shakers in the list, but each command makes me a little more
comfortable working in DOS.


Post a Comment

<< Home