ebook img

Mastering Unix shell scripting PDF

701 Pages·2003·1.968 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Mastering Unix shell scripting

Mastering Unix Shell Scripting Randal K. Michael 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, MA01923, (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: [email protected]. 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 This book is dedicated to My Wife Robin, and the girls, Andrea and Ana Acknowledgments The information that I gathered together in this book is the result of working with some of the most talented UNIX professionals on the topic. I have enjoyed every minute of my association with these UNIX gurus and it has been my pleasure to have the opportunity to gain so much knowledge from the pros. I want to thank every one of these experts for asking and answering questions over the last fifteen years. If my brother, Jim, had not kept telling me, “you should write a book,” after querying me for UNIX details on almost a weekly basis, I doubt this book would have ever been writ- ten. So, thanks Jim! I especially want to thank Jack Renfro at Daimler/Chrysler Corporation for giving me my first shell scripting project so long ago. I had to start with the manpages, but that is how I learned to dig deep to get an answer. Since then I have been on a mission to automate, through shell scripting, everything on every system that I come in contact with. I certainly value the years that I was able to work with Jack. I must also thank the talented people at Wiley Publishing. Margaret Eldridge started me on this project by letting me do my own thing, and Carol Long kept me going. Scott Amerman kept me on schedule, and Angela Smith did the edits that make my writing flow with ease. It has been a valuable experience for me to work with such a fine group of professionals at Wiley. I also want to thank Carole McClendon at Waterside Produc- tions for all of the support on this project. Carole is the best Agent that anyone could ever ask for. She is a true professional with the highest ethics. Of course my family had a lot to do with my success on this and every project. I want to thank Mom, Gene, Jim, Marcia, Rusty, Mallory, and Anica. I want to thank my Wife Robin for her understanding and support. The girls, Andrea and Ana, always keep a smile on my face, and Steve is always on my mind. I could not have written this book without the support of all of these people and the many others that remain unnamed. It has been an honor! vii Contents Acknowledgments vii Introduction xix Chapter1 Scripting Quick Start and Review 1 Case Sensitivity 1 Unix Special Characters 2 Shells 2 Shell Scripts 2 Functions 2 AFunction Has the Form 3 Running a Shell Script 3 Declare the Shell in the Shell Script 3 Comments and Style in Shell Scripts 4 Control Structures 6 Using break, continue, exit, and return 9 Here Document 9 Syntax for a Here Document 9 Shell Script Commands 10 Symbol Commands 13 Variables 13 Command-Line Arguments 13 Shift Command 14 Special Parameters $* and $@ 15 Special Parameter Definitions 15 Double Quotes “, Forward Tics ‘, and Back Tics ` 16 Math in a Shell Script 17 Operators 17 Built-in Mathematical Functions 18 ix x Contents File Permissions, suid and sgid Programs 18 chmod Command Syntax for Each Purpose 19 Running Commands on a Remote Host 20 Setting Traps 21 User Information Commands 22 who Command 22 w Command 22 last Command 22 ps Command 23 Communicating with Users 23 Uppercase or Lowercase Text for Easy Testing 24 Check the Return Code 25 Time-Based Script Execution 27 cron tables 27 Cron Table Entry Syntax 27 Wildcards 28 at Command 28 Output Control 28 Silent Running 28 Using getopts to Parse Command-Line Arguments 29 Making a Co-Process with Background Function 30 Catching a Delayed Command Output 32 Fastest Ways to Process a File Line -by Line 33 Mail Notification Techniques 34 Using the mail and mailx Commands 34 Using the sendmail Command to Send Outbound Mail 34 Creating a Progress Indicator 35 ASeries of Dots 35 ARotating Line 35 Creating a Psuedo-Random Number 36 Checking for Stale Disk Partitions in AIX 37 Automated Host Pinging 37 Highlighting Specific Text in a File 38 Keeping the Printers Printing 38 AIX “Classic” Printer Subsystem 38 System V Printing 39 Automated FTPFile Transfer 39 Capturing a List of Files Larger than $MEG 39 Capturing a User’s Keystrokes 40 Using the bc Utility for Floating-Point Math 40 Number Base Conversions 41 Using the typeset Command 41 Using the printf Command 41 Create a Menu with the select Command 42 Sending Pop-Up Messages to Windows 43 Removing Repeated Lines in a File 43 Removing Blank Lines from a File 44 Contents xi Testing for a Null Variable 44 Directly Access the Value of the Last Positional Parameter, $# 45 Remove the Columns Heading in a Command Output 45 Arrays 46 Loading an Array 46 Testing a String 47 Summary 51 Chapter2 Twelve Ways to Process a File Line by Line 53 Command Syntax 53 Using File Descriptors 54 Creating a Large File to Use in the Timing Test 54 Twelve Methods to Parse a File Line by Line 56 Method 1: cat $FILENAME | while read LINE 57 Method 2: while read $FILENAME from Bottom 58 Method 3: while_line_LINE_Bottom 58 Method 4: cat $FILENAME | while LINE=`line` 59 Method 5: cat $FILENAME | while line LINE 60 Method 6: while LINE=`line` from the Bottom 61 Method 7: cat $FILENAME | while LINE=$(line) 61 Method 8: while LINE=$(line) from the Bottom 62 Method 9: while read LINE Using File Descriptors 63 Method 10: while LINE=’line’ Using File Descriptors 64 Method 11: while LINE=$(line) Using File Descriptors 65 Method 12: while line LINE Using File Descriptors 66 Timing Each Method 66 Timing Script 67 Timing Data for Each Method 73 Timing Command Substitution Methods 77 Summary 78 Chapter 3 Automated Event Notification 79 Basics of Automating Event Notification 79 Using the mail and mailx Commands 80 Problems with Outbound Mail 82 Create a “Bounce” Account with a .forward File 82 Using the sendmail Command to Send Outbound Mail 83 Dial-Out Modem Software 84 SNMPTraps 85 Summary 86 Chapter 4 Progress Indicator Using a Series of Dots, a Rotating Line, or a Countdown to Zero 87 Indicating Progress with a Series of Dots 87 Indicating Progress with a Rotating Line 89 Creating a Countdown Indicator 91 Other Options to Consider 95 Summary 96

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.