UNIX Shell

Services for UNIX Version 1.0 includes a 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 can be used to achieve complex tasks by combining basic utilities and functions provided by the operating system.

Korn shell, developed by David Korn at AT&T, combines many of the desirable features of the C and Bourne shells. Bourne shell, developed at AT&T by Steven Bourne, was the first UNIX shell. Bourne shell provides a powerful programming language. 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 11.7 Shell Feature Summary

 

Bourne

C

Korn

Command Alias

 

X

X

Command History

 

X

X

Command-line Editing

 

 

X

Job Control

 

X

X

Shell Scripting

X

X

X

Other shells are available for the UNIX operating system. Bash (Bourne Again shell) is an extension of Bourne shell that incorporates features of both the Korn and C shells and is generally used with Linux. Tcsh is an extended version of 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 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 system-wide 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 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 into when accessing a Services for UNIX server via Telnet. If you want to use Korn shell without logging into it, you can access it using the sh command ( ksh in the standard UNIX Korn shell).

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.

The Korn shell runs the profile.ksh file when you login. 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 system-wide for all user accounts on the system.) Some of the variables used in .profile include PATH, HOME, VISUAL, EDITOR, SHELL, HISTSIZE, HISTFILE, PS1, PS2, CDPATH.

Table 11.8 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, consult the Services for UNIX online help for sh .

Table 11.8 Korn Shell Environment Variables

Variable Name

Description

_

Expands to the argument of the previously executed command.

CDPATH

The search path used by the cd command.

COLUMNS

Defines the width of the output display for programs that read the value, like vi.

EDITOR

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

ENV

If ENV is set, parameter substitution is performed on the value. When the shell is invoked, the named file is run first.

ERRNO

Value set by most recently failed subroutine.

FIGNORE

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

FCEDIT

The editor for the fc command.

HISTFILE

The absolute path of the file (default sh_histo) containing the command history.

HISTSIZE

The number of commands in the history file.

HOME

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

IFS

Characters used as internal field separators.

LINENO

The number of the line from standard input currently being executed by the shell script.

LINES

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

MAIL

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

MAILCHECK

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

MAILPATH

The mailbox files where new mail notification is sent.

OLDPWD

The path of the previous working directory.

PATH

The absolute paths of the directories that the shell searches for executable files.

PPID

The process ID of the parent of the shell.

PS1

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

PS2

The secondary shell prompt

PWD

The path of the current working directory.

RANDOM

Generates a random number.

REPLY

Contains user input from the select statement.

SHELL

The absolute path of the current shell and is used by commands to invoke the shell.

TMOUT

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

VISUAL

Specifies a default editor, overriding the EDITOR variable.

Metacharacters

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 11.9.

Table 11.9 Korn Shell Metacharacters

Character

Meaning

\

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

*

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.

|

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

&

Causes a process to run in the background when 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 to a command.

/

Root directory.

´

Takes a string literally. Variable substitution allowed.

"

Takes a string literally. Variable substitution allowed.

`

Back quotes around 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:

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 forked 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.

It is possible to redirect the standard input from the terminal to a file:

command-name < file-name

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

command-name > file-name

You can append it to an existing file:

command-name >> file-name

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

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 .

Pipes can be used to connect the standard output of one command to the standard input of another command:

command-name | command-name > file-name

The Services for UNIX Korn shell is a programmable shell that supports the following structured commands. For a complete list of supported shell commands, see the Services for UNIX online help for sh .

Table 11.10 Shell Programming Services for UNIX Korn Shell

Command

Use

case

Runs commands based on a particular setting of another variable.

for

Runs a specific list of commands.

if

Specifies conditions in a script.

select

Writes specified words to standard error.

until

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

while

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 shell's own process. The built-in commands available with the Services for UNIX Korn shell are listed below. For details about each command, consult Services for UNIX online help.

Table 11.11 Services for UNIX Korn Shell Built-In Commands

Command

Description

.

Runs a shell file in the current environment.

:

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

alias

Assigns a new name to a command.

break

Exits from a for, while, or until loop.

cd

Changes the current working directory.

continue

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

echo

Displays its arguments to standard output.

environ

Standard environmental variables.

eval

Scans and runs the specified command.

exec

Runs the specified without creating a new process.

exit

Exits the shell.

export

Makes the value of the variable available to child processes.

false

Returns an exit status of 1 (failure).

fc

Selects specified commands from command history.

getopts

Parses command line options.

jobs

Displays current jobs.

kill

Ends the specified job.

let

Evaluates the expression.

print

Displays arguments from the shell.

pwd

Displays current working directory.

read

Reads one line from standard output.

readonly

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

return

Exits a function.

set

Sets shell flags or command line argument variables.

shedit

Interactive command and history editing in the shell.

shift

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

shpc

Features of Korn shell specific to Windows NT.

test

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

time

Displays run-time and CPU time.

times

Displays user program and system times accumulated by the shell.

trap

Specifies commands to run at a signal.

true

Returns exit status of 0 (success).

type

Identifies a name as interpreted by the shell.

typeset

Sets attributes and values for shell parameters.

umask

Changes access permissions.

unalias

Removes an alias.

unset

Removes a variable definition from the environment.

wait

Waits for a child process to terminate.

whence

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

Command Aliases

You can assign an alias , which is a name, usually easy to remember, that the shell translates to another name or string, for a command, including command-line options. 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 part of the work environment, add a 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:

unaliasnewname

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

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 login sessions.

You can set the number of commands saved in the history file 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 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, using built-in Korn shell editors such as vi or emacs, or the built-in fc command, or the complete vi editor. 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 provided with the Korn shell provide a subset of the full functionality available with the UNIX vi editor. You can access the vi editor to edit a command by entering the command, pressing ENTER, and then typing vi . This will allow 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 11.12 lists the operators.

Table 11.12 Arithmetic and Logical Operators

Operator

Description

+

Plus

-

Minus

*

Multiply

/

Divide (with truncation)

%

Remainder

<<

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 line 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 Korn shell is not running by entering the following command:

shfile-name

Windows NT does not support execution of a script invoked from the command line only by file name; under UNIX, scripts may be executed in this manner if the path and file name of the shell are specified on the first line of the script, like the following:

#!/bin/sh

Each file or file name extension must be associated with a program. In particular, .sh or .ksh can be associated with Korn shell.

Job Control

You can use job control to run a command in the foreground or the background or 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 very 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 11.13 lists the job control commands supported by Services for UNIX.

Table 11.13 Job Control Commands

Command

Description

jobsl

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   &

killjob-number

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

UNIX Utilities

The following UNIX utilities are available as part of Services for UNIX. For more information on these commands, see Services for UNIX Help.

Table 11.14 UNIX Utilities

UNIX Command

Description

sh

Invokes Korn shell.

basename

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.

cat

Concatenates and displays file.

chmod

Changes or assigns the permissions mode of a file.

chown

Changes the owner of a file.

cp

Copies files.

dirname

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

find

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

grep

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

head

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

ln

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

ls

Lists contents of a directory.

mkdir

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

more

A filter that displays the contents of a text file on the terminal, one screen at a time.

mv

Moves file name to target.

rm

Removes entry for file from a directory.

rmdir

Removes directory.

sed

Stream editor. Copies named file name to standard output, edited according to a script of commands.

sort

Sorts lines of all named files together and writes result to standard output.

tail

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

tee

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

touch

Updates access time or modification time of a file.

uniq

Reports repeated lines in a file.

wc

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

vi

Screen-oriented visual display editor based on ex.

perl

An interpreted language used for scanning text files, extracting information from those files, and printing 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 some basic information to get you started using vi. Once you understand the basic mechanics of using vi, you can explore its functionality. (The mechanics are simple; the details can seem obscure at first.) For further details on the complete functionality of vi, consult any of the available print or online sources. In addition, consult the Services for UNIX online help for vi .

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

vifile-name

and press ENTER.

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

note-icon

Note

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-hand corner of the screen, a column of tildes along the left margin of the file representing blank lines (if there are any 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 the indication of input mode if 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:

:wq

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 11.15 Starting and Quitting vi

Command

Description

vifile-name

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

vi -rfile-name

Recovers a file after a system crash and edit it

:q

Quits vi if no changes have been made

:q!

Quits vi without saving changes

:wq

Writes (save changes) and quit vi

After you have created a file using vi, you can move throughout the file. As the size of the size of the file increases, the ability to move at will to any place in the file becomes increasingly useful using the following commands in command mode.

Table 11.16 Moving the Cursor in Command Mode

Command

Description

Spacebar

Moves the cursor forward one character

Backspace

Moves the cursor back one character

l

Moves the cursor one character to the right

h

Moves the cursor one character to the left

j

Moves the cursor down one line

k

Moves the cursor up one line

Ctrl-d

Scrolls down half a screen

Ctrl-u

Scrolls up half a screen

Ctrl-f

Scrolls down one screen

Ctrl-b

Scrolls up one screen

n G

Moves the cursor to line n

G

Moves the cursor to the end of the file

Many ways are provided for inserting and changing text that allow for detailed control.

Table 11.17 Input Mode

Command

Description

a

Insert text after the cursor

A

Insert text at end of the current line

i

Insert text before the cursor

I

Insert text before the current line

o

Open a line in the text below the cursor

O

Open a line in the text above the cursor

Table 11.18 Changing Text

Command

Description

r

Replace the current character with the next character typed; return to Command mode.

R

Replace text beginning with the current character, until ESC invoked.

cc

Changes the entire current line to the new text entered.

cw

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

s

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

S

Substitutes the entire current line with the new text entered.

Table 11.9 shows the possible ways to delete text in vi.

Table 11.19 Deleting Text

Command

Description

D

Delete from cursor to the end of the line

x

Delete the current character

dd

Delete the current line

You can yank and put — that is, copy and paste — text within a file and between files. The yank commands copies specified 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 11.20 Yank and Put Commands

Command

Description

yy or Y

Yanks (copies) the current line.

5yy

Yanks five lines.

p

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

P

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

You can search for a character string within the file. Remember that 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 11.21 Search Commands

Command

Description

/pattern

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

/

Repeats the previous forward search.

?pattern

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

?

Repeats the previous backward search.

Global pattern substitution, a powerful tool, is available from the command line.

The command takes the form:

:s/string/replacement/g

and press ENTER. In this command, string represents any regular expression that you want to search for, replacement represents the text that will replace 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:

:s/string/replacement/gc

Here are a few of the many other tools available in vi.

Table 11.22 Other Useful Commands

Command

Description

:sh

Escape to the shell to run a command

:!command

Run one command

u

Undo last change

U

Restore the last deleted line

~

Toggle the case of the current character

xp

Transpose the character in the current cursor position with the next character

.

Repeat last change