VMS-to-Unix Phrase Book
<=  Return           

1.4  Symbols and Logicals

Problem

You want to use filename shortcuts interactively to save time, and in scripts to enhance portability.

Solution

The Korn shell supports shell variables (analogous to DCL symbols) but does not have anything truely comparable to VMS logicals. Some imperfect work arounds are illustrated below.

Discussion

Shell variables and DCL symbols are largely analogous constructs, and are used for many of the same purposes. Here's how they compare.

As mentioned above, a child process cannot change the environment of the parent process. To define or modify shell variables in the parent process using a script, 'source' that script so that it is executed in the context of that process. For the Korn shell, the period is used to source a script into the current shell. Consider the following:
unix> cat set-editor
#!/bin/sh
EDITOR="vi";  export EDITOR
echo "I set EDITOR to $EDITOR";
# EOF

unix> chmod +x set-editor
unix> echo $EDITOR
emacs
unix> ./set-editor
I set EDITOR to vi
unix> echo $EDITOR
emacs
unix> . ./set-editor
I set EDITOR to vi
unix> echo $EDITOR
vi
unix>
In the above example, the chmod command was used to make script executable so we could execute it directly (see x.x). This demonstration shows that when run as a child process, the script could only change the variable's value for itself, not the parent. The most common use of the source command is to use scripts to setup interactive environment defaults. For example, after changing a .profile file, you would use the source command to apply the changes to your current shell environment.

In VMS, logicals are identifiers that can be set for the life of the process, or can be shared by multiple processes. Like symbols, logicals can be inherited by a child process from the parent. What makes logicals so powerful, however, is that wherever a filename may be used, in either a script or inside a program, a logical may be used instead. When the operating system is given the logical name in a context where a file name would be expected, such as an open statement in a program, the operating system will automatically dereference the logical and use it's value to find the referenced file. This dereferencing is transparent to the program. Unfortunately Unix does not support this powerful feature. Two methods can be used to approximate VMS logicals.

  1. If the program accepts filenames from the command line, use shell variables:
    unix> dataset="/usr/local/app/data/old"
    unix> ./myprog $dataset/sample.dat
              program uses /usr/local/app/data/old/sample.dat
    unix> dataset="/usr/local/app/data/new"
    unix> ./myprog $dataset/sample.dat
              program uses /usr/local/app/data/new/sample.dat
    unix>
    
    In the example above, we invoke the program in exactly the same way each time, but change the environment variable definition to change the directory used for locating the desired input file.

  2. If the program must be 'hard wired' ahead of time with a fixed filespec, establish a standard naming scheme and use file links to make changes at run time. In this example, let's assume the program is written to always open the file

            /tmp/app/sample.dat
    Then to change inputs for each execution, we can create a new file link from that file specification to the actual input located someplace else.
    unix> ln -s /usr/local/app/data/old/sample.dat /tmp/app/sample.dat
    unix> ./myprog
    
              program uses /usr/local/app/data/old/sample.dat
    
    unix> ln -s /usr/local/app/data/new/sample.dat /tmp/app/sample.dat 
    unix> ./myprog
    
              program uses /usr/local/app/data/new/sample.dat
    
    unix>
    

Note that 'symbolic links' were used in the above example rather than the default 'hard link'. Since the object is to try and simulate a VMS logical name, a symbolic link is a (ahem) more logical choice for a number of reasons:

So while symbolic links do not work exactly like VMS logical names, they can be used to create aliases for files that are transparent to application programs.

See Also

1.3 - Aliases;
Chapter 3 of Unix for OpenVMS Users ;
Chapter 19 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