Interoperability with UNIX

Services for UNIX 2.0 includes an implementation of the Korn shell. The shell is a command language interpreter that acts as the interface to the UNIX operating system. The shell interprets commands, calls the appropriate program, and returns standard output. Many shells also provide a high-level programming language that you can use to accomplish complex tasks by combining basic utilities and functions provided by the operating system.

The Korn shell, developed by David Korn at AT&T, combines many of the desirable features of the C and Bourne shells. The Bourne shell, developed at AT&T by Steven Bourne, was the first UNIX shell. The Bourne shell provides a powerful programming language. The C shell, another UNIX shell, provides a number of features not available with the Bourne shell, such as command aliases, a command history mechanism, and job control of command processing. Table 25.10 provides a feature summary of these three common shells.

Table 25.10 Shell Feature Summary





Command alias




Command history




Command-line editing




Job control




Shell scripting




Other shells are available for the UNIX operating system. Bash (Bourne Again shell) is an extension of the Bourne shell that incorporates features of both the Korn and C shells and is common on Linux systems. Tcsh is an extended version of the C shell that includes command completion, a command-line editor, and enhanced history manipulation.

Using the Korn Shell

The implementation of the Korn shell included with Services for UNIX differs from the standard UNIX Korn shell in the following ways:

  • Semicolons are used instead of colons to separate entries in the PATH variable.

  • Current directory in PATH is referred to as ;; or ;.; instead of period (.).

  • Startup file is called Profile.ksh instead of . profile.

  • Startup file for systemwide environment variables is called /etc/profile.ksh instead of /etc/profile.

  • History file, which stores the command history of a user, is called Sh_histo file instead of Sh_history.

  • Partial job control enables running of jobs in the background by using the ampersand (&) on the command line.

If your system administrator sets up the Korn shell as your default shell in Telnet Server, it is the shell you log in to when accessing a Services for UNIX server by means of Telnet. If you want to use the Korn shell without logging on to it, you can access it by using the sh command ( ksh in standard UNIX).

Environment Variables

A variable consists of a name and its assigned value. You can define variables and use them in shell scripts. Other variables, called shell variables , are set by the shell. A variable name can contain letters, numbers (but not as the first character), and the underscore. The equal sign with no spaces on either side is used to assign a value to the variable. Once a variable is defined, you must use the export command to make the value of the variable available to other processes running under the shell.

The Korn shell runs the Profile.ksh file when you log on. The Profile.ksh file is used to set user-specific environment variables and terminal modes. (The system administrator can also use / etc/profile.ksh to set variables systemwide for all user accounts on the system.) Some of the variables that you can use in Profile.ksh include PATH, HOME, VISUAL, EDITOR, SHELL, HISTSIZE, HISTFILE, PS1, PS2, CDPATH.

Table 25.11 lists many of the environment variables used by the Services for UNIX Korn shell. For a complete list of the shell variables supported by the Services for UNIX Korn shell, see Services for UNIX Help under the topic sh .

Table 25.11 Korn Shell Environment Variables

Variable Name



Expands to the argument of the previously executed command.


Defines the search path used by the cd command.


Defines the width of the output display for programs that read the value; for example, the text editor vi .


Specifies a default editor for the system to call when no editor is specified.


Performs parameter substitution on the value if ENV is set. When the shell is invoked, the named file runs first.


Displays the value set by the most recently failed subroutine.


Contains a pattern that defines which files are ignored during file expansion.


Displays the editor for the fc command.


Displays the absolute path of the file (default.sh_histo) that contains the command history.


Displays the number of commands in the history file.


Contains the absolute path of your home directory, which becomes your current directory when you log on.


Contains the characters used as internal field separators.


Displays the number of the line from standard input that the shell script is currently executing.


Defines the number of output lines used by the select statement when printing its menu. Select writes specific words to standard error.


Contains the absolute path of the file where your mail is stored.


Defines the number of seconds the shell waits before checking for new mail.


Contains the mailbox files where new mail notification is sent.


Displays the path of the previous working directory.


Defines the absolute paths of the directories where the shell searches for executable files.


Displays the process ID of the parent of the shell.


Contains the prompt displayed by the shell. The default Korn shell prompt is $. Other options exist.


Contains the secondary shell prompt.


Contains the path of the current working directory.


Generates a random number.


Contains user input from the select statement.


Defines the absolute path of the current shell. Is used by commands to invoke the shell.


Defines the number of seconds the shell remains inactive before it terminates.


Specifies a default editor that overrides the EDITOR variable.


The Korn shell recognizes a special meaning for certain characters. When a regular expression contains a metacharacter, the Korn shell interprets the character as shown in Table 25.12.

Table 25.12 Korn Shell Metacharacters




Escape character. When immediately preceding another character, it removes the special meaning from that character.


Wildcard match for zero or more characters.


Wildcard match for one character.

[ ]

Wildcard match for the characters specified within the brackets.


Redirects standard input so that it comes from a specified file instead of the terminal.


Redirects standard output so that it goes to a specified file instead of the terminal.


Appends standard output to the end of a specified file.


Connects the standard output of one command to the standard input of another command.(Called the Pipe.)


Causes a process to run in the background when it is appended to a command line.


Represents the path of a user's home directory.


Current directory.


Parent to the current directory.

$1 - $9

Represents the first nine arguments in a command.


Root directory.


Takes a string literally. Variable substitution allowed.


Takes a string literally. Variable substitution allowed.


When back quotes precede and follow a command string, tells the shell to run the command and use the output in place of the string.

( )

Groups commands together for execution.


Separates commands on a command line.

newline (ENTER)

Starts command execution.

Shell Commands

When you enter a command at the shell prompt, the shell evaluates the command, makes substitutions for variables and aliases, and then runs the command.

The basic structure of a command is as follows:

command-name argument1 argument2 >file-name

Commands can take options, which modify the action of a command. For example, ls lists the contents of a directory but does not include the hidden (.) files. Use ls -a to also see the hidden files.

The shell processes the command after you press ENTER. Commands can also be separated by semicolons and entered on a single line; the commands on the line are not processed, however, until after you press ENTER.

When the shell runs a command, it starts a process. Each process has a process ID (PID), which is used to access the process. Processes can be run in the foreground or the background and can also be suspended or cancelled. Parent processes fork child processes, which are assigned their own PIDs.

A command receives standard input from the terminal and sends standard output and standard error to the terminal.

You can redirect the standard input from the terminal to a file as follows:

command-name < file-name

You can also redirect the standard output from the terminal to a file as follows:

command-name > file-name

You can append it to an existing file as follows:

command-name >> file-name

In addition, you can redirect the standard error to a file as follows:

command-name *1>***file-name1 *2>***file-name2

The standard output is sent to file-name1 and the standard error is sent to file-name2 .

You can use pipes to connect the standard output of one command to the standard input of another command as follows:

command-name | command-name > file-name

The Services for UNIX Korn shell is a programmable shell that supports the following structured commands, as shown in Table 25.13. For a complete list of supported shell commands, see Services for UNIX Help under the topic sh .

Table 25.13 Shell Programming Services for UNIX Korn Shell




Runs commands based on a particular setting of another variable.


Runs a specific list of commands.


Specifies conditions in a script.


Writes specified words to standard error.


Runs a list of commands until a zero value is returned.


Runs a list of commands while a certain condition is true.

The Services for UNIX Korn shell has built-in commands . Built-in commands are run by the shells own process. Table 25.14 lists the built-in commands that are available with the Services for UNIX Korn shell. For details about each command, see Services for UNIX Help.

Table 25.14 Services for UNIX Korn Shell Built-in Commands




Runs a shell file in the current environment.


Expands arguments. Returns an exit status of 0 (success).


Assigns a new name to a command.


Exits from a for, while, or until loop.


Changes the current working directory.


Resumes with the next iteration of a for, while, or until loop.


Displays its arguments to standard output.


Standard environmental variables.


Scans and runs the specified command.


Runs the specified command without creating a new process.


Exits the shell.


Makes the value of the variable available to child processes.


Returns an exit status of 1 (failure).


Selects specified commands from command history.


Parses command line options.


Displays current jobs.


Ends the specified job.


Evaluates the expression.


Displays arguments from the shell.


Displays the current working directory.


Reads one line from standard output.


Makes the value of the variable read-only so it cannot be changed.


Exits a function.


Sets shell flags or command line argument variables.


Interactive command and history editing in the shell.


Promotes each command line argument (for example, $3 to $2).


Features of Korn shell specific to Windows NT.


Checks for the properties of files, strings, and integers, and returns the results of the test as an exit value.


Displays run time and CPU time.


Displays the user program and system times accumulated by the shell.


Specifies commands to run at a signal.


Returns exit status of 0 (success).


Identifies a name as interpreted by the shell.


Sets attributes and values for shell parameters.


Changes access permissions.


Removes an alias.


Removes a variable definition from the environment.


Waits for a child process to terminate.


Describes how the shell interprets a command name (as a function, shell keyword, command, alias, or executable file).

Command Aliases

For commands and command-line options, you can assign an alias or name that the shell translates to another name or string. (Be sure to choose an alias that is easy to remember.) The shell substitutes the command and options for the alias you enter. Creating an alias at the command line makes the alias available in the current shell environment. To make the alias a part of the work environment, add the following line to the shell start-up file (.kshrc) that defines the alias and exports it:

aliasnewname***=´*** command -option´**;export**newname

The command alias -x exports the alias to the child process only.

To remove an alias, use unalias followed by the alias name:


The Services for UNIX Korn shell provides a set of predefined aliases. For more information about the alias command, see Services for UNIX Help.

Command History

The Services for UNIX Korn shell features a history file, which contains a list of a defined number of executed commands. These commands can be accessed for editing and persist in the file between logon sessions.

You can set the maximum number of commands to be saved in the history file by using the HISTSIZE variable:

HISTSIZE = number ; export HISTSIZE

If you do not define this variable, UNIX saves a system-defined number of commands.

You can define the name and location of the history file by using the HISTFILE variable:

HISTFILE = file-name ; export HISTFILE

If you do not define this variable, your history file is named .sh_histo and stored in your home directory.

Command Line Editing

You can edit the commands in the history file by using built-in Korn shell editors, such as vi , or the built-in fc command. You can use this feature to correct mistakes or to reuse work you have completed.

To define vi as your default editor:

set -o vi

– Or –

VISUAL=/sfu/shell/vi; export VISUAL

The built-in editor that is provided with the Korn shell offers a subset of the full functionality available with vi. You can access vi to edit a command by entering the command, pressing ENTER, and then typing vi . This allows you to edit a multiline command.

Arithmetic Evaluation

The Services for UNIX Korn shell has a built-in arithmetic expression feature. It supports logical and arithmetic operators. The syntax for arithmetic operators is $(( < arithmetic expression > )) or $( < arithmetic expression > ) . The Korn shell replaces the arithmetic expression with its value, beginning with the innermost nested expression. Table 25.15 lists the operators.

Table 25.15 Arithmetic and Logical Operators










Divide (with truncation)




Bit-shift left


Bit-shift right


Bitwise and


Logical and


Bitwise or


Logical or


Bitwise exclusive or


Logical not


Bitwise not


Less than


Greater than


Less than or equal to


Greater than or equal to


Not equal to


Equal to

Shell Scripts

A shell script is a file containing a series of commands that together perform a function. You can access a Korn shell script from the command prompt if you are running the Korn shell and have permission to execute the script by typing the file name. You can also run the shell script if the Korn shell is not running by entering the following command:




Windows NT does not execute a script when you invoke it from the command prompt with only a file name; UNIX, however, does execute scripts if you specify the path and file name of the shell on the first line of the script, such as in the following:


You must link each file or file name extension to a program. In particular, .sh or .ksh can be associated with the Korn shell.

Job Control

You can use job control to run a command in the foreground or the background, or to temporarily suspend it. In addition, you can see a list of the commands currently running.

When you enter a command, if it is not a built-in command, the shell forks a new process in which to run the command. The kernel schedules the process and gives it a process ID (PID). The shell keeps track of the process and gives it a job number.

Some processes are run in the foreground: they might be interactive or take only a short time to run. Other processes are better run in the background, especially commands that take a long time to run, such as a large sort. You can move a process to the foreground or the background and get a list of the current jobs. You can also temporarily suspend a process or terminate it.

Table 25.16 lists the job control commands that Services for UNIX supports.

Table 25.16 Job Control Commands



jobs –l

Lists the current jobs. Each job is numbered. The -l option displays the PID.

Command &

Runs the command in the background. For example, sort file-name   newfile &


Terminates the job specified by job-number . The job number is displayed when a job is started by using  & or the jobs command.

UNIX Utilities

The following UNIX utilities are available in Services for UNIX 2.0.

  • Table 25.17 lists the new utilities that are available in Services for UNIX 2.0.

  • Table 25.18 lists the utilities that were previously available in Services for UNIX 1.0 and that are included in Services for UNIX 2.0.

  • For more information about these commands, see Services for UNIX Help.

Table 25.17 New Utilities in Services for UNIX 2.0

UNIX Command



Schedules tasks.


Lists scheduled tasks and edits them.


Cuts out bytes, character, or character-delimited fields from each line in one or more files, concatenates them and writes them to standard output..


Writes the date and time.


Compares two files and displays line-by-line differences.


Prints the disk usage of a file or directory.


Terminates or signals processes.


Invokes a command with a specified scheduling priority.


Displays files in specified formats.


Merges corresponding or subsequent lines of files.


Runs Perl programs.


Prints environment variables that are set.


Writes formatted output.


Lists processes and their status.


Prints the current working directory.


Reprioritizes a running process.


Prints differences side-by-side.


Suspends execution for a specified interval.


Splits a file into pieces.


Finds printable strings in an object or binary file.


Becomes another user (or administrator).


Creates tape archives, and adds or extracts files from an archive.


Shows top processes sorted by CPU usage.


Translates characters in input stream.


Prints names of the current system.


Decodes a text file into a binary file.


Encodes a binary file.


Waits for process completion.


Locates command and print pathname/alias.


Constructs argument lists and invoke a utility.

Table 25.18 Utilities in Services for UNIX 1.0

UNIX Command



Invokes the Korn shell.


Removes the path, leaving only the file name. Deletes any prefix ending in / and any suffix from string and prints the result to standard output.


Concatenates and displays a file.


Changes or assigns the permissions mode of a file.


Changes the owner of a file.


Copies files.


Delivers all but the last level of the path in a string. See basename .


Recursively searches a directory hierarchy, looking for files that match a specified Boolean expression.


Searches files for a pattern and prints all lines containing that pattern.


Copies first n lines of specified file names to standard output.


Creates a hard link to a file. Links a file name to a target by creating a directory entry that refers to the target.


Lists the contents of a directory.


Creates a named directory with read, write, and execute permission for every type of user.


Filters and displays the contents of a text file on the terminal, one screen at a time.


Moves a file name to a target.


Removes an entry for a file from a directory.


Removes a directory.


Copies named file names to a standard output; edits according to a script of commands (a stream editor).


Sorts the lines of all named files, groups them, and writes the result to standard output.


Copies a named file to standard output, beginning at a designated place.


Transcribes standard input to standard output and makes copies in a file name.


Updates the access time or the modification time of a file.


Reports on repeated lines in a file.


Displays a count of lines, words, or characters in a file.


Edits text in a screen-based environment.


An interpreted language that scans text files, extracts information from those files, and prints reports based on that information.

Using vi

The vi editor is an interactive text editor for creating and editing ASCII files. The vi editor requires you to enter a command to perform an action, such as entering text, deleting text, or moving the cursor. You can be in one of two modes when using vi: command mode or input mode. In command mode, you can enter commands to perform such actions as deleting text or moving the cursor in the file. In input mode, you can enter and change text. You enter input mode by entering a specific vi command. You leave input mode by pressing ESC.

This section provides basic information to get you started using vi. After you understand the mechanics of using vi, you can explore its functionality. (The mechanics are simple; the details can seem obscure at first.) For more information about the complete functionality of vi, see any of the available print or online sources, including Services for UNIX Help.

To edit a file by using vi, at the command prompt type:


and press ENTER.

If the file already exists, it appears on the screen. If the file does not exist, vi creates it.



You can take advantage of a file recovery feature that is provided with vi. If the system saves a copy of the last saved version of your file in a buffer, you can access that copy of the file by typing vi -r file-name and pressing ENTER.

What you see on the screen is the text of the file (if it exists), a blinking cursor in the left corner of the screen, a column of tildes along the left margin of the file that represent blank lines (if any are in view), and the name of the file in the last line of the screen. (The bottom of the screen is also used to display messages, to show commands you enter that begin with /, ?, !, and :, and to indicate input mode if the showmode option is set.)

To begin entering text, press i (to insert text). You can then begin typing. The text you enter appears, beginning at the position of the cursor. When you are done entering text, press ESC.

To save the file and exit vi, type


and press ENTER.

Use the colon to escape to the shell so that you can enter a command at the bottom of the screen. Press w to write the file to disk. Press q to quit the vi editor. Table 25.19 provides a summary of the of the commands used for starting and quitting vi.

Table 25.19 Starting and Quitting vi




Edits file-name (this creates a new file or edits an existing one).

vi -rfile-name

Recovers a file after a system failure and edits it.


Quits vi if no changes have been made.


Quits vi without saving changes.


Writes (saves changes) and quits vi.

As the size of the file increases, you can more easily move throughout the file by using the following commands in command mode, as shown in Table 25.20.

Table 25.20 Moving the Cursor in Command Mode




Moves the cursor forward one character.


Moves the cursor back one character.


Moves the cursor one character to the right.


Moves the cursor one character to the left.


Moves the cursor down one line.


Moves the cursor up one line.


Scrolls down half a screen.


Scrolls up half a screen.


Scrolls down one screen.


Scrolls up one screen.

n G

Moves the cursor to line n.


Moves the cursor to the end of the file.

Tables 25.21 and 25.22 list many ways for inserting and changing text that allow for detailed control.

Table 25.21 Input Mode




Inserts text after the cursor.


Inserts text at the end of the current line.


Inserts text before the cursor.


Inserts text before the current line.


Opens a line in the text below the cursor.


Opens a line in the text above the cursor.

Table 25.22 Changing Text




Replaces the current character with the next character typed; returns to Command mode.


Replaces text beginning with the current character until you pressESC.


Changes the entire current line to the new text entered.


Changes the current word, beginning at the cursor position, to the new text entered.


Substitutes the character at the cursor position with the new text entered.


Substitutes the entire current line with the new text entered.

Table 25.23 lists ways to delete text in vi.

Table 25.23 Deleting Text in vi




Deletes from the cursor to the end of the line.


Deletes the current character.


Deletes the current line.

You can yank and put —that is, copy and paste—text within a file and between files. The yank command, as shown in Table 25.24, copies selected text and places it in a buffer. The put commands copy the text from the buffer to a specified place in the file. Named buffers and numbered buffers are available but are beyond the scope of this discussion.

Table 25.24 Yank and Put Commands



yy or Y

Yanks (copies) the current line and places it in a buffer.


Yanks (copies) five lines and places them in a buffer.


Puts (pastes) the text that is in the buffer into the line after the current one.


Puts (pastes) the text that is in the buffer into the line before the current one.

You can search for a character string within the file. The search tools are case-sensitive. If the pattern is not found, vi displays a message at the bottom of the screen telling you that it is unable to find the pattern. Table 25.25 lists the available search commands and their descriptions.

Table 25.25 Search Commands




Moves forward to the first character in the next occurrence of the character string pattern .


Repeats the previous forward search.


Moves backward to the first character in the next occurrence of the character string pattern .


Repeats the previous backward search.

You can access global pattern substitution from the command prompt.

The command takes the following form:


In this command, string represents any regular expression that you want to search for, replacement represents the text that replaces string , and g specifies global replacement of all occurrences of string . If the trailing g is omitted, only the first occurrence of the string in each line is replaced. If you want to be prompted to confirm each substitution, type a c after the g in the command, as follows:


Table 25.26 lists a few of the many other commands available in vi.

Table 25.26 Other Useful Commands




Escapes to the shell to run a command.


Runs one command.


Undoes the last change.


Restores the last deleted line.


Toggles the case of the current character.


Transposes the character in the current cursor position with the next character.


Repeats the last change.