Sams Teach Yourself Shell Programming in 24 Hours Index Table of Contents
Total Page:16
File Type:pdf, Size:1020Kb
Sams Teach Yourself Shell Programming in 24 Hours Index Table of Contents Sams Teach Yourself Shell Programming in 24 Hours Copyright Introduction ● How This Book Is Organized ● Conventions Used in This Book ● About the Author ● Dedication ● Acknowledgments Part I Introduction to UNIX and Shell Tools Hour 1: Shell Basics ● What Is a Command? ● What Is the Shell? ● Summary ● Questions ● Terms Hour 2: Script Basics ● The UNIX System ● Shell Initialization ● Getting Help ● Summary ● Questions ● Terms Hour 3: Working with Files ● Listing Files ● Viewing the Content of a File ● Manipulating Files ● Summary ● Questions ● Terms Hour 4: Working With Directories ● The Directory Tree ● Switching Directories ● Listing Files and Directories ● Manipulating Directories ● Summary ● Questions Hour 5: Manipulating File Attributes ● File Types ● Owners, Groups, and Permissions ● Summary ● Questions Hour 6: Processes ● Starting a Process ● Listing Running Processes ● Killing a Process (kill Command) ● Parent and Child Processes ● Summary ● Questions ● Terms Part II Shell Programming Hour 7: Variables ● Defining Variables ● Unsetting Variables ● Environment Variables ● Summary ● Questions ● Terms Hour 8: Substitution ● Filename Substitution (Globbing) ● Variable Substitution ● Command and Arithmetic Substitution ● Summary ● Questions Hour 9: Quoting ● Quoting with Backslashes ● Using Single Quotes ● Using Double Quotes ● Quoting Rules and Situations ● Summary ● Questions ● Terms Hour 10: Flow Control ● The if Statement ● The case Statement ● Summary ● Questions Hour 11: Loops ● The while Loop ● The for and select Loops ● Loop Control ● Summary ● Questions ● Terms Hour 12: Parameters ● Special Variables ● Options and Arguments ● Option Parsing in Shell Scripts ● Conclusion ● Questions Hour 13: Input/Output ● Output ● Input ● File Descriptors ● Summary ● Questions ● Terms Hour 14: Functions ● Creating and Using Functions ● Sharing Data Between Functions, an Example ● Conclusion ● Questions Hour 15: Text Filters ● The head and tail Commands ● Using grep ● Counting Words ● Summary ● Questions Hour 16: Filtering Text Using Regular Expressions ● The Basics of awk and sed ● Using sed ● Summary ● Questions Hour 17: Filtering Text with awk ● What is awk? ● Using awk Features ● Summary ● Questions ● Terms Hour 18: Miscellaneous Tools ● The eval Command ● The : Command ● The type Command ● The sleep Command ● The find Command ● xargs ● The expr Command ● The bc Command ● remsh/rsh/rcmd/remote (Remote Shell) ● Summary ● Questions ● Terms Part III Advanced Topics Hour 19: Dealing with Signals ● How Are Signal Represented? ● Dealing with Signals ● Conclusion ● Questions Hour 20: Debugging ● Enabling Debugging ● Syntax Checking ● Shell Tracing ● Summary ● Questions Hour 21: Problem Solving with Functions ● Creating a Library of Functions ● Useful Functions ● Summary ● Questions ● Terms Hour 22: Problem Solving with Shell Scripts ● Moving Directories ● Maintaining an Address Book ● Summary ● Questions ● Terms Hour 23: Scripting for Portability ● Determining UNIX Versions ● Techniques for Increasing Portability ● Summary ● Questions ● Terms Hour 24: Shell Programming FAQs ● Shell and Command Questions ● Variable and Argument Questions ● File and Directory Questions ● Summary Part IV Appendixes Appendix A: Command Quick Reference ● Reserved Words and Built-in Shell Commands ● Conditional Expressions ● Arithmetic Expressions (Korn/Bash Only) ● Parameters and Variables ● Parameter Substitution ● Pattern Matching ● I/O ● Miscellaneous Command Summaries ● Regular Expression Wildcards ● Summary ● Questions Appendix B: Glossary Appendix C: Quiz Answers ● Chapter 1 ● Chapter 2 ● Chapter 3 ● Chapter 4 ● Chapter 5 ● Chapter 6 ● Chapter 7 ● Chapter 8 ● Chapter 9 ● Chapter 10 ● Chapter 11 ● Chapter 12 ● Chapter 13 ● Chapter 14 ● Chapter 15 ● Chapter 16 ● Chapter 17 ● Chapter 18 ● Chapter 19 ● Chapter 20 ● Chapter 21 ● Chapter 22 ● Chapter 23 ● Appendix A Index Sams Teach Yoursel Shell Programming in 24 Hours Index Table of Contents Sams Teach Yourself Shell Programming in 24 Hours Contents Index Copyright Previous Chapter Next Chapter Copyright Sams Teach Yourself Shell Programming in 24 Hours Copyright © 1999 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. Neither is any liability assumed for damages resulting from the use of the information contained herein. International Standard Book Number: 0-672-31481-9 Library of Congress Catalog Card Number: 98-89272 Printed in the United States of America First Printing: March 1999 01 00 99 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 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 or from the use of the programs accompanying it. Sams Teach Yourself Shell Programming in 24 Hours Contents Index Copyright Previous Chapter Next Chapter © Copyright Macmillan Computer Publishing. All rights reserved. Sams Teach Yourself Shell Programming in 24 Hours Contents Index Introduction Previous Chapter Next Chapter Sections in this Chapter: How This Book Is Organized Dedication Conventions Used in This Book Acknowledgments About the Author Previous Section Next Section Introduction In recent years, the UNIX operating system has seen a huge boost in its popularity, especially with the emergence of Linux. For programmers and users of UNIX, this comes as no surprise: UNIX was designed to provide an environment that's powerful yet easy to use. One of the main strengths of UNIX is that it comes with a large collection of standard programs. These programs perform a wide variety of tasks from listing your files to reading email. Unlike other operating systems, one of the key features of UNIX is that these programs can be combined to perform complicated tasks and solve your problems. One of the most powerful standard programs available in UNIX is the shell. The shell is a program that provides you with a consistent and easy-to-use environment for executing programs in UNIX. If you have ever used a UNIX system, you have interacted with the shell. The main responsibility of the shell is to read the commands you type and then ask the UNIX kernel to perform these commands. In addition to this, the shell provides sophisticated programming constructs that enable you to make decisions, repeatedly execute commands, create functions, and store values in variables. This book concentrates on the standard UNIX shell called the Bourne shell. When Dennis Ritche and Ken Thompson were developing much of UNIX in the early 1970s, they used a very simple shell. The first real shell, written by Stephen Bourne, appeared in the mid 1970s. The original Bourne shell has changed slightly over the years; some features were added and others were removed, but its syntax and its resulting power have remained the same. The most attractive feature of the shell is that it enables you to create scripts. Scripts are files that contain a list of commands you want to run. Because every script is contained in a file and every file has a name, scripts enable you to combine existing programs to create completely new programs that solve your problems. This book teaches you how to create, execute, modify, and debug shell scripts quickly and easily. After you get used to writing scripts, you will find yourself solving more and more problems with them. How This Book Is Organized About the Examples I assume that you have some familiarity with UNIX and know how to log in, create and edit files, and work with files and directories to a limited extent. If you haven't used UNIX in a while or you aren't familiar with one of these topics, don't worry. The first part of this book reviews this material thoroughly. This book is divided into three parts: ● Part I is an introduction to UNIX, the shell, and some common tools. ● Part II covers programming using the shell. ● Part III covers advanced topics in shell programming. Part I consists of Chapters 1 through 6. The following material covered in the individual chapters: ● Chapter 1, "Shell Basics," discusses several important concepts related to the shell and describes the different versions of the shell. ● Chapter 2, "Script Basics," describes the process of creating and running a shell script. It also covers the login process and the different modes in which the shell executes. ● Chapters 3, "Working with Files," and 4, "Working with Directories," provide an overview of the commands used when working with files and directories. These chapters show you how to list the contents of a directory, view the contents of a file, and