www.IrPDF.comwww.IrPDF.com
Blum ffirs.tex V2 - 04/04/2008 4:23pm Page iiiLinux®Command Line andShell ScriptingBibleRichard BlumWiley Publishing, Inc.w...
Blum ffirs.tex V2 - 04/04/2008 4:23pm Page iiwww.IrPDF.comwww.IrPDF.com
Blum ffirs.tex V2 - 04/04/2008 4:23pm Page iLinux®Command Line andShell ScriptingBiblewww.IrPDF.comwww.IrPDF.com
Blum ffirs.tex V2 - 04/04/2008 4:23pm Page iiwww.IrPDF.comwww.IrPDF.com
Blum ffirs.tex V2 - 04/04/2008 4:23pm Page iiiLinux®Command Line andShell ScriptingBibleRichard BlumWiley Publishing, Inc.w...
Blum ffirs.tex V2 - 04/04/2008 4:23pm Page ivLinux®Command Line and Shell Scripting BiblePublished byWiley Publishing, Inc....
Blum ffirs.tex V2 - 04/04/2008 4:23pm Page vTo all the people who’ve helped form my education. Parents,relatives, teachers,...
Blum fauth.tex V2 - 04/04/2008 4:42pm Page viAbout the AuthorRichard Blum has worked in the IT industry for over 19 years ...
Blum fcredit.tex V2 - 04/04/2008 4:46pm Page viiCreditsAcquisitions EditorJenny WatsonSenior Development EditorTom DinseTe...
Blum fcredit.tex V2 - 04/04/2008 4:46pm Page viiiwww.IrPDF.comwww.IrPDF.com
Blum CAG.tex V2 - 04/04/2008 4:33pm Page ixAcknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxvIntr...
Blum CAG.tex V2 - 04/04/2008 4:33pm Page xContents at a GlancePart V Advanced TopicsChapter 24: Using a Database ............
Blum ftoc.tex V2 - 04/04/2008 5:48pm Page xiAcknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxvInt...
Blum ftoc.tex V2 - 04/04/2008 5:48pm Page xiiContentsChapter 3: Basic bash Shell Commands . . . . . . . . . . . . . . . . ...
Blum ftoc.tex V2 - 04/04/2008 5:48pm Page xiiiContentsChapter 5: Using Linux Environment Variables . . . . . . . . . . . ....
Blum ftoc.tex V2 - 04/04/2008 5:48pm Page xivContentsThe emacs Editor .......................................................
Blum ftoc.tex V2 - 04/04/2008 5:48pm Page xvContentsAdvanced if-then Features ...............................................
Blum ftoc.tex V2 - 04/04/2008 5:48pm Page xviContentsTiming out .............................................................
Blum ftoc.tex V2 - 04/04/2008 5:48pm Page xviiContentsThe renice command ....................................................
Blum ftoc.tex V2 - 04/04/2008 5:48pm Page xviiiContentsDoing Windows ........................................................
Blum ftoc.tex V2 - 04/04/2008 5:48pm Page xixContentsCounting directory files ................................................
Blum ftoc.tex V2 - 04/04/2008 5:48pm Page xxContentsThe do-while statement ..................................................
Blum ftoc.tex V2 - 04/04/2008 5:48pm Page xxiContentsThe tcsh built-in commands .............................................
Blum ftoc.tex V2 - 04/04/2008 5:48pm Page xxiiContentsThe MySQL client interface ............................................
Blum ftoc.tex V2 - 04/04/2008 5:48pm Page xxiiiContentsThe Mutt command line ................................................
Blum ftoc.tex V2 - 04/04/2008 5:48pm Page xxivwww.IrPDF.comwww.IrPDF.com
Blum flast.tex V2 - 04/04/2008 4:49pm Page xxvFirst, all glory and praise go to God, who through His Son makes all things p...
Blum flast.tex V2 - 04/04/2008 4:49pm Page xxviwww.IrPDF.comwww.IrPDF.com
Blum fintro.tex V2 - 04/04/2008 4:51pm Page xxviiWelcome to Linux Command Line and Shell Scripting Bible. Like all books in...
Blum fintro.tex V2 - 04/04/2008 4:51pm Page xxviiiIntroductionshows how the shell fits in. After learning the basics of the ...
Blum fintro.tex V2 - 04/04/2008 4:51pm Page xxixIntroductionThe last section of the book, Part V, demonstrates how to use s...
Blum fintro.tex V2 - 04/04/2008 4:51pm Page xxxIntroductionMinimum RequirementsLinux Command Line and Shell Scripting Bible...
Blum c01.tex V2 - 04/03/2008 2:57pm Page 1The LinuxCommand LineIN THIS PARTChapter 1Starting with Linux ShellsChapter 2Get...
Blum c01.tex V2 - 04/03/2008 2:57pm Page 2www.IrPDF.comwww.IrPDF.com
Blum c01.tex V2 - 04/03/2008 2:57pm Page 3Starting with LinuxShellsIN THIS CHAPTERWhat Is Linux?Parts of the Linux kernelE...
Blum c01.tex V2 - 04/03/2008 2:57pm Page 4Part I The Linux Command LineFIGURE 1-1The Linux systemApplication SoftwareWindo...
Blum c01.tex V2 - 04/03/2008 2:57pm Page 5Starting with Linux Shells 1The kernel is primarily responsible for four main fu...
Blum c01.tex V2 - 04/03/2008 2:57pm Page 6Part I The Linux Command LineThe memory locations are grouped into blocks called...
Blum c01.tex V2 - 04/03/2008 2:57pm Page 7Starting with Linux Shells 1HugePages Free: 0HugePages Rsvd: 0Hugepagesize: 4096...
Blum c01.tex V2 - 04/03/2008 2:57pm Page 8Part I The Linux Command LineSome Linux implementations contain a table of proce...
Blum c01.tex V2 - 04/03/2008 2:57pm Page 9Starting with Linux Shells 1436 ? SW 0:00 [httpd]437 ? SW 0:00 [httpd]438 ? SW 0...
Blum c01.tex V2 - 04/03/2008 2:57pm Page 10Part I The Linux Command Lineapplications and the hardware. There are two metho...
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Advance scripting
Upcoming SlideShare
Loading in...5
×

Advance scripting

1,115

Published on

Published in: Technology, Business
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,115
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
79
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Advance scripting

  1. 1. www.IrPDF.comwww.IrPDF.com
  2. 2. Blum ffirs.tex V2 - 04/04/2008 4:23pm Page iiiLinux®Command Line andShell ScriptingBibleRichard BlumWiley Publishing, Inc.www.IrPDF.comwww.IrPDF.com
  3. 3. Blum ffirs.tex V2 - 04/04/2008 4:23pm Page iiwww.IrPDF.comwww.IrPDF.com
  4. 4. Blum ffirs.tex V2 - 04/04/2008 4:23pm Page iLinux®Command Line andShell ScriptingBiblewww.IrPDF.comwww.IrPDF.com
  5. 5. Blum ffirs.tex V2 - 04/04/2008 4:23pm Page iiwww.IrPDF.comwww.IrPDF.com
  6. 6. Blum ffirs.tex V2 - 04/04/2008 4:23pm Page iiiLinux®Command Line andShell ScriptingBibleRichard BlumWiley Publishing, Inc.www.IrPDF.comwww.IrPDF.com
  7. 7. Blum ffirs.tex V2 - 04/04/2008 4:23pm Page ivLinux®Command Line and Shell Scripting BiblePublished byWiley Publishing, Inc.10475 Crosspoint BoulevardIndianapolis, IN 46256www.wiley.comCopyright © 2008 by Wiley Publishing, Inc., Indianapolis, IndianaPublished simultaneously in CanadaISBN: 978-0-470-25128-7Manufactured in the United States of America10 9 8 7 6 5 4 3 2 1No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form orby any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permit-ted under Sections 107 or 108 of the 1976 United States Copyright Act, without either the prior writtenpermission of the Publisher, or authorization through payment of the appropriate per-copy fee to the Copy-right 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 Legal Department, Wiley Publishing,Inc., 10475 Crosspoint Blvd., Indianapolis, IN 46256, (317) 572-3447, fax (317) 572-4355, or online athttp://www.wiley.com/go/permissions.Limit of Liability/Disclaimer of Warranty: The publisher and the author make no representations orwarranties with respect to the accuracy or completeness of the contents of this work and specifically disclaimall warranties, including without limitation warranties of fitness for a particular purpose. No warranty maybe created or extended by sales or promotional materials. The advice and strategies contained herein maynot be suitable for every situation. This work is sold with the understanding that the publisher is not en-gaged 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 publisher nor the authorshall be liable for damages arising herefrom. The fact that an organization or Website is referred to in thiswork as a citation and/or a potential source of further information does not mean that the author or thepublisher endorses the information the organization or Website may provide or recommendations it maymake. Further, readers should be aware that Internet Websites listed in this work may have changed ordisappeared between when this work was written and when it is read.For general information on our other products and services or to obtain technical support, please contactour Customer Care Department within the U.S. at (800) 762-2974, outside the U.S. at (317) 572-3993 orfax (317) 572-4002.Library of Congress Cataloging-in-Publication Data is available from the publisher.Trademarks: Wiley, the Wiley logo, and related trade dress are trademarks or registered trademarks ofJohn Wiley & Sons, Inc. and/or its affiliates, in the United States and other countries, and may not be usedwithout written permission. Linux is a registered trademark of Linus Torvald. All other trademarks are theproperty of their respective owners. Wiley Publishing, Inc., is not associated with any product or vendormentioned in this book.Wiley also publishes its books in a variety of electronic formats. Some content that appears in print maynot be available in electronic books.www.IrPDF.comwww.IrPDF.com
  8. 8. Blum ffirs.tex V2 - 04/04/2008 4:23pm Page vTo all the people who’ve helped form my education. Parents,relatives, teachers, coworkers, and even anonymous posters on theInternet. Always be prepared to accept education from whereveryou find it. Always continue to learn new things. ‘‘For the LORDgives wisdom, and from his mouth come knowledge andunderstanding.” Proverbs 2:6 (NIV)www.IrPDF.comwww.IrPDF.com
  9. 9. Blum fauth.tex V2 - 04/04/2008 4:42pm Page viAbout the AuthorRichard Blum has worked in the IT industry for over 19 years as both a systems and networkadministrator. During this time he has administered Unix, Linux, Novell, and Microsoft servers,as well as helped design and maintain a 3500-user network utilizing Cisco switches and routers.He has used Linux servers and shell scripts to perform automated network monitoring, and haswritten shell scripts in just about every Unix shell environment.Rich has a bachelor of science degree in Electrical Engineering, and a master of science degreein Management, specializing in management information systems, from Purdue University. Heis the author of several Linux books, including sendmail for Linux, Running qmail, Postfix, OpenSource E-mail Security, Network Performance Open Source Toolkit, and Professional Assembly LanguageProgramming. He’s also a coauthor of Professional Linux Programming and Linux For Dummies, 8thEdition. When he’s not being a computer nerd, Rich plays bass guitar for his church worship bandand enjoys spending time with his wife, Barbara, and their two daughters, Katie Jane and Jessica.www.IrPDF.comwww.IrPDF.com
  10. 10. Blum fcredit.tex V2 - 04/04/2008 4:46pm Page viiCreditsAcquisitions EditorJenny WatsonSenior Development EditorTom DinseTechnical EditorJohn KennedyProduction EditorAngela SmithCopy EditorFoxxe Editorial ServicesEditorial ManagerMary Beth WakefieldProduction ManagerTim TateVice President and Executive GroupPublisherRichard SwadleyVice President and Executive PublisherJoseph B. WikertProject Coordinator, CoverLynsey StanfordProofreaderWord One New YorkIndexerMelanie Belkinwww.IrPDF.comwww.IrPDF.com
  11. 11. Blum fcredit.tex V2 - 04/04/2008 4:46pm Page viiiwww.IrPDF.comwww.IrPDF.com
  12. 12. Blum CAG.tex V2 - 04/04/2008 4:33pm Page ixAcknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxvIntroduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxviiPart I The Linux Command LineChapter 1: Starting with Linux Shells ...............................................................................................3Chapter 2: Getting to the Shell .......................................................................................................25Chapter 3: Basic bash Shell Commands .........................................................................................59Chapter 4: More bash Shell Commands .........................................................................................91Chapter 5: Using Linux Environment Variables ..........................................................................123Chapter 6: Understanding Linux File Permissions .......................................................................147Chapter 7: Working with Editors .................................................................................................171Part II Shell Scripting BasicsChapter 8: Basic Script Building ...................................................................................................201Chapter 9: Using Structured Commands .....................................................................................229Chapter 10: More Structured Commands ....................................................................................255Chapter 11: Handling User Input .................................................................................................285Chapter 12: Presenting Data .........................................................................................................313Chapter 13: Script Control ...........................................................................................................335Part III Advanced Shell ScriptingChapter 14: Creating Functions ....................................................................................................363Chapter 15: Adding Color to Scripts ............................................................................................385Chapter 16: Introducing sed and gawk ........................................................................................419Chapter 17: Regular Expressions ..................................................................................................447Chapter 18: Advanced sed ............................................................................................................473Chapter 19: Advanced gawk .........................................................................................................501Part IV Alternative Linux ShellsChapter 20: The ash Shell .............................................................................................................533Chapter 21: The tcsh Shell ...........................................................................................................557Chapter 22: The Korn Shell ..........................................................................................................587Chapter 23: The zsh Shell .............................................................................................................611ixwww.IrPDF.comwww.IrPDF.com
  13. 13. Blum CAG.tex V2 - 04/04/2008 4:33pm Page xContents at a GlancePart V Advanced TopicsChapter 24: Using a Database .......................................................................................................639Chapter 25: Using the Web ..........................................................................................................673Chapter 26: Using E-Mail .............................................................................................................701Chapter 27: Shell Scripts for Administrators ...............................................................................725Appendix A: Quick Guide to bash Commands ...........................................................................749Appendix B: Quick Guide to sed and gawk ................................................................................759Appendix C: Comparing Shells .....................................................................................................771Index ..............................................................................................................................................777xwww.IrPDF.comwww.IrPDF.com
  14. 14. Blum ftoc.tex V2 - 04/04/2008 5:48pm Page xiAcknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxvIntroduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxviiPart I The Linux Command LineChapter 1: Starting with Linux Shells . . . . . . . . . . . . . . . . . . . . 3What Is Linux? ........................................................................................................................3Looking into the Linux kernel ......................................................................................4The GNU utilities ........................................................................................................12The Linux desktop environment ................................................................................14Linux Distributions ................................................................................................................20Core Linux distributions .............................................................................................21Specialized Linux distributions ...................................................................................22The Linux LiveCD .......................................................................................................23Summary ................................................................................................................................24Chapter 2: Getting to the Shell . . . . . . . . . . . . . . . . . . . . . . . 25Terminal Emulation ...............................................................................................................25Graphics capabilities ...................................................................................................27The keyboard ...............................................................................................................30The terminfo Database ..........................................................................................................31The Linux Console ................................................................................................................35The xterm Terminal ..............................................................................................................36Command line parameters ..........................................................................................37The xterm main menu ................................................................................................38The VT options menu .................................................................................................41The VT fonts menu .....................................................................................................43The Konsole Terminal ...........................................................................................................45Command line parameters ..........................................................................................45Sessions ........................................................................................................................45The menu bar ..............................................................................................................48The GNOME Terminal ..........................................................................................................52The command line parameters ...................................................................................52Tabs .............................................................................................................................53The menu bar ..............................................................................................................54Summary ................................................................................................................................58xiwww.IrPDF.comwww.IrPDF.com
  15. 15. Blum ftoc.tex V2 - 04/04/2008 5:48pm Page xiiContentsChapter 3: Basic bash Shell Commands . . . . . . . . . . . . . . . . . . 59Starting the Shell ...................................................................................................................59The Shell Prompt ...................................................................................................................60The bash Manual ...................................................................................................................63Filesystem Navigation ............................................................................................................64The Linux filesystem ...................................................................................................64Traversing directories ..................................................................................................66File and Directory Listing .....................................................................................................69Basic listing ..................................................................................................................69Modifying the information presented .........................................................................71The complete parameter list .......................................................................................72Filtering listing output ................................................................................................74File Handling .........................................................................................................................75Creating files ................................................................................................................75Copying files ................................................................................................................76Linking files .................................................................................................................79Renaming files .............................................................................................................80Deleting files ................................................................................................................81Directory Handling ................................................................................................................82Creating directories .....................................................................................................82Deleting directories .....................................................................................................82Viewing File Contents ...........................................................................................................83Viewing file statistics ...................................................................................................84Viewing the file type ...................................................................................................84Viewing the whole file ................................................................................................85Viewing parts of a file .................................................................................................89Summary ................................................................................................................................90Chapter 4: More bash Shell Commands . . . . . . . . . . . . . . . . . . 91Monitoring Programs .............................................................................................................91Peeking at the processes .............................................................................................91Real-time process monitoring .....................................................................................98Stopping processes ....................................................................................................101Monitoring Disk Space ........................................................................................................104Mounting media ........................................................................................................104Using the df command .............................................................................................108Using the du command ............................................................................................109Working with Data Files .....................................................................................................110Sorting data ...............................................................................................................110Searching for data .....................................................................................................114Compressing data ......................................................................................................116Archiving data ...........................................................................................................120Summary ..............................................................................................................................121xiiwww.IrPDF.comwww.IrPDF.com
  16. 16. Blum ftoc.tex V2 - 04/04/2008 5:48pm Page xiiiContentsChapter 5: Using Linux Environment Variables . . . . . . . . . . . . . . 123What Are Environment Variables? ......................................................................................123Global environment variables ...................................................................................124Local environment variables .....................................................................................125Setting Environment Variables ............................................................................................127Setting local environment variables ..........................................................................127Setting global environment variables ........................................................................129Removing Environment Variables .......................................................................................129Default Shell Environment Variables ..................................................................................130Setting the PATH Environment Variable ............................................................................134Locating System Environment Variables ............................................................................135Login shell .................................................................................................................136Interactive shell .........................................................................................................139Non-interactive shell .................................................................................................141Variable Arrays ....................................................................................................................142Using Command Aliases .....................................................................................................143Summary ..............................................................................................................................144Chapter 6: Understanding Linux File Permissions . . . . . . . . . . . . . 147Linux Security .....................................................................................................................147The /etc/passwd file ...................................................................................................148The /etc/shadow file ..................................................................................................150Adding a new user ....................................................................................................150Removing a user ........................................................................................................153Modifying a user .......................................................................................................154Using Linux Groups ............................................................................................................157The /etc/group file .....................................................................................................157Creating new groups .................................................................................................158Modifying groups ......................................................................................................159Decoding File Permissions ..................................................................................................160Using file permission symbols ..................................................................................160Default file permissions .............................................................................................161Changing Security Settings .................................................................................................163Changing permissions ...............................................................................................163Changing ownership .................................................................................................165Sharing Files ........................................................................................................................166Summary ..............................................................................................................................168Chapter 7: Working with Editors . . . . . . . . . . . . . . . . . . . . . 171The vim Editor ....................................................................................................................171The basics of vim ......................................................................................................172Editing data ...............................................................................................................174Copy and paste ..........................................................................................................174Search and substitute ................................................................................................175xiiiwww.IrPDF.comwww.IrPDF.com
  17. 17. Blum ftoc.tex V2 - 04/04/2008 5:48pm Page xivContentsThe emacs Editor .................................................................................................................176Using emacs on the console .....................................................................................176Using emacs in X Windows ......................................................................................181The KDE Family of Editors .................................................................................................183The KWrite editor .....................................................................................................183The Kate editor ..........................................................................................................189The GNOME Editor ............................................................................................................191Starting gedit .............................................................................................................191Basic gedit features ....................................................................................................192Setting preferences ....................................................................................................193Summary ..............................................................................................................................197Part II Shell Scripting BasicsChapter 8: Basic Script Building . . . . . . . . . . . . . . . . . . . . . 201Using Multiple Commands .................................................................................................201Creating a Script File ..........................................................................................................202Displaying Messages ............................................................................................................204Using Variables ....................................................................................................................206Environment variables ...............................................................................................206User variables ............................................................................................................207The backtick ..............................................................................................................209Redirecting Input and Output ............................................................................................210Output redirection ....................................................................................................211Input redirection .......................................................................................................211Pipes .....................................................................................................................................213Performing Math ..................................................................................................................216The expr command ...................................................................................................216Using brackets ...........................................................................................................218A floating-point solution ...........................................................................................219Exiting the Script .................................................................................................................223Checking the exit status ............................................................................................223The exit command ....................................................................................................225Summary ..............................................................................................................................226Chapter 9: Using Structured Commands . . . . . . . . . . . . . . . . . 229Working with the if-then Statement ...................................................................................229The if-then-else Statement ..................................................................................................232Nesting ifs ............................................................................................................................232The test Command ..............................................................................................................233Numeric comparisons ...............................................................................................234String comparisons ....................................................................................................236File comparisons .......................................................................................................241Compound Condition Testing ............................................................................................249xivwww.IrPDF.comwww.IrPDF.com
  18. 18. Blum ftoc.tex V2 - 04/04/2008 5:48pm Page xvContentsAdvanced if-then Features ..................................................................................................250Using double parentheses .........................................................................................250Using double brackets ...............................................................................................251The case Command .............................................................................................................252Summary ..............................................................................................................................253Chapter 10: More Structured Commands . . . . . . . . . . . . . . . . . 255The for Command ...............................................................................................................255Reading values in a list .............................................................................................256Reading complex values in a list ..............................................................................257Reading a list from a variable ...................................................................................259Reading values from a command .............................................................................260Changing the field separator .....................................................................................261Reading a directory using wildcards .........................................................................262The C-Style for Command ..................................................................................................264The C language for command ..................................................................................264Using multiple variables ............................................................................................266The while Command ...........................................................................................................266Basic while format .....................................................................................................267Using multiple test commands .................................................................................268The until Command ............................................................................................................269Nesting Loops ......................................................................................................................271Looping on File Data ..........................................................................................................273Controlling the Loop ...........................................................................................................274The break command .................................................................................................275The continue Command ...........................................................................................278Processing the Output of a Loop ........................................................................................281Summary ..............................................................................................................................282Chapter 11: Handling User Input . . . . . . . . . . . . . . . . . . . . . 285Command Line Parameters .................................................................................................285Reading parameters ...................................................................................................285Reading the program name .......................................................................................288Testing parameters ....................................................................................................289Special Parameter Variables .................................................................................................290Counting parameters .................................................................................................290Grabbing all the data ................................................................................................292Being Shifty ..........................................................................................................................293Working With Options .......................................................................................................295Finding your options ................................................................................................295Using the getopt command .......................................................................................299The more advanced getopts ......................................................................................302Standardizing Options .........................................................................................................304Getting User Input ..............................................................................................................305Basic reading ..............................................................................................................306xvwww.IrPDF.comwww.IrPDF.com
  19. 19. Blum ftoc.tex V2 - 04/04/2008 5:48pm Page xviContentsTiming out .................................................................................................................307Silent reading .............................................................................................................308Reading from a file ....................................................................................................309Summary ..............................................................................................................................310Chapter 12: Presenting Data . . . . . . . . . . . . . . . . . . . . . . . 313Understanding Input and Output .......................................................................................313Standard file descriptors ...........................................................................................314Redirecting errors ......................................................................................................316Redirecting Output in Scripts .............................................................................................318Temporary redirections .............................................................................................318Permanent redirections .............................................................................................319Redirecting Input in Scripts ................................................................................................320Creating Your Own Redirection ..........................................................................................321Creating output file descriptors ................................................................................321Redirecting file descriptors ........................................................................................322Creating input file descriptors ..................................................................................323Creating a read/write file descriptor .........................................................................323Closing file descriptors ..............................................................................................324Listing Open File Descriptors .............................................................................................326Suppressing Command Output ..........................................................................................328Using Temporary Files ........................................................................................................328Creating a local temporary file .................................................................................329Creating a temporary file in /tmp .............................................................................330Creating a temporary directory .................................................................................331Logging Messages ................................................................................................................332Summary ..............................................................................................................................333Chapter 13: Script Control . . . . . . . . . . . . . . . . . . . . . . . . 335Handling Signals ..................................................................................................................335Linux signals revisited ...............................................................................................335Generating signals .....................................................................................................336Trapping signals ........................................................................................................338Trapping a script exit ................................................................................................339Removing a trap ........................................................................................................340Running Scripts in Background Mode ................................................................................341Running in the background ......................................................................................341Running multiple background jobs ..........................................................................342Exiting the terminal ..................................................................................................343Running Scripts without a Console ....................................................................................343Job Control ..........................................................................................................................344Viewing jobs ..............................................................................................................345Restarting stopped jobs .............................................................................................347Being Nice ............................................................................................................................348The nice command ....................................................................................................348xviwww.IrPDF.comwww.IrPDF.com
  20. 20. Blum ftoc.tex V2 - 04/04/2008 5:48pm Page xviiContentsThe renice command ................................................................................................349Running Like Clockwork ....................................................................................................349Scheduling a job using the at command ..................................................................350Using the batch command ........................................................................................352Scheduling regular scripts .........................................................................................353Start At the Beginning .........................................................................................................355Starting your scripts at boot .....................................................................................355Starting with a new shell ..........................................................................................357Summary ..............................................................................................................................358Part III Advanced Shell ScriptingChapter 14: Creating Functions . . . . . . . . . . . . . . . . . . . . . . 363Basic Script Functions .........................................................................................................363Creating a function ....................................................................................................364Using functions .........................................................................................................364Returning a Value ................................................................................................................366The default exit status ...............................................................................................367Using the return command .......................................................................................368Using function output ...............................................................................................369Using Variables in Functions ..............................................................................................369Passing parameters to a function ..............................................................................370Handling variables in a function ..............................................................................372Array Variables and Functions ............................................................................................375Passing arrays to functions ........................................................................................375Returning arrays from functions ...............................................................................376Function Recursion .............................................................................................................377Creating a Library ................................................................................................................379Using Functions on the Command Line ............................................................................381Creating functions on the command line .................................................................381Defining functions in the .bashrc file .......................................................................382Summary ..............................................................................................................................384Chapter 15: Adding Color to Scripts . . . . . . . . . . . . . . . . . . . 385Creating Text Menus ...........................................................................................................385Create the menu layout .............................................................................................386Create the menu functions ........................................................................................387Add the menu logic ..................................................................................................388Putting it all together ................................................................................................389Using the select command ........................................................................................390Adding Color .......................................................................................................................391The ANSI escape codes .............................................................................................392Displaying ANSI escape codes ..................................................................................393Using colors in scripts ...............................................................................................395xviiwww.IrPDF.comwww.IrPDF.com
  21. 21. Blum ftoc.tex V2 - 04/04/2008 5:48pm Page xviiiContentsDoing Windows ...................................................................................................................397The dialog package ....................................................................................................397The dialog options ....................................................................................................404Using the dialog command in a script .....................................................................407Getting Graphic ...................................................................................................................409The KDE environment ..............................................................................................409The GNOME environment ........................................................................................412Summary ..............................................................................................................................417Chapter 16: Introducing sed and gawk . . . . . . . . . . . . . . . . . . 419Text Manipulation ...............................................................................................................419The sed editor ...........................................................................................................420The gawk program ....................................................................................................423The sed Editor Basics ..........................................................................................................430More substitution options .........................................................................................430Using addresses .........................................................................................................432Deleting lines .............................................................................................................434Inserting and appending text ....................................................................................436Changing lines ...........................................................................................................438The transform command ..........................................................................................439Printing revisited .......................................................................................................440Using files with sed ...................................................................................................442Summary ..............................................................................................................................445Chapter 17: Regular Expressions . . . . . . . . . . . . . . . . . . . . . 447What Are Regular Expressions? ..........................................................................................447A definition ................................................................................................................447Types of regular expressions .....................................................................................448Defining BRE Patterns .........................................................................................................449Plain text ....................................................................................................................449Special characters ......................................................................................................451Anchor characters ......................................................................................................452The dot character ......................................................................................................454Character classes .......................................................................................................455Negating character classes .........................................................................................457Using ranges ..............................................................................................................458Special character classes ............................................................................................459The asterisk ...............................................................................................................460Extended Regular Expressions ............................................................................................461The question mark ....................................................................................................461The plus sign .............................................................................................................462Using braces ..............................................................................................................463The pipe symbol ........................................................................................................464Grouping expressions ................................................................................................465Regular Expressions in Action ............................................................................................466xviiiwww.IrPDF.comwww.IrPDF.com
  22. 22. Blum ftoc.tex V2 - 04/04/2008 5:48pm Page xixContentsCounting directory files ............................................................................................466Validating a phone number ......................................................................................467Parsing an e-mail address .........................................................................................469Summary ..............................................................................................................................471Chapter 18: Advanced sed . . . . . . . . . . . . . . . . . . . . . . . . 473Multiline Commands ...........................................................................................................473The next commands ..................................................................................................474The multiline delete command .................................................................................477The multiline print command ..................................................................................479The Hold Space ...................................................................................................................479Negating a Command .........................................................................................................481Changing the Flow ..............................................................................................................484Branching ...................................................................................................................484Testing .......................................................................................................................486Pattern Replacement ............................................................................................................487The ampersand ..........................................................................................................488Replacing individual words .......................................................................................488Using sed in Scripts ............................................................................................................489Using wrappers ..........................................................................................................489Redirecting sed output ..............................................................................................490Creating sed Utilities ...........................................................................................................491Double spacing lines .................................................................................................491Double spacing files that may have blanks ..............................................................492Numbering lines in a file ..........................................................................................493Printing last lines .......................................................................................................494Deleting lines .............................................................................................................495Removing HTML tags ................................................................................................497Summary ..............................................................................................................................499Chapter 19: Advanced gawk . . . . . . . . . . . . . . . . . . . . . . . 501Using Variables ....................................................................................................................501Built-in variables ........................................................................................................502User-defined variables ...............................................................................................508Working with Arrays ...........................................................................................................510Defining array variables ............................................................................................510Iterating through array variables ...............................................................................511Deleting array variables .............................................................................................511Using Patterns ......................................................................................................................512Regular expressions ...................................................................................................512The matching operator ..............................................................................................513Mathematical expressions ..........................................................................................514Structured Commands ........................................................................................................514The if statement .........................................................................................................514The while statement ..................................................................................................516xixwww.IrPDF.comwww.IrPDF.com
  23. 23. Blum ftoc.tex V2 - 04/04/2008 5:48pm Page xxContentsThe do-while statement ............................................................................................518The for statement ......................................................................................................518Formatted Printing ..............................................................................................................519Built-in Functions ................................................................................................................522Mathematical functions .............................................................................................522String functions .........................................................................................................524Time functions ..........................................................................................................526User-Defined Functions ......................................................................................................527Defining a function ...................................................................................................527Using your functions .................................................................................................528Creating a function library ........................................................................................528Summary ..............................................................................................................................529Part IV Alternative Linux ShellsChapter 20: The ash Shell . . . . . . . . . . . . . . . . . . . . . . . . 533What Is the ash Shell? .........................................................................................................533The Original ash Shell .........................................................................................................534The Original ash command line parameters ............................................................534The original ash built-in commands ........................................................................536The ash shell files ......................................................................................................539The dash Shell .....................................................................................................................540The dash command line parameters ........................................................................540The dash environment variables ...............................................................................540The dash built-in commands ....................................................................................544Scripting in dash .................................................................................................................549Creating ash and dash scripts ...................................................................................549Things that won’t work .............................................................................................549Summary ..............................................................................................................................555Chapter 21: The tcsh Shell . . . . . . . . . . . . . . . . . . . . . . . . 557What Is the tcsh Shell? .......................................................................................................557The tcsh Shell Components ................................................................................................558The tcsh command line parameters .........................................................................558The tcsh files .............................................................................................................560The tcsh login files ....................................................................................................560Shell startup files .......................................................................................................561The logout files ..........................................................................................................562The tcsh environment variables ................................................................................563Shell variables ............................................................................................................563Environment variables ...............................................................................................569Setting variables in tcsh ............................................................................................572Using the set command ............................................................................................572Using the setenv command .......................................................................................573xxwww.IrPDF.comwww.IrPDF.com
  24. 24. Blum ftoc.tex V2 - 04/04/2008 5:48pm Page xxiContentsThe tcsh built-in commands .....................................................................................574Scripting in tcsh ..................................................................................................................577Working with variables .............................................................................................578Array variables ...........................................................................................................578Handling mathematical operations ...........................................................................578Structured commands ...............................................................................................578The if statements .......................................................................................................579The foreach statement ...............................................................................................582The while statement ..................................................................................................582The switch command ................................................................................................583Summary ..............................................................................................................................584Chapter 22: The Korn Shell . . . . . . . . . . . . . . . . . . . . . . . . 587The Korn Shell History .......................................................................................................587The Parts of the ksh93 Shell ...............................................................................................588Command line parameters ........................................................................................588Default files ................................................................................................................590Environment variables ...............................................................................................590Built-in commands ....................................................................................................597Scripting in the ksh93 Shell ...............................................................................................602Mathematical operations ...........................................................................................602Structured commands ...............................................................................................605Command redirection ...............................................................................................607Discipline functions ...................................................................................................608Summary ..............................................................................................................................609Chapter 23: The zsh Shell . . . . . . . . . . . . . . . . . . . . . . . . 611History of the zsh Shell .......................................................................................................611Parts of the zsh Shell ...........................................................................................................612Shell options ..............................................................................................................612The zsh shell files ......................................................................................................615Environment variables ...............................................................................................619Built-in commands ....................................................................................................625Scripting with zsh ...............................................................................................................631Mathematical operations ...........................................................................................631Structured commands ...............................................................................................633Functions ...................................................................................................................634Summary ..............................................................................................................................636Part V Advanced TopicsChapter 24: Using a Database . . . . . . . . . . . . . . . . . . . . . . 639The MySQL Database ..........................................................................................................639Installing MySQL .......................................................................................................640Completing the MySQL configuration ......................................................................642xxiwww.IrPDF.comwww.IrPDF.com
  25. 25. Blum ftoc.tex V2 - 04/04/2008 5:48pm Page xxiiContentsThe MySQL client interface ......................................................................................644Creating MySQL database objects ............................................................................649The PostgreSQL Database ...................................................................................................651Installing PostgreSQL ................................................................................................652The PostgreSQL command interface ........................................................................654Creating PostgreSQL database objects ......................................................................657Working with Tables ...........................................................................................................659Creating a table .........................................................................................................659Inserting and deleting data .......................................................................................661Querying data ............................................................................................................663Using the Database in Your Scripts ....................................................................................664Connecting to the databases .....................................................................................664Sending commands to the server .............................................................................666Formatting data .........................................................................................................670Summary ..............................................................................................................................671Chapter 25: Using the Web . . . . . . . . . . . . . . . . . . . . . . . . 673The Lynx Program ...............................................................................................................673Installing Lynx ...........................................................................................................674The lynx command line ............................................................................................675The Lynx configuration file .......................................................................................676The Lynx environment variables ..............................................................................683Capturing data from Lynx ........................................................................................684The cURL Program ..............................................................................................................687Installing cURL ..........................................................................................................687The cURL command line ..........................................................................................688Exploring with curl ...................................................................................................688Networking with zsh ...........................................................................................................694The TCP module .......................................................................................................694The client/server paradigm ........................................................................................695Client/server programming with zsh ........................................................................695Summary ..............................................................................................................................699Chapter 26: Using E-Mail . . . . . . . . . . . . . . . . . . . . . . . . . 701The Basics of Linux E-Mail .................................................................................................701E-Mail in Linux .........................................................................................................701The Mail Transfer Agent ...........................................................................................702The Mail Delivery Agent ...........................................................................................703The Mail User Agent .................................................................................................705Setting Up Your Server ........................................................................................................708sendmail ....................................................................................................................709Postfix ........................................................................................................................711Sending a Message with Mailx ............................................................................................717The Mutt Program ...............................................................................................................720Installing Mutt ...........................................................................................................720xxiiwww.IrPDF.comwww.IrPDF.com
  26. 26. Blum ftoc.tex V2 - 04/04/2008 5:48pm Page xxiiiContentsThe Mutt command line ...........................................................................................721Using Mutt .................................................................................................................721Summary ..............................................................................................................................723Chapter 27: Shell Scripts for Administrators . . . . . . . . . . . . . . . 725Monitoring System Statistics ...............................................................................................725Monitoring disk free space ........................................................................................725Catching disk hogs ....................................................................................................728Watching CPU and memory usage ...........................................................................732Performing Backups ............................................................................................................739Archiving data files ....................................................................................................740Storing backups off-site .............................................................................................744Summary ..............................................................................................................................746Appendix A: Quick Guide to bash Commands . . . . . . . . . . . . . . 749Built-In Commands .............................................................................................................749Bash Commands ..................................................................................................................749Environment Variables ........................................................................................................753Appendix B: Quick Guide to sed and gawk . . . . . . . . . . . . . . . . 759The sed Editor .....................................................................................................................759Starting the sed editor ...............................................................................................759sed commands ...........................................................................................................760The gawk program ..............................................................................................................764The gawk command format ......................................................................................764Using gawk ................................................................................................................765The gawk variables ....................................................................................................766The gawk program features ......................................................................................768Appendix C: Comparing Shells . . . . . . . . . . . . . . . . . . . . . . 771Variables ..............................................................................................................................771Environment variables ...............................................................................................771User-defined variables ...............................................................................................772Array variables ...........................................................................................................772Structured Commands ........................................................................................................773The if-then, while, and until statements ..................................................................773The for statement ......................................................................................................774Mathematical Operations ....................................................................................................775Index ..............................................................................................................................................777xxiiiwww.IrPDF.comwww.IrPDF.com
  27. 27. Blum ftoc.tex V2 - 04/04/2008 5:48pm Page xxivwww.IrPDF.comwww.IrPDF.com
  28. 28. Blum flast.tex V2 - 04/04/2008 4:49pm Page xxvFirst, all glory and praise go to God, who through His Son makes all things possible, andgives us the gift of eternal life.Many thanks go to the great team of people at John Wiley & Sons for their outstanding workon this project. Thanks to Jenny Watson, the acquisitions editor, for offering me the opportunityto work on this book. Also thanks to Tom Dinse, the development editor, for keeping thingson track and making this book more presentable. The technical editor, John Kennedy, did anamazing job of double-checking all the work in this book, plus making suggestions to improvethe content. Thanks, John, for your hard work and diligence. I would also like to thank CaroleMcClendon at Waterside Productions, Inc. for arranging this opportunity for me, and for helpingout in my writing career.Finally, I would like to thank my parents, Mike and Joyce Blum, for their dedication and supportwhile raising me, and my wife, Barbara, and daughters, Katie Jane and Jessica, for their love,patience, and understanding, especially while I was writing this book.xxvwww.IrPDF.comwww.IrPDF.com
  29. 29. Blum flast.tex V2 - 04/04/2008 4:49pm Page xxviwww.IrPDF.comwww.IrPDF.com
  30. 30. Blum fintro.tex V2 - 04/04/2008 4:51pm Page xxviiWelcome to Linux Command Line and Shell Scripting Bible. Like all books in the Bibleseries, you can expect to find both hands-on tutorials and real-world practical appli-cation information, as well as reference and background information that providesa context for what you are learning. This book is a fairly comprehensive resource on the Linuxcommand line and shell commands. By the time you have completed Linux Command Line andShell Scripting Bible, you will be well prepared to write your own shell scripts that can automatepractically any task on your Linux system.Who Should Read This BookIf you’re a system administrator in a Linux environment, you’ll benefit greatly by knowing how towrite shell scripts. The book doesn’t walk through setting up a Linux system, but once you haveit running, you’ll want to start automating some of the routine administrative tasks. That’s whereshell scripting comes in, and that’s where this book will help you out. This book will demonstratehow to automate any administrative task using shell scripts, from monitoring system statistics anddata files to generating reports for your boss.If you’re a home Linux enthusiast, you’ll also benefit from Linux Command Line and Shell ScriptingBible. Nowadays it’s easy to get lost in the graphical world of prebuilt widgets. Most desktopLinux distributions try their best to hide the Linux system from the typical user. However, thereare times when you have to know what’s going on under the hood. This book shows you howto access the Linux command line prompt, and what to do once you get there. Often performingsimple tasks, such as file management, can be done more quickly from the command line thanfrom a fancy graphical interface. There’s a wealth of commands you can use from the commandline, and this book shows you just how to use them.How This Book Is OrganizedThis book is organized in a way that leads you through the basics of the Linux command line allthe way to creating your own shell scripts. The book is divided into five parts, each one buildingon the previous parts.Part I assumes that you either have a Linux system running or are looking into getting a Linuxsystem. Chapter 1, ‘‘Starting with Linux Shells,’’ describes the parts of a total Linux system andxxviiwww.IrPDF.comwww.IrPDF.com
  31. 31. Blum fintro.tex V2 - 04/04/2008 4:51pm Page xxviiiIntroductionshows how the shell fits in. After learning the basics of the Linux system, this sectioncontinues with:■ Using a terminal emulation package to access the shell (Chapter 2)■ Introducing the basic shell commands (Chapter 3)■ Using more advanced shell commands to peek at system information (Chapter 4)■ Working with shell variables to manipulate data (Chapter 5)■ Understanding the Linux filesystem and security (Chapter 6)■ Knowing how to use the Linux editors to start writing shell scripts (Chapter 7)In Part II, you’ll start writing shell scripts:■ Learn how to create and run shell scripts (Chapter 8)■ Alter the program flow in a shell script (Chapter 9)■ Iterate through code sections (Chapter 10)■ Handle data from the user in your scripts (Chapter 11)■ See different methods for storing and displaying data from your script (Chapter 12)■ Control how and when your shell scripts run on the system (Chapter 13)Part III dives into more advanced areas of shell script programming:■ Create your own functions to use in all your scripts (Chapter 14)■ See different methods for interacting with your script users (Chapter 15)■ Use advanced Linux commands to filter and parse data files (Chapter 16)■ Use regular expressions to define data (Chapter 17)■ Learn advanced methods of manipulating data in your scripts (Chapter 18)■ See how to generate reports from raw data (Chapter 19)In Part IV, you’ll get to see how to write shell scripts using some of the alternative shells availablein the Linux environment:■ Write scripts for the ash or dash shells (Chapter 20)■ See how writing scripts in the tcsh shell is different (Chapter 21)■ Work with floating-point numbers in the ksh93 shell (Chapter 22)■ Use advanced network and math features in the zsh shell (Chapter 23)xxviiiwww.IrPDF.comwww.IrPDF.com
  32. 32. Blum fintro.tex V2 - 04/04/2008 4:51pm Page xxixIntroductionThe last section of the book, Part V, demonstrates how to use shell scripts in real-worldenvironments:■ See how to use popular open source databases in your shells scripts (Chapter 24)■ Learn how to extract data from Web sites, and send data between systems (Chapter 25)■ Use e-mail to send notifications and reports to external users (Chapter 26)■ Write shell scripts to automate your daily system administration functions (Chapter 27)Conventions and FeaturesThere are many different organizational and typographical features throughout this book designedto help you get the most out of the information.Throughout the book, special typography indicates code and commands. Commands and codeare shown in a monospaced font. In a regular paragraph, programming code words look likethis. Lines of code are presented like this:$ cat test2#!/bin/bash# testing a bad commandif asdfgthenecho "it didn’t work"fiecho "we’re outside of the if statement"$ ./test2./test2: line 3: asdfg: command not foundwe’re outside of the if statement$Notes and CautionsWhenever the author wants to bring something important to your attention, the information willappear in a Note or Caution.Notes provide additional, ancillary information that is helpful, but somewhat outsideof the current presentation of information.This information is important and is set off in a separate paragraph with a specialicon. Cautions provide information about things to watch out for, whether simplyinconvenient or potentially hazardous to your data or systems.xxixwww.IrPDF.comwww.IrPDF.com
  33. 33. Blum fintro.tex V2 - 04/04/2008 4:51pm Page xxxIntroductionMinimum RequirementsLinux Command Line and Shell Scripting Bible looks at Linux from a generic point of view, so you’llbe able to follow along in the book using any Linux system you have available. The bulk of thebook references the bash shell, which is the default shell for most Linux systems.Where to Go from HereOnce you’ve completed Linux Command Line and Shell Scripting Bible, you’ll be well on your way toincorporating Linux commands in your daily Linux work. In the ever-changing world of Linux,it’s always a good idea to stay in touch with new developments. Often Linux distributions willchange, adding new features and removing older ones. To keep your knowledge of Linux fresh,always stay well informed. Find a good Linux forum site and monitor what’s happening in theLinux world. There are many popular Linux news sites, such as Slashdot and Distrowatch, thatprovide up-to-the-minute information about new advances in Linux.xxxwww.IrPDF.comwww.IrPDF.com
  34. 34. Blum c01.tex V2 - 04/03/2008 2:57pm Page 1The LinuxCommand LineIN THIS PARTChapter 1Starting with Linux ShellsChapter 2Getting to the ShellChapter 3Basic bash Shell CommandsChapter 4More bash Shell CommandsChapter 5Using Linux EnvironmentVariablesChapter 6Understanding Linux FilePermissionsChapter 7Working with Editorswww.IrPDF.comwww.IrPDF.com
  35. 35. Blum c01.tex V2 - 04/03/2008 2:57pm Page 2www.IrPDF.comwww.IrPDF.com
  36. 36. Blum c01.tex V2 - 04/03/2008 2:57pm Page 3Starting with LinuxShellsIN THIS CHAPTERWhat Is Linux?Parts of the Linux kernelExploring the Linux desktopVisiting Linux distributionsBefore you can dive into working with the Linux command line andshells, it’s a good idea to first understand what Linux is, where itcame from, and how it works. This chapter walks you throughwhat Linux is, and explains where the shell and command line fit in theoverall Linux picture.What Is Linux?If you’ve never worked with Linux before, you may be confused as to whythere are so many different versions of it available. I’m sure that you haveheard various terms such as distribution, LiveCD, and GNU when lookingat Linux packages and been confused. Trying to wade through the worldof Linux for the first time can be a tricky experience. This chapter will takesome of the mystery out of the Linux system before we start working oncommands and scripts.For starters, there are four main parts that make up a Linux system:■ The Linux kernel■ The GNU utilities■ A graphical desktop environment■ Application softwareEach of these four parts has a specific job in the Linux system. Each of theparts by itself isn’t very useful. Figure 1-1 shows a basic diagram of howthe parts fit together to create the overall Linux system.3www.IrPDF.comwww.IrPDF.com
  37. 37. Blum c01.tex V2 - 04/03/2008 2:57pm Page 4Part I The Linux Command LineFIGURE 1-1The Linux systemApplication SoftwareWindowsManagementSoftwareGNUSystemUtilitiesLinux kernelcomputer hardwareThis section describes these four main parts in detail, and gives you an overview of how theywork together to create a complete Linux system.Looking into the Linux kernelThe core of the Linux system is the kernel. The kernel controls all of the hardware and software onthe computer system, allocating hardware when necessary, and executing software when required.If you’ve been following the Linux world at all, no doubt you’ve heard the name Linus Torvalds.Linus is the person responsible for creating the first Linux kernel software while he was a studentat the University of Helsinki. He intended it to be a copy of the Unix system, at the time a popularoperating system used at many universities.After developing the Linux kernel, Linus released it to the Internet community and solicitedsuggestions for improving it. This simple process started a revolution in the world of computeroperating systems. Soon Linus was receiving suggestions from students as well as professionalprogrammers from around the world.Allowing anyone to change programming code in the kernel would result in complete chaos. Tosimplify things, Linus acted as a central point for all improvement suggestions. It was ultimatelyLinus’s decision whether or not to incorporate suggested code in the kernel. This same concept isstill in place with the Linux kernel code, except that instead of just Linus controlling the kernelcode, a team of developers has taken on the task.4www.IrPDF.comwww.IrPDF.com
  38. 38. Blum c01.tex V2 - 04/03/2008 2:57pm Page 5Starting with Linux Shells 1The kernel is primarily responsible for four main functions:■ System memory management■ Software program management■ Hardware management■ Filesystem managementThe following sections explore each of these functions in more detail.System memory managementOne of the primary functions of the operating system kernel is memory management. Not onlydoes the kernel manage the physical memory available on the server, it can also create andmanage virtual memory, or memory that does not actually exist.It does this by using space on the hard disk, called the swap space. The kernel swaps the contentsof virtual memory locations back and forth from the swap space to the actual physical mem-ory. This allows the system to think there is more memory available than what physically exists(shown in Figure 1-2).FIGURE 1-2The Linux system memory mapVirtual MemoryPhysical MemorySwap SpaceThe Kernel5www.IrPDF.comwww.IrPDF.com
  39. 39. Blum c01.tex V2 - 04/03/2008 2:57pm Page 6Part I The Linux Command LineThe memory locations are grouped into blocks called pages. The kernel locates each page of mem-ory either in the physical memory or the swap space. The kernel then maintains a table of thememory pages that indicates which pages are in physical memory, and which pages are swappedout to disk.The kernel keeps track of which memory pages are in use and automatically copies memory pagesthat have not been accessed for a period of time to the swap space area (called swapping out), evenif there’s other memory available. When a program wants to access a memory page that has beenswapped out, the kernel must make room for it in physical memory by swapping out a differentmemory page, and swap in the required page from the swap space. Obviously, this process takestime, and can slow down a running process. The process of swapping out memory pages forrunning applications continues for as long as the Linux system is running.You can see the current status of the virtual memory on your Linux system by viewing the special/proc/meminfo file. Here’s an example of a sample /proc/meminfo entry:# cat /proc/meminfoMemTotal: 255392 kBMemFree: 4336 kBBuffers: 1236 kBCached: 48212 kBSwapCached: 1028 kBActive: 182932 kBInactive: 44388 kBHighTotal: 0 kBHighFree: 0 kBLowTotal: 255392 kBLowFree: 4336 kBSwapTotal: 524280 kBSwapFree: 514528 kBDirty: 456 kBWriteback: 0 kBAnonPages: 176940 kBMapped: 40168 kBSlab: 16080 kBSReclaimable: 4048 kBSUnreclaim: 12032 kBPageTables: 4048 kBNFS Unstable: 0 kBBounce: 0 kBCommitLimit: 651976 kBCommitted AS: 442296 kBVmallocTotal: 770040 kBVmallocUsed: 3112 kBVmallocChunk: 766764 kBHugePages Total: 06www.IrPDF.comwww.IrPDF.com
  40. 40. Blum c01.tex V2 - 04/03/2008 2:57pm Page 7Starting with Linux Shells 1HugePages Free: 0HugePages Rsvd: 0Hugepagesize: 4096 kB#The Mem: line shows that this Linux server has 256 MB of physical memory. It also shows thatabout 4 MB is not currently being used (free). The output also shows that there is about 512 MBof swap space memory available on this system.By default, each process running on the Linux system has its own private memory pages. Oneprocess cannot access memory pages being used by another process. The kernel maintains itsown memory areas. For security purposes, no processes can access memory used by the kernelprocesses.To facilitate data sharing, you can create shared memory pages. Multiple processes can read andwrite to and from a common shared memory area. The kernel maintains and administers theshared memory areas and allows individual processes access to the shared area.The special ipcs command allows you to view the current shared memory pages on the system.Here’s the output from a sample ipcs command:# ipcs -m------ Shared Memory Segments --------key shmid owner perms bytes nattch status0x00000000 0 rich 600 52228 6 dest0x395ec51c 1 oracle 640 5787648 6#Each shared memory segment has an owner that created the segment. Each segment also has astandard Linux permissions setting that sets the availability of the segment for other users. Thekey value is used to allow other users to gain access to the shared memory segment.Software program managementThe Linux operating system calls a running program a process. A process can run in the fore-ground, displaying output on a display, or it can run in background, behind the scenes. Thekernel controls how the Linux system manages all the processes running on the system.The kernel creates the first process, called the init process, to start all other processes on thesystem. When the kernel starts, it loads the init process into virtual memory. As the kernel startseach additional process, it gives it a unique area in virtual memory to store the data and code thatthe process uses.7www.IrPDF.comwww.IrPDF.com
  41. 41. Blum c01.tex V2 - 04/03/2008 2:57pm Page 8Part I The Linux Command LineSome Linux implementations contain a table of processes to start automatically on bootup. OnLinux systems this table is usually located in the special file /etc/inittabs.The Linux operating system uses an init system that utilizes run levels. A run level can be used todirect the init process to run only certain types of processes, as defined in the /etc/inittabsfile. There are five init run levels in the Linux operating system.At run level 1, only the basic system processes are started, along with one console terminal pro-cess. This is called single user mode. Single user mode is most often used for emergency filesystemmaintenance when something is broken. Obviously, in this mode only one person (usually theadministrator) can log in to the system to manipulate data.The standard init run level is 3. At this run level most application software such as networksupport software is started. Another popular run level in Linux is run level 5. This is the runlevel where the system starts the graphical X Window software, and allows you to log in using agraphical desktop window.The Linux system can control the overall system functionality by controlling the init run level.By changing the run level from 3 to 5, the system can change from a console-based system to anadvanced, graphical X Window system.Later on (in Chapter 4) you’ll see how to use the ps command to view the processes currentlyrunning on the Linux system. Here’s an example of what you’ll see using the ps command:$ ps axPID TTY STAT TIME COMMAND1 ? S 0:03 init2 ? SW 0:00 [kflushd]3 ? SW 0:00 [kupdate]4 ? SW 0:00 [kpiod]5 ? SW 0:00 [kswapd]243 ? SW 0:00 [portmap]295 ? S 0:00 syslogd305 ? S 0:00 klogd320 ? S 0:00 /usr/sbin/atd335 ? S 0:00 crond350 ? S 0:00 inetd365 ? SW 0:00 [lpd]403 ttyS0 S 0:00 gpm -t ms418 ? S 0:00 httpd423 ? S 0:00 httpd424 ? SW 0:00 [httpd]425 ? SW 0:00 [httpd]426 ? SW 0:00 [httpd]427 ? SW 0:00 [httpd]428 ? SW 0:00 [httpd]429 ? SW 0:00 [httpd]430 ? SW 0:00 [httpd]8www.IrPDF.comwww.IrPDF.com
  42. 42. Blum c01.tex V2 - 04/03/2008 2:57pm Page 9Starting with Linux Shells 1436 ? SW 0:00 [httpd]437 ? SW 0:00 [httpd]438 ? SW 0:00 [httpd]470 ? S 0:02 xfs -port -1485 ? SW 0:00 [smbd]495 ? S 0:00 nmbd -D533 ? SW 0:00 [postmaster]538 tty1 SW 0:00 [mingetty]539 tty2 SW 0:00 [mingetty]540 tty3 SW 0:00 [mingetty]541 tty4 SW 0:00 [mingetty]542 tty5 SW 0:00 [mingetty]543 tty6 SW 0:00 [mingetty]544 ? SW 0:00 [prefdm]549 ? SW 0:00 [prefdm]559 ? S 0:02 [kwm]585 ? S 0:06 kikbd594 ? S 0:00 kwmsound595 ? S 0:03 kpanel596 ? S 0:02 kfm597 ? S 0:00 krootwm598 ? S 0:01 kbgndwm611 ? S 0:00 kcmlaptop -daemon666 ? S 0:00 /usr/libexec/postfix/master668 ? S 0:00 qmgr -l -t fifo -u787 ? S 0:00 pickup -l -t fifo790 ? S 0:00 telnetd: 192.168.1.2 [vt100]791 pts/0 S 0:00 login -- rich792 pts/0 S 0:00 -bash805 pts/0 R 0:00 ps ax$The first column in the output shows the process ID (or PID) of the process. Notice that the firstprocess is our friend the init process, and assigned PID 1 by the Linux system. All other processesthat start after the init process are assigned PIDs in numerical order. No two processes can havethe same PID.The third column shows the current status of the process (S for sleeping, SW for sleeping andwaiting, and R for running). The process name is shown in the last column. Processes that arein brackets are processes that have been swapped out of memory to the disk swap space dueto inactivity. You can see that some of the processes have been swapped out, but most of therunning processes have not.Hardware managementStill another responsibility for the kernel is hardware management. Any device that the Linuxsystem must communicate with needs driver code inserted inside the kernel code. The drivercode allows the kernel to pass data back and forth to the device, acting as a middle man between9www.IrPDF.comwww.IrPDF.com
  43. 43. Blum c01.tex V2 - 04/03/2008 2:57pm Page 10Part I The Linux Command Lineapplications and the hardware. There are two methods used for inserting device driver code inthe Linux kernel:■ Drivers compiled in the kernel■ Driver modules added to the kernelPreviously, the only way to insert device driver code was to recompile the kernel. Each time youadded a new device to the system, you had to recompile the kernel code. This process becameeven more inefficient as Linux kernels supported more hardware. Fortunately, Linux developersdevised a better method to insert driver code into the running kernel.Programmers developed the concept of kernel modules to allow you to insert driver code into arunning kernel without having to recompile the kernel. Also, a kernel module could be removedfrom the kernel when the device was finished being used. This greatly simplified and expandedusing hardware with Linux.The Linux system identifies hardware devices as special files, called device files. There are threedifferent classifications of device files:■ Character■ Block■ NetworkCharacter device files are for devices that can only handle data one character at a time. Most typesof modems and terminals are created as character files. Block files are for devices that can handledata in large blocks at a time, such as disk drives.The network file types are used for devices that use packets to send and receive data. Thisincludes network cards and a special loopback device that allows the Linux system to communi-cate with itself using common network programming protocols.Linux creates special files, called nodes, for each device on the system. All communication withthe device is performed through the device node. Each node has a unique number pair that identi-fies it to the Linux kernel. The number pair includes a major and a minor device number. Similardevices are grouped into the same major device number. The minor device number is used toidentify a specific device within the major device group. This is an example of a few device fileson a Linux server:$ ls -al sda* ttyS*brw-rw---- 1 root disk 8, 0 May 5 2006 sdabrw-rw---- 1 root disk 8, 1 May 5 2006 sda1brw-rw---- 1 root disk 8, 10 May 5 2006 sda10brw-rw---- 1 root disk 8, 11 May 5 2006 sda11brw-rw---- 1 root disk 8, 12 May 5 2006 sda12brw-rw---- 1 root disk 8, 13 May 5 2006 sda1310www.IrPDF.comwww.IrPDF.com

×