www.it-ebooks.info Randal K. Michael Mastering Unix Shell Scripting Free & Share & Open www.it-ebooks.info www.it-ebooks.info Dear Valued Customer, We realize you’re a busy professional with deadlines to hit. Whether your goal is to learn a new technology or solve a critical problem, we want to be there to lend you a hand. Our primary objective is to provide you with the insight and knowledge you need to stay atop the highly competitive and ever- changing technology industry. Wiley Publishing, Inc., offers books on a wide variety of technical categories, including security, data warehousing, software development tools, and networking — everything you need to reach your peak. Regardless of your level of expertise, the Wiley family of books has you covered. • For Dummies – The fun and easy way to learn • The Weekend Crash Course –The fastest way to learn a new tool or technology • Visual – For those who prefer to learn a new topic visually • The Bible – The 100% comprehensive tutorial and reference • The Wiley Professional list – Practical and reliable resources for IT professionals The book you hold now, Mastering Unix Shell Scripting, is the first book to provide end-to-end scripting solutions that will solve real-world system administration problems for those who have to automate these often complex and repetitive tasks. Starting with a sample task and targeting the most common Unix systems: Solaris, Linux, AIX, and HP-UX with specific command structures, this book will save precious time with hands-on detail. The companion Web site contains all the timesaving scripts from the book. Our commitment to you does not end at the last page of this book. We’d want to open a dialog with you to see what other solutions we can provide. Please be sure to visit us at www.wiley.com/compbooks to review our complete title list and explore the other resources we offer. If you have a comment, suggestion, or any other inquiry, please locate the “contact us” link at www.wiley.com. Thank you for your support and we look forward to hearing from you and serving your needs again in the future. Sincerely, Richard K. Swadley Vice President & Executive Group Publisher Wiley Technology Publishing WILEY advantage The Free & Share & Open www.it-ebooks.info www.it-ebooks.info Mastering Unix Shell Scripting Free & Share & Open www.it-ebooks.info www.it-ebooks.info Randal K. Michael Mastering Unix Shell Scripting Free & Share & Open www.it-ebooks.info Publisher: Robert Ipsen Executive Editor: Carol Long Developmental Editor: Scott Amerman Managing Editor: Angela Smith Text Design & Composition: Wiley Composition Services This book is printed on acid-free paper. ∞ Copyright © 2003 by Randal K. Michael. All rights reserved. Published by Wiley Publishing, Inc., Indianapolis, Indiana Published simultaneously in Canada 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 Section 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, Inc., 222 Rose- wood Drive, Danvers, MA 01923, (978) 750-8400, fax (978) 750-4470. Requests to the Pub- lisher for permission should be addressed to the Legal Department, Wiley Publishing, Inc., 10475 Crosspoint Blvd., Indianapolis, IN 46256, (317) 572-3447, fax (317) 572-4447, E-mail: PERMCOORDINATOR@WILEY.COM. Limit of Liability/Disclaimer of Warranty: While the publisher and author have used their best efforts in preparing this book, they make no representations or warranties with respect to the accuracy or completeness of the contents of this book and specifically disclaim any implied warranties of merchantability or fitness for a particular purpose. No warranty may be created or extended by sales representatives or written sales materials. The advice and strategies contained herein may not be suitable for your situation. You should consult with a professional where appropriate. Neither the publisher nor author shall be liable for any loss of profit or any other commercial damages, including but not limited to special, inci- dental, consequential, or other damages. For general information on our other products and services please contact our Customer Care Department within the United States at (800) 762-2974, outside the United States at (317) 572-3993 or fax (317) 572-4002. Trademarks: Wiley, the Wiley Publishing logo and related trade dress are trademarks or registered trademarks of Wiley Publishing, Inc., in the United States and other countries, and may not be used without written permission. All other trademarks are the property of their respective owners. Wiley Publishing, Inc., is not associated with any product or ven- dor mentioned in this book. 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 Cataloging-in-Publication Data: ISBN: 0-471-21821-9 Printed in the United States of America 10 9 8 7 6 5 4 3 2 1 www.it-ebooks.info This book is dedicated to My Wife Robin, and the girls, Andrea and Ana Free & Share & Open www.it-ebooks.info [...]... Shell Script A shell script can be executed in the following ways: ksh shell_ script_name will create a Korn shell and execute the shell_ script_name in the newly created Korn shell environment shell_ script_name will execute shell_ script_name if the execution bit is set on the file (see the man page on the chmod command) The script will execute in the shell that is declared on the first line of the shell. .. shell script If no shell is declared on the first line of the shell script, it will execute in the default shell, which is the user’s system-defined shell Executing in an unintended shell may result in a failure and give unpredictable results Declare the Shell in the Shell Script Declare the shell! If we want to have complete control over how a shell script is going to run and in which shell it is to execute,... Chapter 1 Free & Share & Open Scripting Quick Start and Review Case Sensitivity Unix Special Characters Shells Shell Scripts Functions A Function Has the Form Running a Shell Script 1 1 2 2 2 2 3 3 Declare the Shell in the Shell Script 3 Comments and Style in Shell Scripts Control Structures Using break, continue, exit, and return Here Document 4 6 9 9 Syntax for a Here Document 9 Shell Script Commands Symbol... ] ` ‘ “ + Shells A shell is an environment in which we can run our commands, programs, and shell scripts There are different flavors of shells, just as there are different flavors of operating systems Each flavor of shell has its own set of recognized commands and functions This book works entirely with the Korn shell Korn Shell /bin/ksh OR /usr/bin/ksh Shell Scripts The basic concept of a shell script... benefit Every script is written using Korn shell, which is the industry standard for scripting solutions in Unix, although some may argue this point There are several versions of the Korn shell shipped with Unix, depending on the Unix operating system (OS) and the version of the OS release I have found that the shell scripts in this book will run on any of the Korn shell versions without any modification... MUST declare the shell in the very first line Free & Share & Open www.it-ebooks.info 3 4 Chapter 1 of the script If no shell is declared, the script will execute in the default shell, defined by the system for the user executing the shell script If the script was written, for example, to execute in Korn shell ksh, and the default shell for the user executing the shell script is the C shell csh, then... of writing a shell script to solve real-world Unix problems and tasks The shell scripts range from using a pseudorandom number generator to create pseudo-random passwords to checking for full filesystems on Unix machines and to sending pop-up messages to Windows desktops The details required to write these shell scripts include using good style and providing good comments throughout the shell script... this book The shell scripts presented in this book are complete shell scripts, which is one of the things that sets this book apart from other shell scripting books on the market The solutions are explained thoroughly, with each part of the shell script explained in minute detail down to the philosophy and mindset of the author How This Book Is Organized Each chapter starts with a typical Unix challenge... build options into the shell script to give the end user more flexibility on the command line When a shell script has to be rewritten for each operating system, a combined shell script is shown at the end of the chapter that joins the Unix flavor differences together into one shell script that will run on all of the OS flavors To do this last step we query the system for the Unix flavor using the uname... good idea to make your default shell environment the Korn shell (ksh); the standard shell on Linux is the Bourne Again shell (bash) shell, and some others use Bourne shell (sh) as the default You can find your default shell by entering echo $SHELL from the command line None of the shell scripts in this book requires a graphical terminal, but it sure does not hurt to have GNOME, CDE, KDE2, or X-Windows . Open www.it-ebooks.info www.it-ebooks.info Mastering Unix Shell Scripting Free & Share & Open www.it-ebooks.info www.it-ebooks.info Randal K. Michael Mastering Unix Shell Scripting Free & Share. xix Chapter 1 Scripting Quick Start and Review 1 Case Sensitivity 1 Unix Special Characters 2 Shells 2 Shell Scripts 2 Functions 2 A Function Has the Form 3 Running a Shell Script 3 Declare the Shell. reliable resources for IT professionals The book you hold now, Mastering Unix Shell Scripting, is the first book to provide end-to-end scripting solutions that will solve real-world system administration