Microsoft ® Windows ® Shell Script Programming ™ This page intentionally left blank Microsoft ® Windows ® Shell Script Programming ™ JERRY LEE FORD, JR. ANDY HARRIS, S eries Editor © 2004 by Premier Press, a division of Course Technology. SVP, Retail Strategic Market Group: All rights reserved. No part of this book may be reproduced Andy Shafran or transmitted in any form or by any means, electronic or Publisher: mechanical, including photocopying, recording, or by any Stacy L. Hiquet information storage or retrieval system without written permission from Premier Press, except for the inclusion Senior Marketing Manager: of brief quotations in a review. Sarah O’Donnell The Premier Press logo and related trade dress Marketing Manager: are trademarks of Premier Press and may not be Heather Hurley used without written permission. Manager of Editorial Services: Microsoft, Windows, Notepad, and VBScript are either Heather Talbot registered trademarks or trademarks of Microsoft Cor- Aquisitions Editor: poration in the United States and/or other countries. Todd Jensen All other trademarks are the property of their respective Associate Marketing Manager: owners. Kristin Eisenzopf Important: Premier Press cannot provide software support. Project Editor/Copy Editor: Please contact the appropriate software manufacturer’s Dan Foster, Scribe Tribe technical support line or Web site for assistance. Technical Reviewer: Premier Press and the author have attempted through- Keith Davenport out this book to distinguish proprietary trademarks from descriptive terms by following the capitalization style Retail Market Coordinator: used by the manufacturer. Sarah Dubois Information contained in this book has been obtained Interior Layout: by Premier Press from sources believed to be reliable. Danielle Foster, Scribe Tribe However, because of the possibility of human or me- Cover Designer: chanical error by our sources, Premier Press, or others, Mike Tanamachi the Publisher does not guarantee the accuracy, ad- equacy, or completeness of any information and is not CD-ROM Producer: responsible for any errors or omissions or the results Keith Davenport obtained from use of such information. Readers should Indexer: be particularly aware of the fact that the Internet is an Sharon Shock ever-changing entity. Some facts may have changed since this book went to press. Proofreader: Kim Benbow ISBN: 1-59200-085-1 Library of Congress Catalog Card Number: 2003094425 Printed in the United States of America 04 05 06 07 08 BH 10 9 8 7 6 5 4 3 2 1 Premier Press, a division of Course Technology 25 Thomson Place Boston, MA 02210 To Alexander, William, Molly, and Mary. k d n A n le g e t c ow m s A number of individuals deserve credit for their work on this book. I especially want to thank Todd Jensen, who served as the book’s acquisitions editor and who has worked with me on numerous other writing projects. I also want to thank the book’s project editor and copy editor, Dan Foster, for his guid- ance and suggestions. Finally, I want to acknowledge the book’s technical editor and CD-ROM developer, Keith Davenport, as well as everyone else at Premier Press for all their hard work. ut A o A o t e u h r b h t J erry Lee Ford, Jr. is an author, educator, and IT professional with over 15 years of experience in information technology, including roles as an automation analyst, technical manager, technical support analyst, auto- mation engineer, and security analyst. Jerry is a MCSE and has earned Microsoft’s MCP and MCP+ Internet certifications. In addition, he has a master’s degree in Busi- ness Administration from Virginia Commonwealth University in Richmond, Virginia. Jerry is the author of 12 other books, including Learn JavaScript in a Weekend, Learn VBScript in a Weekend, Microsoft Windows Shell Scripting and WSH Administrator’s Guide, VBScript Professional Projects, and Microsoft Windows XP Professional Administrator’s Guide. He has over 5 years of experience as an adjunct instructor teaching networking courses in Information Technology. Jerry lives in Richmond, Virginia, with his wife, Mary, and their children, William, Alexander, and Molly. te t C n n s a a o t c l n e G a Introduction xvii CHAPTER 1 Introducing Windows Shell Scripting 1 CHAPTER 2 Interacting with the Windows Shell 25 CHAPTER 3 Windows Shell Scripting Basics 57 CHAPTER 4 Storing and Retrieving Information in Variables 87 CHAPTER 5 Applying Conditional Logic 123 CHAPTER 6 Creating Loops to Process Collections of Data 155 CHAPTER 7 Creating Procedures and Subroutines 201 CHAPTER 8 Debugging and Error Handling 241 APPENDIX A Windows Shell Scripting Administrative Scripts 297 APPENDIX B What’s on the CD-ROM? 341 APPENDIX C What Next? 349 Glossary 355 Index 365 te C n n s o t Introduction ...............................................................................xvii 1 Introducing CHAPTER Windows Shell Scripting 1 Project Preview: The Knock Knock Joke .....................................2 Overview of Windows Shell Scripting ........................................4 What Can Shell Scripts Do?.....................................................6 Supported Microsoft Operating Systems...............................7 Alternatives to Windows Shell Scripting ....................................7 The Windows Script Host.........................................................7 Third-Party Scripting Languages...........................................10 Understanding the Windows Shell............................................10 Assembling Your First Windows Shell Script...........................13 Creating a Scripting Environment .............................................15 Back to the Knock Knock Joke ...................................................17 Designing the Game...............................................................17 The Final Result.......................................................................22 Summary .....................................................................................24 2 Interacting with CHAPTER the Windows Shell 25 Project Preview: The Unpredictable Command Prompt..........26 Command Shell Command Syntax...........................................27 Starting Windows Shell Sessions..............................................29 Working with the CMD Command.........................................30 Working with the START Command .....................................32
Description: