VMS-to-Unix Phrase Book
<=  Return           

1.3  Aliases

Problem

You want to define 'custom' commands.

Solution

Defined your own versions of commands using the Korn shell alias command.

Discussion

You can define your own commands, much as you did with DCL using the ksh alias command.
VMS> dir == "directory/size/date/protection"
VMS> dir

Directory DISK$USER01:[J_USER]

BATCH.DIR;1                3  29-NOV-2001 21:27:24.02  (RWE,RWED,RE,E)
CHECKLIST.SDML;29         20   6-APR-2001 22:02:08.38  (RW,RWED,R,)
CHECKLIST.TXT;1           32  12-OCT-2000 21:10:04.30  (RWED,RWED,RE,)
   :
   :
unix> alias dir='ls -la'
unix> dir
total 152
drwxr-x--x   5 juser    users       8192 Feb 19 20:53 .
drwxr-xr-x  41 root     system      8192 Feb 13 09:01 ..
drwxr-x--x   2 juser    users       8192 Nov 29 21:27 batch
-rwxr-----   1 juser    users      10510 Apr  6 22:02 checklist.html
-rwxr-x---   1 juser    users      12817 Oct 12 21:10 checklist.txt
   :
   :

Note in the example above the use of single quotes (') instead of double quotes (") or the backtick (`). Each of these has a different function as described in 1.5 so for now stick with single quotes which are sometimes referred to as strong quotes.

Unlike DCL, the default behavior is for aliases not to be expanded within scripts. Thus theoretically any existing command that you have redefine as an alias should not become hidden or altered for any of your scripts. But it's still generally a good idea to avoid defining an alias that is the same as an existing command. But how to check to see if the name is already taken? Try the following 5 commands to search for your proposed new command name.

unix> man 1 tscan
No reference page found for tscan in section 1.
unix> which tscan
no tscan in . /isapps/sct/banner/deve /isapps/sct/banner/general/exe
/isapps/sct/banner/links /isapps/sct/banner/admin /usr/ccs/bin
/usr/bin /bin /usr/local/bin /sbin . /oraapp/oracle/product/8.1.6/bin
. /isapps/adminutl/mis
unix> whereis tscan
tscan:
unix> alias tscan
tscan: alias not found
unix> whence tscan
unix> 

In the above example, we didn't have any matches, so tscan would be a good choice. Here's another example. Let's say I want to defined the command esac for Extended Search And Compare, a script that I use for comparing files. Doing our above test again:

unix> man 1 esac
No reference page found for esac in section 1.
unix> which esac
no esac in . /isapps/sct/banner/deve /isapps/sct/banner/general/exe
/isapps/sct/banner/links /isapps/sct/banner/admin /usr/ccs/bin
/usr/bin /bin /usr/local/bin /sbin . /oraapp/oracle/product/8.1.6/bin
. /isapps/adminutl/mis
unix> whereis esac 
esac:
unix> alias esac
esac: alias not found
unix> whence esac
esac
unix> 

Ooops! We got a match on that last one. Turns out esac is an internal ksh command for ending a shell case statement.

To define these personal commands everytime you logon, place them in your .profile file.

While the basic idea is the same with each system, command line syntax differences introduce a few wrinkles you should keep in mind.

In DCL, global qualifiers may appear anywhere in the command line where a space would be allowed. This means that such options can always be applied to a command alias to augment or override qualifiers specified in the definition itself. For example, given the DCL command:

     mybook == "directory/size/date/protection chap*.txt"
it would be possible to turn off the size qualifier by doing:
     mybook/nosize
which expands into:
     directory/size/date/protection chap*.txt/nosize

With Unix shells, on the other hand, all options must be specified before any arguments (parameters). This means if you include an argument value as part of your alias definition, you will not be able to apply additional flags when invoking the alias. In other words, given:

     alias mybook="ls -la chap*.txt"
the command:
     mybook -g
would expand to:
     ls -la chap*.txt -g
The -g text in this case would be interpreted as a filename not an option.

Unlike DCL, in Unix shells you can enter more than one command on a line by using the semicolon (;) as a command separator. This means that you can also define an alias that performs a series of commands in one step. For example, to set and export a symbol in one step:

/usr/users/wfc (\*)
jupiter_wfc> alias short-prompt='PS1="unix> "; export PS1'
/usr/users/wfc (\*)
jupiter_wfc> short-prompt
unix> 

Finally, Korn shell functions can be used to define what amount to complex aliases that take positional parameters. Functions should be used in your batch scripts where you would normally create an alias in DCL -- for example:

     say := write sys$output
See 3.3 for more information about sh and ksh functions, but consider the following example:

unix> swapum () {
> ls -l $1 $2;
> mv $1 $1.swapum;
> mv $2 $1;
> mv $1.swapum $2;
> echo "---";
> ls -l $1 $2;
> }
unix>
unix> swapum set-editor set-editor~
-rw-r--r--   1 wfc      users         46 Feb 19 22:53 set-editor
-rwxr-xr-x   1 wfc      users         78 Feb 19 22:54 set-editor~
---
-rwxr-xr-x   1 wfc      users         78 Feb 19 22:54 set-editor
-rw-r--r--   1 wfc      users         46 Feb 19 22:53 set-editor~
unix>

Discovering what this alias does and how it works is left as an exercise for the reader.

See Also

1.4 - Symbols and Logicals;
1.5 - Quoting;
Chapter 3 of Unix for OpenVMS Users ;
Chapter 11 of Unix Power Tools .


<=  Return           

Colophon:
Best Viewed With Any Browser
This page maintained by:
    Bill.Costa@unh.edu
    of the Enterprise Computing Group
    in the dept of Computing & Information Sevices
    at the University of New Hampshire

Typographical
Conventions

Created:  31-Jan-2001 BC
Revised:  27-Mar-2001 BC