SKIP THE SHIPPING
Use code NOSHIP during checkout to save 40% on eligible eBooks, now through January 5. Shop now.
Register your product to gain access to bonus material or receive a coupon.
2006D-2
The Hands-On UNIX Learning System That's Fast, Easy & Web-charged!
Master UNIX shell programming now! Start with basic scripts and functions; then learn to execute internal and external commands; use control structures, token splitting, and environment and shell variables. Discover powerful techniques for parameter and command substitution; filename expansion and pattern matching; working with traps; I/O; testing; expression evaluation, and more. Whatever your experience, UNIX Shell Programmer's Interactive Workbook will transform you into a power shell programmer, fast!
Just the facts! No endless, boring discussions here! You'll learn hands-on, through practical exercises, self-review questions and real-world answers. Exclusive "Test Your Thinking" projects guarantee you'll go beyond rote knowledge to really master the subject! It's an integrated learning system that's proven to work!
Every UNIX Interactive Workbook is fully integrated with its own exclusive Web site, giving you all this and more:
FREE CD-ROM: CBT Systems Tutorial on UNIX Shell Programming!
Click here for a sample chapter for this book: 0130200646.pdf
(NOTE: Most chapters end with Test Your Thinking sections).
Set Up for LINUX and Other UNIX Systems. Set Up for MS Windows Systems. Create Directories for Your Programs. Set Up Your Profile and Environment File. Define and Run a Script. "Dot" a Script. Define and Run a Function.
LABS: Running the Current KornShell. Setting Up Your Interactive Environment. Editing and Running Scripts and Functions.
Identify Command Names. Identify Parameters. Document a Program's Syntax. List All Built-In Commands. Use Two Built-In Tools: print and whence. Execute External Commands. Define Aliases. Write and Run a Simple Function. Redirect stdin and stdout (). Control Clobbering! (>>and>|). Redirect stderr (>). Link File Descriptors for Redirection (n>&). Use a Here Document for stdin (<<).
LABS: Command Parts. Command Types. Redirecting Standard Input and Output.
Sequential Execution ( ; ). Parallel Execution ( & ). Pipelines ( | ). Conditional Commands && and ||. Co-Processes ( |& ). Group Commands { } and ( ). Use Groups to Control I/O Redirection. Use Groups to Control && and ||.
LABS: Command Sequences. Command Groups.
Use Proper Terminology. Evaluate Simple Variables. Assign Values to Variables Using =. Use unset to Uninitialize a Variable. Quote the Value Part of an Assignment. Quote Variable References. Quote Single Characters.
LABS: Terminology. Evaluating and Assigning Values. Quoting Variables.
Verify Your Set-Up. Pass Parameters to a Function or Script. Use Special Parameter Variables (#@*). Use set to Assign Positional Parameters. Use shift to Access Positional Parameters. Use the getopts Command. Use the optind Variable. Use the optarg Variable. Customize getopts Error Messages.
LABS: Parameters. Process Option Parameters.
Declare Numeric Variables and Assign Values. Write Arithmetic Expressions. Use Arithmetic Expansion. Control the Scope of Variables. Use Indexed Arrays. Declare and Use Associative Arrays. Use Compound Variables and Discipline Functions.
LABS: Typed Variables and Scope. Arrays and Compound Variables.
Write a Simple if, then, ... else statement. Use if to Test the Outcome of External Commands. Use an Alternative Way to Test a Command. Nest if Statements. Use [[ ... ]] In an if Statement. Use {{ ... }} Instead of if. Write Logical Expressions.
LABS: The if Command. The [[ Command ... ]] Operator.
Use while to Repeat Commands. Use break and continue to Manage a Loop. Use until, a Variant of while. Use for to Work with a List of Values. Use for to Count Iterations.
LABS: Loops: while and until. Loops: List and Arithmetic for.
Use the case Command. Use the select Command. Work with select Details.
LAB: The case and select Statements
Use Asterisk ( * ) to Match Zero or More Characters. Use Question Mark ( ? ) to Match a Single Character. Use Square Brackets ( [ ] ) to Match Character Sets. Work with Subpatterns. Use the Output of a Command as the Value of a Variable. Use Command Substitution for the Value of a Parameter. Manage Defaults and Errors. Distinguish Between Null and Unset Variables. Work with Strings and Substrings.
LABS: Patterns. Command Substitution. Parameter Expansion.
Use the read Command. Format Output. Use exec to Execute Another Program. Use exec to Open and Close Files. Use exec to Read and Write "Interesting" Devices. Intercept INT and Exit Traps. Use the Debug Trap.
LABS: The read and printf Commands. The exec Command. Intercepting and Generating Traps.
Introduction
Professional and casual programmers alike are finding that scripting languages, such as the KornShell programming language covered in this book, are valuable tools for developing powerful applications while writing a minimal amount of code.
The KornShell is both a scripting language and a command interpreter, which means that you can use the shell's programming features interactively, intermixed with commands you enter to run whatever processing programs you use in your work. UNIX calls its command interpreters shells, and there are a number of them available for UNIX systems besides the KornShell: the Bourne shell (sh), the Bourne-again shell (bash), the C shell (csh), and the TC shell (tcsh) among others. Microsoft Windows also provides rudimentary scripting features with its command interpreters for Windows 95/98 (command.com) and Windows NT/2000 (cmd.exe). The KornShell has the advantages of being a complete implementation of the POSIX shell standard and of being available for a wide range of platforms, including all major UNIX variants as well as for Microsoft Windows systems. The skills you develop as a KornShell programmer will carry over to a wide variety of programming platforms.
What You'll Need
You will need a computer with the current version of the KornShell installed in order to use this book. The first chapter will make sure your system is set up correctly, including how to test whether you have the proper version of the shell and how to get a copy if you don't already have it. Your operating system may be any major version of UNIX, including Linux, or it might be Microsoft Windows, in which case you will also need a software package that enables UNIX programs to run under windows, specifically the U/WIN package which is actively being developed by David Korn (author of the KornShell) at AT&T Research. If you do work in a Windows environment, and if you have a choice about which one to use, you should use Windows NT/2000 because it supports more of the shell's features than either of the "9x" versions. You will also need Internet access to use the book's Companion Web Site that is an integral part of this book.
How This Book is Organized
Don't just read this book! It's an interactive workbook, and the way to use it is to interact with it and with your computer as you work your way through it. Each chapter has one or more Labs, and each Lab has one or more Exercises for you to do. The Exercises are written as a series of questions for you to answer, typically after running some commands on your computer. The answers to all the Exercise questions are given at the end of each Lab, along with an explanation of the concept being developed. Even if you are able to answer a question, you should always check the answer before going on to the next one because there is often explanatory material with the answer that you will need in order to answer the next question. Put a bookmark by the answers so you can get to them easily while working through the questions.
At the end of each Lab there is a set of Self-Review Questions that you can use to check if you are on the right track in mastering the material. The answers to these questions are given in Appendix A. There are additional practice questions for each chapter at the companion Web site for this book.
After the first chapter, which deals with setting up your computer, the remainder of the book introduces the KornShell's programming constructs in what is intended to be a logical sequence of steps. This way of organizing the material was chosen for pedagogical reasons, but it has one characteristic you should be aware of: Some of the earlier material has you do things that you would do differently if you knew the material that comes later on. The goal has been to keep the focus on the part of the language you are learning rather than to introduce extraneous material in order to make each exercise a completely realistic task. The later chapters in the book will give you more realistic problems and solutions to work with.
Appendix B is an alphabetical reference guide to all of the KornShell's built-in commands, extracted from the definitive reference book on the KornShell, "The New KornShell Command and Programming Language" by Morris I. Bolsky and David G. Korn (Prentice-Hall, 1995). The book you are holding tries to cover as much of the KornShell as possible, but there are parts of the language that just don't fit into an introductory workbook. If you find that you want to learn more when you finish this book, the Bolsky and Korn book is the one to get. If you are interested in using the KornShell to develop programs with a Graphical User Interface on UNIX systems, you should also consider "Desktop KornShell Graphical Programming" by J. Stephen Pendergast, Jr. (Addison-Wesley, 1995).
Appendix C is a Glossary, with references back to the chapters. You might find this quicker than the index for tracking down key concepts.
The companion Web site for this book is at:
http://www.phptr.com/phptrinteractive
This Web site is closely integrated with the content of this workbook. Each chapter in the book ends with a set of "Test Your Thinking" exercises designed to give you practice with the material developed in that chapter, but the answers are not given in the book. Rather, you are encouraged to go to the Web site to compare your answers with those of others who are using the book. You will also find additional practice questions for each chapter there, along with hints and "coaching" comments for those questions. Finally, there is also an "Author's Corner" at the Web site where I've put some additional material that wouldn't fit in the book itself, where I will respond to questions about the book, and where I will post corrections to the book's content as needed.
I think of the Web site as a blend of traditional publishing with current trends in asynchronous learning, and I think that you will find it a lively source of help.
There is a CD-ROM packaged with this book called "UNIX: Using the Shell" published by CBT Systems Ltd. Although I was not involved in the development of this CD-ROM, it follows very much the same educational philosophy as this book. You can't just sit back and watch it go by, you have to think and answer questions as you go along. It doesn't provide the depth of coverage for the KornShell that the book does, but it does introduce you to some of the other UNIX shells, namely the Bourne and C shells. There were some technical errors in a pre-publication edition of the CD-ROM that I saw, so be sure to check the Author's Corner of this book's Web site for more information.
Conventions Used in This Book
Variable names and statement types are italicized. Computer output is boldface monospaced. Interactive input and the names of commands are Courier. In Appendix B, the conventions are a little different, where boldface is used to indicate material that you must type as shown. Control codes are shown using normal text. For example, "Control-C" means to press the letter C while holding the "Ctrl" key on the keyboard, and "Newline" means the end-of-line code produced by pressing the "Enter" key on the keyboard.
NOTE : For lines of code in the book, when text that would appear as a single line on your computer screen doesn't fit a single line on the printed page, you will see a false line break, indicated by a backslash character ( \ ), at the end of the first printed line.
Acknowledgements
This book began its life with the help of Jayne Demsky, my Prentice-Hall sales representative at Queens College, who put me in contact with the editorial people at Prentice-Hall PTR. Thank you, Jayne. The Prentice-Hall editors have been a great help, starting with Mark Taub, the series editor, and Jeff Gitlin, the series advisor. Mark and Jeff are the ones who came up with the structure of the interactive workbook series, and Ralph Moore has been the editor who made the format concrete and provided tremendous help in transforming my efforts into a real manuscript. Nick Radhuber in production gets a special vote of thanks for dealing with the time pressures imposed by my submission schedule with grace.
Kevin Wall was the technical reviewer for the book. Kevin has been invaluable in helping me get things right. His understanding of the KornShell is vast and deep, and the book has benefited greatly from his wisdom. I've learned a lot from Kevin, and hope I haven't distorted that knowledge too much in putting it in print.
I've used a computer running Windows NT while writing the book. One window holds the word processor, another one runs a KornShell login to the local computer, another two run xterm logins to KornShell logins on Solaris and DEC OSF/1 computers at Queens College. But the window that deserves special mention here is the one connected to the mail program that manages my subscriptions to the U/WIN users and U/WIN developers news groups. The people using these groups have been great sources of information about KornShell features and programming techniques. With David Korn as an active participant in these groups, they have provided a stimulating environment for working on the book. You know how the author always ends the Acknowledgements by thanking his/her family for their patience and support during the difficult time of writing a book? Well, I can vouch that it's more than just lip service going on here. Many thanks to Nancy, Josh, and Alex for their support during the book's gestation period.
Christopher Vickery
Holliswood, NY
November 1998
About the Author
Christopher Vickery is a professor of computer science at Queen's College of the City University of New York, where he teaches courses in software design and development. His home page on the Web is:
http://baggage.cs.qc.edu