WINDOWS 1002000 PROFESSIONAL RESOURCE KIT [Electronic resources]

Chris Aschauer

نسخه متنی -صفحه : 335/ 244
نمايش فراداده

Tools

Services for UNIX allows for central management of all included components. Services for UNIX also provides UNIX utilities and a Korn shell to automate common processes across Windows NT, Windows 2000, and UNIX platforms using scripts.

Network Management

Because all the Services for UNIX components are integrated into Microsoft Management Console (MMC) as snap-ins, you can centrally manage them from a computer running Windows 2000 Professional.

To access Services for UNIX Components

    Click the Start button, point to Programs, and then click Windows Services for UNIX.

    Click Services for UNIX Administration.

    You can configure all the Services for UNIX components from this window.

UNIX Shell

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

Features 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 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 Description
_ Expands to the argument of the previously executed command.
CDPATH Defines the search path used by the cd command.
COLUMNS Defines the width of the output display for programs that read the value; for example, the text editor vi.
EDITOR Specifies a default editor for the system to call when no editor is specified.
ENV Performs parameter substitution on the value if ENV is set. When the shell is invoked, the named file runs first.
ERRNO Displays the value set by the most recently failed subroutine.
FIGNORE Contains a pattern that defines which files are ignored during file expansion.
FCEDIT Displays the editor for the fc command.
HISTFILE Displays the absolute path of the file (default.sh_histo) that contains the command history.
HISTSIZE Displays the number of commands in the history file.
HOME Contains the absolute path of your home directory, which becomes your current directory when you log on.
IFS Contains the characters used as internal field separators.
LINENO Displays the number of the line from standard input that the shell script is currently executing.
LINES Defines the number of output lines used by the select statement when printing its menu. Select writes specific words to standard error.
MAIL Contains the absolute path of the file where your mail is stored.
MAILCHECK Defines the number of seconds the shell waits before checking for new mail.
MAILPATH Contains the mailbox files where new mail notification is sent.
OLDPWD Displays the path of the previous working directory.
PATH Defines the absolute paths of the directories where the shell searches for executable files.
PPID Displays the process ID of the parent of the shell.
PS1 Contains the prompt displayed by the shell. The default Korn shell prompt is $. Other options exist.
PS2 Contains the secondary shell prompt.
PWD Contains the path of the current working directory.
RANDOM Generates a random number.
REPLY Contains user input from the select statement.
SHELL Defines the absolute path of the current shell. Is used by commands to invoke the shell.
TMOUT Defines the number of seconds the shell remains inactive before it terminates.
VISUAL Specifies a default editor that overrides the EDITOR variable.

Metacharacters

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

Character Meaning
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

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

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 command 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 the 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 the 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

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:

alias newname=´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:

unalias newname

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

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 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:

sh file-name

NOTE

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:

#!/bin/sh

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

Command Description
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 &
kill job-number 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 Description
cron Schedules tasks.
crontab Lists scheduled tasks and edits them.
cut Cuts out bytes, character, or character-delimited fields from each line in one or more files, concatenates them and writes them to standard output.
date Writes the date and time.
diff Compares two files and displays line-by-line differences.
du Prints the disk usage of a file or directory.
kill Terminates or signals processes.
nice Invokes a command with a specified scheduling priority.
od Displays files in specified formats.
paste Merges corresponding or subsequent lines of files.
perl Runs Perl programs.
printenv Prints environment variables that are set.
printf Writes formatted output.
ps Lists processes and their status.
pwd Prints the current working directory.
renice Reprioritizes a running process.
sdiff Prints differences side-by-side.
sleep Suspends execution for a specified interval.
split Splits a file into pieces.
strings Finds printable strings in an object or binary file.
su Becomes another user (or administrator).
tar Creates tape archives, and adds or extracts files from an archive.
top Shows top processes sorted by CPU usage.
tr Translates characters in input stream.
uname Prints names of the current system.
uudecode Decodes a text file into a binary file.
uuencode Encodes a binary file.
wait Waits for process completion.
which Locates command and print pathname/alias.
xargs Constructs argument lists and invoke a utility.

Table 25.18 Utilities in Services for UNIX 1.0

UNIX Command Description
sh Invokes the 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 a 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 a string. See basename.
find Recursively searches a directory hierarchy, looking for files that match a specified Boolean expression.
grep Searches files for a pattern and prints all lines containing that pattern.
head Copies first n lines of specified file names to standard output.
ln Creates a hard link to a file. Links a file name to a target by creating a directory entry that refers to the target.
ls Lists the contents of a directory.
mkdir Creates a named directory with read, write, and execute permission for every type of user.
more Filters and displays the contents of a text file on the terminal, one screen at a time.
mv Moves a file name to a target.
rm Removes an entry for a file from a directory.
rmdir Removes a directory.
sed Copies named file names to a standard output; edits according to a script of commands (a stream editor).
sort Sorts the lines of all named files, groups them, and writes the result to standard output.
tail Copies a named file to standard output, beginning at a designated place.
tee Transcribes standard input to standard output and makes copies in a file name.
touch Updates the access time or the modification time of a file.
uniq Reports on repeated lines in a file.
wc Displays a count of lines, words, or characters in a file.
vi Edits text in a screen-based environment.
perl 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:

vi file-name

and press ENTER.

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

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

: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 25.19 provides a summary of the of the commands used for starting and quitting vi.

Table 25.19 Starting and Quitting vi

Command Description
vi file-name Edits file-name (this creates a new file or edits an existing one).
vi -r file-name Recovers a file after a system failure and edits it.
:q Quits vi if no changes have been made.
:q! Quits vi without saving changes.
:wq 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

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.
nG Moves the cursor to line n.
G 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

Command Description
a Inserts text after the cursor.
A Inserts text at the end of the current line.
I Inserts text before the cursor.
I Inserts text before the current line.
o Opens a line in the text below the cursor.
O Opens a line in the text above the cursor.

Table 25.22 Changing Text

Command Description
r Replaces the current character with the next character typed; returns to Command mode.
R Replaces text beginning with the current character until you pressESC.
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 25.23 lists ways to delete text in vi.

Table 25.23 Deleting Text in vi

Command Description
D Deletes from the cursor to the end of the line.
x Deletes the current character.
dd 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

Command Description
yy or Y Yanks (copies) the current line and places it in a buffer.
5yy Yanks (copies) five lines and places them in a buffer.
P Puts (pastes) the text that is in the buffer into the line after the current one.
P 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

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.

You can access global pattern substitution from the command prompt.

The command takes the following form:

:s/string/replacement/g

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:

:s/string/replacement/gc

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

Table 25.26 Other Useful Commands

Command Description
:sh Escapes to the shell to run a command.
:!command Runs one command.
u Undoes the last change.
U Restores the last deleted line.
~ Toggles the case of the current character.
xp Transposes the character in the current cursor position with the next character.
. Repeats the last change.

Scripting

Services for UNIX includes two tools that you can use for scripting: Perl and sh.

Perl is a scripting language that is useful for automated tasks, such as processing text files by using pattern matching techniques. Perl is "open source" software. Not all Perl functions are implemented in Services for UNIX. For more information about Perl, see Services for UNIX Help.

The Korn shell provided with Services for UNIX can be used as a shell script processor. For more information about using the Korn shell for scripting, see Services for UNIX Help under the topic sh.