Tải bản đầy đủ (.pdf) (487 trang)

IT training korn shell unix and linux programming manual

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (5.08 MB, 487 trang )

Korn Shell: Unix and Linux Programming Manual, Third Edition, The
By Anatole Olczak
Publisher: Addison Wesley
Pub Date: December 01, 2000
ISBN: 0-201-67523-4
Pages: 480
Supplier: Team FLY

If you are a Unix or Linux Shell programmer, this book will provide you with the practical advice and technical tips you will need in order to
become proficient in all aspects of the Korn Shell and enhance your programming skills. From basic introductory concepts through to
advanced programming techniques, you will learn how to:
Customize your Unix and Linux environments
Write and debug Korn Shell scripts
Fine-tune Korn Shell scripts for faster execution
Illustrated throughout with expansive sample programs and easy-to-apply examples, plus complete ready-to-run scripts, this book will
prove an indispensable guide and technical reference for the Korn Shell.
NEW! Chapter on pdksh, the public domain Korn Shell for Linux NEW! Appendices of Pdksh quick reference and Man Page NEW! CD
containing pdksh source code and evaluation version of U/WIN commands.


"This is the best Korn Shell book I've seen. I presently have at least five different books on Shell programming in Unix. I actually was able
to read this book cover to cover AND am able to use this as a reference...I found this book to be very complete."
John A. Siegel, Solution Engineer, Pershing, a Division of Donaldson Lufkin and Jenrette


Korn Shell: Unix and Linux Programming Manual, Third Edition, The
By Anatole Olczak
Publisher: Addison Wesley
Pub Date: December 01, 2000
ISBN: 0-201-67523-4
Pages: 480



Examples

Supplier: Team FLY

Copyright
List of Tables:
Preface
Korn Shell 93: The Latest Version
Acknowledgements
Miscellaneous
Conventions
Source Code Listing
Licensing Agreement
Chapter 1. Introduction
Major Features
Where To Get the Korn Shell
Which Version Do You Have?
Logging In
Invoking The Korn Shell Separately
Using The Korn Shell in Scripts
Chapter 2. Korn Shell Basics
Simple Script
Process Execution
Input/Output Redirection
File Name Substitution
Command Substitution
Tilde Substitution
Chapter 3. Variables and Parameters
Variables

Special Parameters
Variable Expansion
Array Variables
Compound Variables
Quoting
Chapter 4. Editing Commands
Terminal Requirements
Command History File
The fc Command
In-Line Editor
Chapter 5. Job Control
Manipulating Jobs


Checking Job Status
Background Jobs and I/O
Job Names
Leaving Stopped Jobs
Chapter 6. Performing Arithmetic
The let Command
The ((...)) Command
Declaring Integer Variables
Arithmetic Constants
Arithmetic Operators
Random Numbers
Chapter 7. The Environment
After You Log In
The Environment File
Environment Variables
Korn Shell Options

Aliases
Prompts
Subshells
Restricted Shell
Chapter 8. Writing Korn Shell Scripts
Executing Korn Shell Scripts
The [[...]] Command
Control Commands
Input/Output Commands
Miscellaneous Programming Features
Chapter 9. Miscellaneous Commands
The : Command
The eval Command
The export Command
The false Command
The newgrp Command
The pwd Command
The readonly Command
The set Command
The time Command
The times Command
The true Command
The ulimit Command
The umask Command
The whence Command
Appendix A. Sample .profile File
Appendix B. Sample Environment File
Appendix C. C Shell Functionality
Directory Functions
Miscellaneous Commands

Appendix D. Sample Korn Shell Scripts
Display Files - kcat
Interactive uucp - kuucp
Basename - kbasename


Dirname - kdirname
Display Files with Line Numbers -knl
Find Words - match
Simple Calculator - kcalc
Searching for Patterns in Files -kgrep
Calendar Program - kcal
Appendix E. Korn Shell Man Page
Synopsis
Description
Definitions
Commands
Variable Assignments
Comments
Aliasing
Tilde Substitution
Command Substitution
Arithmetic Substitution
Process Substitution
Parameter Expansion
Field Splitting
File Name Generation
Quoting
Arithmetic Evaluation
Prompting

Conditional Expressions
Input/Output
Environment
Functions
Discipline Functions
Jobs
Signals
Execution
Command Re-entry
In-line Editing Options
Key Bindings
Emacs Editing Mode
Vi Editing Mode
Input Edit Commands
Motion Edit Commands
Search Edit Commands
Text Modification Edit Commands
Other Edit Commands
Built-in Commands
Invocation
Rsh Only
EXIT STATUS
CAVEATS
Appendix F. Pdksh
Downloading the Source
Building Pdksh


Appendix G. Pdksh Quick Reference
Command Execution

Redirecting Input/Output
Filename Substitution
Variables
Variable Attributes
Variable Substitution
Special Parameters
Special Variables
Array Variables
Misc Substitution
Quoting
In-Line Editors
Job Control
Arithmetic
Options
Aliases
Conditional Expressions
Other [[...]] Operators
Control Commands
Commands
Functions
THE print COMMAND
THE read COMMAND
Miscellaneous
Debugging Korn Shell Scripts
Files
Example Commands
Appendix H. Pdksh Man Page
Synopsis
Description
Shell Startup

Command Syntax
Quoting
Aliases
Substitution
Parameters

Top


Korn Shell: Unix and Linux Programming Manual, Third Edition, The
By Anatole Olczak

Table of Contents

List of Tables:
2.1: Command Execution Format 17
2.2: Some I/O Redirection Operators 21
2.3: File Descriptors 23
2.4: Redirect Operators and File Descriptors 27
2.5: Basic Pattern Matching Characters 33
2.6: Other File Name Patterns 37
2.7: Tilde Substitution 43
3.1: Assigning Values to Variables 47
3.2: Assigning Values/Attributes to Variables 49
3.3: Some Variable Attributes 53
3.4: Some Preset Special Parameters 59
3.5: Variable Expansion Formats 69
3.6: More Variable Expansion Formats 73
3.7: Array Variables 75



4.1: Vi Input Mode Commands 94
4.2: Some Vi Command Mode Commands 97
4.3: Some Emacs/Gmacs In-Line Edit Commands 101
5.1: Job Control Commands 111
5.2: Job Names 113
6.1: Arithmetic Operators 117
7.1: Some Korn Shell Environment Variables 143
7.2: Some Korn Shell Options 147
7.3: Preset Aliases 151
8.1: [[...]] Positional Parameters 163
8.2: [[...]] String Operators 169
8.3: Some [[...]] File Operators 171
8.4: [[...]] Integer Operators 175
8.5: Other [[...]] Operators 177
8.6: print Escape Characters 209
8.7: print Options 213
8.8: read Options 219
8.9: Korn Shell Debugging Options 239
8.10: Some Frequently Used Signals 243
8.11: Co-Processes 249
9.1: ulimit Options 261


F.1: Pdksh Build Steps 371

Top


Korn Shell: Unix and Linux Programming Manual, Third Edition, The

By Anatole Olczak

Table of Contents


Copyright
PEARSON EDUCATION LIMITED
Head Office:
Edinburgh Gate
Harlow CM20 2JE
Tel: +44 (0)1279 623623
Fax: +44 (0)1279 431059
London Office:
128 Long Acre
London WC2E 9AN
Tel: +44 (0)20 7447 2000
Fax: +44 (0)20 7240 5771
First published in Great Britain in 1991
This edition published 2001
© Anatole Olczak 2001
The right of Anatole Olczak to be identified as the author of this work has been asserted by him in
accordance with the Copyright, Designs and Patents Act 1988.
British Library Cataloguing in Publication Data
A CIP catalogue record for this book can be obtained from the British Library
Library of Congress Cataloging in Publication Data
Applied for.
All rights reserved; no part of this publication may be reproduced, stored in a retrieval system, or
transmitted in any form or by any means, electronic, mechanical, photocopying, recording, or otherwise
without either the prior written permission of the Publishers or a licence permitting restricted copying in
the United Kingdom issued by the Copyright Licensing Agency Ltd, 90 Tottenham Court Road, London

W1P 0LP. This book may not be lent, resold, hired out or otherwise disposed of by way of trade in any


form of binding or cover other than that in which it is published, without the prior consent of the
Publishers.
The programs in this book have been included for their instructional value. The publisher does not offer
any warranties or representations in respect of their fitness for a particular purpose, nor does the
publisher accept any liability for any loss or damage arising from their use.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as
trademarks. Pearson Education Limited has made every attempt to supply trademark information about
manufacturers and their products mentioned in this book. Where those designations appear in this book
and Addison-Wesley were aware of a trademark claim, the designations have been printed in initial or
capital letters.
10 9 8 7 6 5 4 3 2 1
Printed and bound in Great Britain by Biddles Ltd, Guildford and King's Lynn.
The Publishers' policy is to use paper manufactured from sustainable forests.

Top


Korn Shell: Unix and Linux Programming Manual, Third Edition, The
By Anatole Olczak

Table of Contents


Preface
Korn Shell 93: The Latest Version
Acknowledgements
Miscellaneous

Conventions
Source Code Listing
The Korn Shell User and Programming Manual is designed to be a reference and learning tool for a
range of users - from the novice with some experience to the pro who is familiar with both the Bourne and
C shells. It contains complete technical information, as well as hands-on examples and complete
programs to help guide you and illustrate all the features of the Korn shell. This edition of the book has
been updated to cover Korn Shell 93, the latest version of the Korn shell. This book also assumes that
you are familiar with the basic Unix commands, and understand file system concepts. You should also be
able to login to a system, and enter basic commands.
If you are an experienced user, you may want to skip Chapter 1 and the first half of Chapter 2. The first
seven chapters deal primarily with interactive use, while Chapter 8 and 9 cover the programming
concepts.
The goal of this book to teach you the Korn shell, and this is done by walking you through examples. So
by the time you are finished reading the book, you'll be comfortable with it, and writing your own Korn
shell scripts.
But don't just read the book. The best way for you to learn about the Korn shell is to type in the examples
yourself. Then do some experimentation on your own by either modifying the examples or coming up with
your own commands.
Chapter 1 contains an overview of the major features in the Korn shell. It covers where to get it, how your
login shell is configured, and setting up the Korn shell to co-exist with other shells while you are on the
learning curve. It also includes brief descriptions of other related shells, including the Born Again shell
(bash), Mortice Kern shell (ksh) for PC/Windows, and the public domain Korn shell ( pdksh) for Linux.
Chapter 2 covers the Korn shell basics: how commands can work together to do other things, and some
basic shortcuts to improve productivity, and Korn shell I/O. You'll also be introduced to file name,
command, and tilde substitution: important concepts that are the basis of much of the Korn shell.
Chapter 3 teaches you about Korn shell variables, variable attributes, and parameters. You'll learn about
all the different types of variable expansion, including the substring features. Array variables and quoting
are also discussed in detail.



Chapter 4 discusses the Korn shell command history mechanism and vi and emacs in-line editors. Here
you will learn how to call up previous commands and manipulate them.
Chapter 5 shows you how to manage and manipulate multiple processes using the job control
mechanism, a feature almost directly copied from the C shell.
In Chapter 6, you will learn how to perform arithmetic with the Korn shell. It contains sections on
multi-base arithmetic, declaring integer-type variables, and random numbers, along with examples for
each type of arithmetic operator.
Chapter 7 will show you how to set up your own customized environment: from setting up the prompt how
you like it, to configuring your personal email. Korn shell options, environment variables, aliases, the
.profile file, and subshells are also covered.
In Chapter 8, you are taught how to write programs using the many Korn shell commands and features.
Executing and debugging scripts, input/output commands, positional parameters, flow control commands
such as case, for, if, select, while, and until are also discussed. Step-by-step examples are included,
and complete usable scripts are built from the bottom up. For those experienced Unix programmers,
important differences between the Korn and Bourne shells are discussed, and something else new to
Unix shell programming - performance. You'll learn a few tricks that can speed up execution of your Korn
shell scripts.
Chapter 9 covers miscellaneous commands, such as readonly, ulimit, whence, and Korn shell
functions.
Appendix A and B include a sample ready-to-use profile and environment file.
Appendix C contains the Korn shell versions of a number of C shell commands and functions.
Appendix D contains the source code listing for a number of handy ready-to-run Korn shell scripts,
including an interactive calendar program.
Appendix E contains the Korn shell man pages.
Appendix F contains information about pdksh, the public domain version of the Korn shell for Linux.
Appendix G contains the Pdksh quick reference guide, and Appendix H contains the Pdksh man page.

Top



Korn Shell: Unix and Linux Programming Manual, Third Edition, The
By Anatole Olczak

Table of Contents

Preface

Korn Shell 93: The Latest Version
This edition is based on the latest edition of the Korn shell. There have been a number of new features
and enhancements added to Korn Shell 93 including:

Datatypes:

New data types: floats and structures.

Variables:

New variable typer: compound and nameref variables.

Arrays:

Associative arrays and additional commands for array manipulation.

Functions:

Discipline functions to support further manipulation of variables.

String Manipulation:
Search, replace, and substring operators.


Top


Korn Shell: Unix and Linux Programming Manual, Third Edition, The
By Anatole Olczak

Table of Contents

Preface

Acknowledgements
Thanks to David Korn for developing the Korn shell, Steven Bourne for the Bourne shell, and Bill Joy for
the C shell. Other thanks to Mike Veach and Pat Sullivan for contributing to the development of the Korn
shell, and Mark Horton, Bill Joy (again), and Richard Stallman for developing the vi and emacs editors
which were used in the development of the Korn shell.
Special thanks to Peter Collinson, Cynthia Duquette, Ian Jones, Peter Kettle, Heather Prenatt, the ASP
staff, Aspen Technologies, O'Reilly & Associates (who reviewed the initial draft of this book before
publishing their own Korn shell book!), James Lamm, Darian Edwards and others for reviewing drafts of
this book.

Top


Korn Shell: Unix and Linux Programming Manual, Third Edition, The
By Anatole Olczak

Table of Contents

Preface


Miscellaneous
The information and material has been tested and verified to the best of our ability. This does not mean
that it is always 100% correct! Subtle differences and variations may exist between implementations and
features may have changed. And of course there may even be bugs or mistakes! Please send us any
comments or suggestions for future editions, along with information about your environment. Please visit
our Web site for more information www.aw.com/cseng.

Top


Korn Shell: Unix and Linux Programming Manual, Third Edition, The
By Anatole Olczak

Table of Contents

Preface

Conventions
For readability sake, the default $ prompt is used in the examples in this publication. Control characters
are given as Ctl followed by the character in boldface. For example, Ctl-d specifies Control-d and is
entered by pressing the d key while holding down the Control key. Special keys, like carriage-return and
backspace, are enclosed in < >'s. Long command lines are continued onto the next line using the \
character.

Top


Korn Shell: Unix and Linux Programming Manual, Third Edition, The
By Anatole Olczak


Table of Contents

Preface

Source Code Listing
If you would like the source code listing to the Korn shell scripts listed in the appendices, please visit our
web site at www.aw.com/cseng.

Top


Korn Shell: Unix and Linux Programming Manual, Third Edition, The
By Anatole Olczak

Table of Contents

Preface

Licensing Agreement
This book comes with a CD software package. By opening this package you are agreeing to be bound by
the license terms contained on the CD-ROM.

Top


Korn Shell: Unix and Linux Programming Manual, Third Edition, The
By Anatole Olczak

Table of Contents


Chapter 1. Introduction
Major Features
Where to Get the Korn Shell
Which Version Do You Have?
Logging In
Invoking the Korn Shell Separately
Using the Korn Shell in Scripts
The Korn shell is an interactive command and programming language that provides an interface to the
Unix and other systems. As an interactive command language, it is responsible for reading and executing
the commands that you enter. For example, when you type in the date command to check the system
date, your login shell is responsible for interpreting the command before it is executed. It also provides
the ability to customize your working environment. You can set up your own commands, specify
environment variables for other programs, change your command prompt, and a lot more. As a
programming language, its special commands allow you to write sophisticated programs. These
programs are called scripts in Unix shell speak and are just text files that contain programs written in the
Korn shell programming language. You can use any Unix editor, such as vi or emacs to create scripts.

Top


Korn Shell: Unix and Linux Programming Manual, Third Edition, The
By Anatole Olczak

Table of Contents

Chapter 1. Introduction

Major Features
The Korn shell offers compatibility with the Bourne shell, while providing a more robust programming
language and command interpreter. It also contains some features of the C shell. The major features of

the Korn shell are:
Improved performance. Programs written in the Korn shell can run faster than similar
programs written in the Bourne or C shells.
Bourne shell compatibility. Programs written for the Bourne shell can run under the Korn shell
without modification.
Command-line editing. Instead of backspacing or retyping, commands can be edited in vi,
emacs, or gmacs mode.
Command history. Commands are stored in a history file, which can then be modified and
re-executed or just re-executed as is. The commands are saved, up to a user-specified limit,
across multiple login sessions.
Enhanced I/O facilities. File descriptors and streams can be specified. Multiple files can be
opened at the same time and read. Menus can be formatted and processed more easily.
Added data types and attributes. Variables can now have a type, as well as size and
justification attributes.
Integer arithmetic support. Integer arithmetic can be performed in any base from two to
thirty-six using variables and constants. A wide range of arithmetic operators is supported,
including bitwise operators.
Arrays. One-dimensional and associative arrays can be used.


Improved string manipulation facilities. Substrings can be generated from the value of
strings. Strings can be converted to upper or lower case.
Regular expressions. Better support of regular expressions in variable expansion and filename
wildcards has been added.
Job control. The Korn shell job control feature is virtually the same as that of the C shell.
Programs can be stopped, restarted, or moved to and from the background. Programs can be
identified for kill with their job numbers.
Aliases. Programs, and Korn shell scripts, along with their options can be aliased to a single
name.
New options and variables. These have been added to provide more control over environment

customization.
Functions. Increases programmability by allowing code to be organized in smaller, more
manageable units, similar to procedures in other languages. Functions also allow Korn shell
programs to be stored in memory.
Enhanced directory navigation facilities. The previous directory, and home directory of any
user can be referred to without using pathnames. Components of previous pathnames can be
manipulated to generate new pathnames.
Enhanced debugging features. More robust diagnostic facilities have been provided, and
functions can be traced separately.
Other miscellaneous features. New test operators, special variables, special commands,
control commands, command-line options, and security features have also been added.

Top


Korn Shell: Unix and Linux Programming Manual, Third Edition, The
By Anatole Olczak

Table of Contents

Chapter 1. Introduction

Where To Get the Korn Shell
The Korn shell is included as an optional shell, along with the Bourne and C shells by most vendors,
including Sun for Solaris, SCO for UnixWare, Hewlett-Packard for HP-UX, and others. It is also available
as an unbundled product from many vendors.
The Desktop Korn shell (dtksh) is another version of the Korn shell that is available by many vendors as
an upgrade to Kornshell88-based versions. It is usually located in /usr/dt/bin/dtksh.
The Public Domain Korn shell, or pdksh, as the name suggests, is a public domain version of the Korn
shell. It's compatible with most any version of Unix, but is mostly used on Linux-based systems. At the

time of this writing, the current version (5.2.14) has most of the Kornshell88, as well as some of the
Kornshell93 and additional features that are not in either. For more detailed information, refer to
Appendices F H.
David Korn and AT&T offer U/WIN, a non-commercial version of the Korn shell for Windows-based
systems (NT and 98). It is based on KornShell98 and contains almost 200 of the popular Unix
commands. We've included a version in the accompanying CD, but it is also available from
/>More information including links for the source distribution for the Korn Shell and U/Win is available at this
URL: .
Mortice Kern Systems sells a version of the Korn shell for MS-DOS and Windows. There are also a
number of shareware shells that have Korn shell-like functionality.

Top


×