www.it-ebooks.info www.it-ebooks.info SHELL SCRIPTING EXPERT RECIPES FOR LINUX, BASH, AND MORE INTRODUCTION xxix PART I ABOUT THE INGREDIENTS CHAPTER 1 The History of Unix, GNU, and Linux . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 CHAPTER 2 Getting Started 15 CHAPTER 3 Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 CHAPTER 4 Wildcard Expansion 67 CHAPTER 5 Conditional Execution 83 CHAPTER 6 Flow Control Using Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 CHAPTER 7 Variables Continued . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139 CHAPTER 8 Functions and Libraries 161 CHAPTER 9 Arrays 199 CHAPTER 10 Processes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219 CHAPTER 11 Choosing and Using Shells . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255 PART I I RECIPES FOR USING AND EXTENDING SYSTEM TOOLS CHAPTER 12 File Manipulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279 CHAPTER 13 Text Manipulation 315 CHAPTER 14 Tools for Systems Administration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 353 PART II I RECIPES FOR SYSTEMS ADMINISTRATION CHAPTER 15 Shell Features 409 CHAPTER 16 Systems Administration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 427 CHAPTER 17 Presentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 459 CHAPTER 18 Data Storage and Retrieval . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .471 CHAPTER 19 Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 483 CHAPTER 20 Processes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 501 CHAPTER 21 Internationalization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .517 Continues www.it-ebooks.info PART I V REFERENCE APPENDIX Further Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 529 GLOSSARY . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 533 INDEX 539 www.it-ebooks.info Shell Scripting EXPERT RECIPES FOR LINUX, BASH, AND MORE www.it-ebooks.info www.it-ebooks.info Shell Scripting EXPERT RECIPES FOR LINUX, BASH, AND MORE Steve Parker www.it-ebooks.info Shell Scripting: Expert Recipes for Linux, Bash, and More Published by John Wiley & Sons, Inc. 10475 Crosspoint Boulevard Indianapolis, IN 46256 www.wiley.com Copyright © 2011 by Steve Parker, Manchester, England Published by John Wiley & Sons, Inc., Indianapolis, Indiana Published simultaneously in Canada ISBN: 978-0-470-02448-5 ISBN: 978-1-118-16633-8 (ebk) ISBN: 978-1-118-16632-1 (ebk) ISBN: 978-1-118-16631-4 (ebk) Manufactured in the United States of America 10 9 8 7 6 5 4 3 2 1 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, scanning or otherwise, except as permitted under Sections 107 or 108 of the 1976 United States Copyright Act, without either the prior written permission of the Publisher, or authorization through payment of the appropriate per-copy fee to the Copyright Clearance Center, 222 Rosewood Drive, Danvers, MA 01923, (978) 750-8400, fax (978) 646-8600. Requests to the Publisher for permission should be addressed to the Permissions Department, John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030, (201) 748-6011, fax (201) 748- 6008, or online at http://www.wiley.com/go/permissions. Limit of Liability/Disclaimer of Warranty: The publisher and the author make no representations or warranties with respect to the accuracy or completeness of the contents of this work and specifically disclaim all warranties, including without limitation warranties of fitness for a particular purpose. No warranty may be created or extended by sales or pro- motional materials. The advice and strategies contained herein may not be suitable for every situation. This work is sold with the understanding that the publisher is not engaged in rendering legal, accounting, or other professional services. If professional assistance is required, the services of a competent professional person should be sought. Neither the pub- lisher nor the author shall be liable for damages arising herefrom. The fact that an organization or Web site is referred to in this work as a citation and/or a potential source of further information does not mean that the author or the publisher endorses the information the organization or Web site may provide or recommendations it may make. Further, readers should be aware that Internet Web sites listed in this work may have changed or disappeared between when this work was written and when it is read. For general information on our other products and services please contact our Customer Care Department within the United States at (877) 762-2974, outside the United States at (317) 572-3993 or fax (317) 572-4002. Wiley also publishes its books in a variety of electronic formats. Some content that appears in print may not be available in electronic books. Library of Congress Control Number: 2011932268 Trademarks: Wiley, the Wiley logo, Wrox, the Wrox logo, Programmer to Programmer, and related trade dress are trade- marks or registered trademarks of John Wiley & Sons, Inc. and/or its affiliates, in the United States and other countries, and may not be used without written permission. John Wiley & Sons, Inc., is not associated with any product or vendor mentioned in this book. www.it-ebooks.info For my daughters, Bethany and Emily, and my wife, Jackie. Putting up with a professional geek is never easy, particularly when it leads to a career which often means a lot of travel and time spent away from home. Also to God, from whom comes all wisdom, intelligence, and learning. The better we understand the Creation, the better chance we have of understanding the Creator. For it is written: “I will destroy the wisdom of the wise; the intelligence of the intelligent I will frustrate. Where is the wise man? Where is the scholar? Where is the philosopher of this age? Has not God made foolish the wisdom of the world?…For the foolishness of God is wiser than man’s wisdom, and the weakness of God is stronger than man’s strength.” 1 Corinthians chapter 1, verses 19, 20, and 25 www.it-ebooks.info www.it-ebooks.info [...]... sysctl 242 242 245 245 246 251 253 Summary 254 Chapter 11: Choosing and Using Shells The Bourne Shell The KornShell The C Shell The Tenex C Shell The Z Shell The Bourne Again Shell The Debian Almquist Shell Dotfiles 255 256 256 256 257 257 257 258 258 Interactive Login Shells Interactive Non-Login Shells Non-Interactive Shells Logout Scripts 259 260 261 262 xxi www.it-ebooks.info CONTENTS Command Prompts... Further Reading Shell Tutorials and Documentation Arrays Tools Unix Flavors 529 529 530 530 531 Shell Services 531 Glossary 533 Index 539 xxviii www.it-ebooks.info Introduction The lyf so short, the craft so long to lerne — Chaucer The shell is the standard interface to every Unix and Linux system; users and administrators alike have experience with the shell, and combining commands into shell scripts... there that are worth revisiting What This Book Covers This book addresses shell scripting, with a focus on Bourne shell and POSIX compatibility, but a wide coverage of more recent developments, particularly the Bash shell, which is almost universal in GNU/Linux operating systems, and is included with most commercial Unices too The KornShell is also widely available in most such operating systems, both closed... concentrating on only one aspect of the shell Part Two covers the tools external to the shell that make shell scripts more useful; these are broken down into three chapters on text, files, and general systems administration The examples in Part Two are a bit more real-world in their nature, and a bit longer and more complex than those in Part One Part Three is a set of shell scripting recipes The hope is that... Linux consultant with 20 years’ experience with Unix, and 15 years’ experience with GNU/Linux He wrote and maintains the online shell scripting tutorial at http://steve-parker.org/sh/sh.shtml Steve provides IT consultancy services, and can also provide training courses in shell scripting as well as Unix and Linux technologies He can be contacted via http://sgpit.com/ www.it-ebooks.info www.it-ebooks.info... Up the Environment 24 The Shell Profile Aliases vim Settings 24 26 30 Summary 31 Chapter 3: Variables 33 Using Variables 33 Typing Assigning Values to Variables Positional Parameters Return Codes Unsetting Variables www.it-ebooks.info 34 35 39 42 45 CONTENTS Preset and Standard Variables BASH_ENV BASHOPTS SHELLOPTS BASH_COMMAND BASH_SOURCE, FUNCNAME, LINENO, and BASH_LINENO SHELL HOSTNAME and HOSTTYPE... The shell is actually a full programming language, with variables and functions, and also more advanced structures such as arrays (including associative arrays), and being so directly linked to the kernel, it has native file I/O primitives built into its very syntax, as well as process and job control All of the main features that Unix is best known for are available in the shell, and available to shell. .. the most out of the shell, and should have something to surprise any reader, regardless of background and experience This book is aimed at intermediate and experienced Unix and Linux administrators, and it may be of interest to other advanced users, too The book assumes that you know your way around at least one flavor of Unix-like system, and have probably already written some shell scripts, but want... discussion of Unix, Linux, and shell features Similarly, in a professional context, I have had the honor of chatting online with the key developers of specific Linux kernel features to discuss how they are implemented in different versions of the Linux kernel, none of which would be possible with a different development model Similarly, Chet Ramey, the maintainer of the bash shell, has responded to emails... such operating systems, both closed and open source How This Book Is Structured This book is in four parts; the first part covers the fundamental features and syntax of the shell; the second part looks at the tools available that a shell script can make use of; and the third part has recipes covering a fairly broad range of topics Finally, the fourth part contains reference information www.it-ebooks.info . 539 www.it-ebooks.info Shell Scripting EXPERT RECIPES FOR LINUX, BASH, AND MORE www.it-ebooks.info www.it-ebooks.info Shell Scripting EXPERT RECIPES FOR LINUX, BASH, AND MORE Steve Parker www.it-ebooks.info Shell. maintains the online shell scripting tutorial at http://steve-parker.org/sh/sh.shtml. Steve provides IT consultancy services, and can also provide training courses in shell scripting as well. www.it-ebooks.info www.it-ebooks.info SHELL SCRIPTING EXPERT RECIPES FOR LINUX, BASH, AND MORE INTRODUCTION xxix PART I ABOUT THE INGREDIENTS CHAPTER