Unix Shell Programming Is a Tutorial Aimed at Helping Unix and Linux Users Get Optimal Performance out of Their Operating out of Their Operating System
Total Page:16
File Type:pdf, Size:1020Kb
• Table of Contents • Index Unix® Shell Programming, Third Edition By Stephen G. Kochan, Patrick Wood Publisher: Sams Publishing Pub Date: February 27, 2003 ISBN: 0-672-32490-3 Pages: 456 Slots: 1 Unix Shell Programming is a tutorial aimed at helping Unix and Linux users get optimal performance out of their operating out of their operating system. It shows them how to take control of their systems and work efficiently by harnessing the power of the shell to solve common problems. The reader learns everything he or she needs to know to customize the way a Unix system responds. The vast majority of Unix users utilize the Korn shell or some variant of the Bourne shell, such as bash. Three are covered in the third edition of Unix Shell Programming. It begins with a generalized tutorial of Unix and tools and then moves into detailed coverage of shell programming. Topics covered include: regular expressions, the kernel and the utilities, command files, parameters, manipulating text filters, understanding and debugging shell scripts, creating and utilizing variables, tools, processes, and customizing the shell. • Table of Contents • Index Unix® Shell Programming, Third Edition By Stephen G. Kochan, Patrick Wood Publisher: Sams Publishing Pub Date: February 27, 2003 ISBN: 0-672-32490-3 Pages: 456 Slots: 1 Copyright About the Authors We Want to Hear from You! Reader Services Chapter 1. Introduction Chapter 2. A Quick Review of the Basics Some Basic Commands Working with Files Working with Directories Filename Substitution Standard Input/Output and I/O Redirection Pipes Standard Error More on Commands Command Summary Exercises Chapter 3. What Is the Shell? The Kernel and the Utilities The Login Shell Typing Commands to the Shell The Shell's Responsibilities Chapter 4. Tools of the Trade Regular Expressions cut paste sed tr grep sort uniq Exercises Chapter 5. And Away We Go Command Files Variables Built-in Integer Arithmetic Exercises Chapter 6. Can I Quote You on That? The Single Quote The Double Quote The Backslash Command Substitution Exercises Chapter 7. Passing Arguments The $# Variable The $* Variable A Program to Look Up Someone in the Phone Book A Program to Add Someone to the Phone Book A Program to Remove Someone from the Phone Book ${n} The shift Command Exercises Chapter 8. Decisions, Decisions Exit Status The test Command The else Construct The exit Command The elif Construct The case Command The Null Command : The && and || Constructs Exercises Chapter 9. 'Round and 'Round She Goes The for Command The until Command More on Loops The getopts Command Exercises Chapter 10. Reading and Printing Data The read Command The printf Command Exercises Chapter 11. Your Environment Local Variables Exported Variables PS1 and PS2 HOME, James Your PATH Your Current Directory More on Subshells Your .profile File The TERM Variable The TZ Variable Exercises Chapter 12. More on Parameters Parameter Substitution The $0 Variable The set Command The IFS Variable The readonly Command The unset Command Exercises Chapter 13. Loose Ends The eval Command The wait Command The trap Command More on I/O Functions The type Command Exercises Chapter 14. Rolo Revisited Design Considerations rolo add lu display rem change listall Sample Output Exercises Chapter 15. Interactive and Nonstandard Shell Features Getting the Right Shell The ENV File Command-Line Editing Command History The vi Line Edit Mode The Line Edit Mode Other Ways to Access Your History Functions Integer Arithmetic The alias Command Arrays Job Control The Restricted Shell rsh Miscellaneous Features Compatibility Summary Exercises Appendix A. Shell Summary Startup Commands Comments Parameters and Variables Command Re-entry Quoting Filename Substitution I/O Redirection Exported Variables and Subshell Execution Functions Job Control Command Summary Appendix B. For More Information Online Documentation Documentation on the Web Books Index Copyright Copyright © 2003 by Sams Publishing All rights reserved. No part of this book shall be reproduced, stored in a retrieval system, or transmitted by any means, electronic, mechanical, photocopying, recording, or otherwise, without written permission from the publisher. No patent liability is assumed with respect to the use of the information contained herein. Although every precaution has been taken in the preparation of this book, the publisher and author assume no responsibility for errors or omissions. Nor is any liability assumed for damages resulting from the use of the information contained herein. Library of Congress Catalog Card Number: 2002115932 Printed in the United States of America First Printing: March 2003 06 05 04 03 4 3 2 1 Trademarks All terms mentioned in this book that are known to be trademarks or service marks have been appropriately capitalized. Sams Publishing cannot attest to the accuracy of this information. Use of a term in this book should not be regarded as affecting the validity of any trademark or service mark. Warning and Disclaimer Every effort has been made to make this book as complete and as accurate as possible, but no warranty or fitness is implied. The information provided is on an "as is" basis. The authors and the publisher shall have neither liability nor responsibility to any person or entity with respect to any loss or damages arising from the information contained in this book. Credits Acquisitions Editor Katie Purdum Development Editor Scott Meyers Managing Editor Charlotte Clapp Copy Editor Geneil Breeze Indexer Erika Millen Proofreader Jessica McCarty Technical Editor Michael Watson Interior Designer Gary Adair Cover Designer Gary Adair Page Layout Susan Geiselman Dedication To my father, Harry Wood —Patrick Wood To Gregory, Linda, and Julia for giving meaning to my life —Stephen G. Kochan About the Authors Stephen G. Kochan is the owner of TechFitness, a technology-based fitness company. Prior to that, he was president and CEO of Pipeline Associates, a company specializing in color printing software. Mr. Kochan is the author of several best-selling books on Unix and C programming, including the best-selling Programming in C. He also acted as Series Editor for the Hayden Unix System Library. Patrick Wood is the CTO of the New Jersey location of Electronics for Imaging. He was a member of the technical staff at Bell Laboratories when he met Mr. Kochan in 1985. Together they founded Pipeline Associates, Inc., a Unix consulting firm, where he was the Vice President. They coauthored Exploring the Unix System, Unix System Security, Topics in C Programming, and Unix Shell Programming. We Want to Hear from You! As the reader of this book, you are our most important critic and commentator. We value your opinion and want to know what we're doing right, what we could do better, what areas you'd like to see us publish in, and any other words of wisdom you're willing to pass our way. You can email or write me directly to let me know what you did or didn't like about this book—as well as what we can do to make our books stronger. Please note that I cannot help you with technical problems related to the topic of this book, and that due to the high volume of mail I receive, I might not be able to reply to every message. When you write, please be sure to include this book's title and author as well as your name and phone or email address. I will carefully review your comments and share them with the author and editors who worked on the book. Email: [email protected] Mail: Mark Taber Associate Publisher Sams Publishing 201 West 103rd Street Indianapolis, IN 46290 USA Reader Services For more information about this book or others from Sams Publishing, visit our Web site at www.samspublishing.com. Type the ISBN (excluding hyphens) or the title of the book in the Search box to find the book you're looking for. Chapter 1. Introduction It's no secret that the Unix operating system has emerged as a standard operating system. For programmers who have been using Unix for many years now, this came as no surprise: The Unix system provides an elegant and efficient environment for program development. After all, this is what Dennis Ritchie and Ken Thompson strived for when they developed Unix at Bell Laboratories in the late 1960s. One of the strongest features of the Unix system is its wide collection of programs. More than 200 basic commands are distributed with the standard operating system. These commands (also known as tools) do everything from counting the number of lines in a file, to sending electronic mail, to displaying a calendar for any desired year. But the real strength of the Unix system comes not entirely from this large collection of commands but also from the elegance and ease with which these commands can be combined to perform far more sophisticated functions. To further this end, and also to provide a consistent buffer between the user and the guts of the Unix system (the kernel), the shell was developed. The shell is simply a program that reads in the commands you type and converts them into a form more readily understood by the Unix system. It also includes some fundamental programming constructs that let you make decisions, loop, and store values in variables. The standard shell distributed with Unix and Linux systems derives from AT&T's distribution, which evolved from a version originally written by Stephen Bourne at Bell Labs. Since then, the IEEE created standards based on the Bourne shell and the other more recent shells. The current version of this standard as of this revision is the Shell and Utilities volume of IEEE Std 1003.1-2001, also known as the POSIX standard. This shell is what we propose to teach you about in this book.