Linux Command Line and Shell Scripting Bible

Similar documents
Linux Command Line and Shell Scripting Bible. Third Edtion

Linux Command Line and Shell Scripting Bible

Mastering UNIX Shell Scripting

Beginning Web Programming with HTML, XHTML, and CSS. Second Edition. Jon Duckett

7 Windows Tweaks. A Comprehensive Guide to Customizing, Increasing Performance, and Securing Microsoft Windows 7. Steve Sinchak

Linux. The book you need to succeed! Boot up to Ubuntu, Fedora, KNOPPIX, Debian, opensuse, and 13 Other Distributions Edition.

Professional ASP.NET 2.0 Databases. Thiru Thangarathinam

Beginning Transact-SQL with SQL Server 2000 and Paul Turley with Dan Wood

Join the p2p.wrox.com. Wrox Programmer to Programmer. Beginning PHP 5.3. Matt Doyle

Implementing Security and Tokens: Current Standards, Tools, and Practices

Study Guide. Robert Schmidt Dane Charlton

Securing SCADA Systems. Ronald L. Krutz

XSLT 2.0 and XPath 2.0

MCITP Windows Server 2008 Server Administrator Study Guide

Agile Database Techniques Effective Strategies for the Agile Software Developer. Scott W. Ambler

Ruby on Rails. Bible

PHP & MySQL. Learn to: Janet Valade. Making Everything Easier! 4th Edition. Create well-formed PHP code that s compliant with PHP 4, 5, and 6

iwork DUMmIES 2ND EDITION FOR

Creating Web Pages. Learn to: Bud E. Smith. Making Everything Easier! 9th Edition. Design, build, and post a Web page

OpenSolaris. The book you need to succeed! Nicholas A. Solter, Gerald Jelinek, and David Miner. Explore the OpenSolaris operating environment

Cloud Phone Systems. Andrew Moore. Making Everything Easier! Nextiva Special Edition. Learn:

Real-Time Optimization by Extremum-Seeking Control

Windows. 7 Desktop Support and Administration. Real World Skills for MCITP Certification and Beyond. Darril Gibson

Oracle PL/SQL. DUMmIES. by Michael Rosenblum and Dr. Paul Dorsey FOR

FileMaker. Pro 10. The book you need to succeed! Companion Web Site. Ray Cologon. Go from basics to full-scale development

MacBook Air PORTABLE GENIUS

RICHARD BLUM DO WHAT YOU NEVER THOUGHT POSSIBLE WITH UBUNTU LINUX. Delve into the underlying applications and features of the Ubuntu Linux system

Hands-On Oracle Application Express Security

Professional Windows PowerShell Programming

Flash Lite. Mobile Development. Professional. Build Flash Applications for Mobile Devices. Jermaine G. Anderson. Join the p2p.wrox.

Fedora 8 and Red Hat Enterprise Linux Bible

Google Voice. Learn to: Bud E. Smith Chris Dannen. Making Everything Easier! Set up and use Google Voice

LEGITIMATE APPLICATIONS OF PEER-TO-PEER NETWORKS DINESH C. VERMA IBM T. J. Watson Research Center A JOHN WILEY & SONS, INC., PUBLICATION

MCTS. Microsoft Windows 7 Configuration STUDY GUIDE. William Panek. Covers All Objectives for Exam Exam

Join the p2p.wrox.com. Wrox Programmer to Programmer. Professional ASP.NET 4. in C# and VB. Bill Evjen, Scott Hanselman, Devin Rader

Microsoft Office Simply the Easiest Way to Learn. Kate Shoup

Blade Servers and Virtualization. Barb Goldworm Anne Skamarock. Transforming Enterprise Computing While Cutting Costs. Wiley Publishing, Inc.

LEGITIMATE APPLICATIONS OF PEER-TO-PEER NETWORKS

Hacking Gmail. Ben Hammersley

COSO Enterprise Risk Management

WINDOWS 10. by Paul McFedries

Office 2010 For Seniors

Mastering BEA WebLogic Server Best Practices for Building and Deploying J2EE Applications

Practical Database Programming with Visual Basic.NET

Richard Wentk. Cocoa. Developer Reference.

Microprocessor Theory

Windows 10 Anniversary Update. Paul McFedries

Network Performance Open Source Toolkit Using Netperf, tcptrace, NIST Net, and SSFNet

DIFFERENTIAL EQUATION ANALYSIS IN BIOMEDICAL SCIENCE AND ENGINEERING

SQL Server Administration. Beginning Microsoft. Chris Leiter, Dan Wood, Michael Cierkowski, Albert Boettger

Professional Java Native Interfaces with SWT/JFace. Jackwind Li Guojie

J2EE TM Best Practices Java TM Design Patterns, Automation, and Performance

Microsoft SQL Server 2008 Integration Services

Beginning Visual Basic 2015

MacBook Pro. PORTABLE GENIUS 5th EDITION

HASHING IN COMPUTER SCIENCE FIFTY YEARS OF SLICING AND DICING

TASK SCHEDULING FOR PARALLEL SYSTEMS

PowerShell, VBScript, JScript

CISSP. DUMmIES 4TH EDITION FOR

IP MULTICAST WITH APPLICATIONS TO IPTV AND MOBILE DVB-H

CCNA. Security STUDY GUIDE. Tim Boyles. Covers All Exam Objectives for IINS

Microsoft. Access by Paul McFedries

Acrobat PDF. Adobe. The book you need to succeed! Standard, Pro, and Pro Extended Version. CD-ROM Included! Ted Padova

ASP.NET 3.5 SP1. In C# and VB. Professional. Covers. Service Pack 1. Bill Evjen, Scott Hanselman, Devin Rader

Beginning Visual Basic 2005 Databases. Thearon Willis

Modern Experimental Design


Making Everything Easier! Windows 7. Pick the task, Find it fast, Get it DONE! Nancy Muir

Relational Database Index Design and the Optimizers

Wrox Programmer to Programmer TM. Professional. Design Patterns. Aaron Saray PHP. Updates, source code, and Wrox technical support at

IC Internet and Computing Core Certification Living Online. Study Guide

Microsoft. Excel. Paul McFedries

Algorithms and Parallel Computing

COMPONENT-ORIENTED PROGRAMMING

Dreamweaver CS4. A Complete Training Package! Adobe. Full-color, step-by-step instructional book Video training from Adobe Certified Experts

Essential Angular for ASP.NET Core MVC

Exploiting Distributed Resources in Wireless, Mobile and Social Networks Frank H. P. Fitzek and Marcos D. Katz

PROFESSIONAL SCALA. INTRODUCTION... xv

Fedora Linux TOOLBOX



Implementing 802.1X Security Solutions for Wired and Wireless Networks. Jim Geier

ASP.NET MVC 1.0. Website Programming. Nick Berardi, Al Katawazi, Marco Bellinaso. Wrox Programmer to Programmer TM

ASUS Eee PC. DUMmIES. by Joel McNamara FOR

Networking. 11th Edition. by Doug Lowe

Linux Fundamentals (GL120) U8583S

PostgreSQL 8 for Windows

IC Internet and Computing Core Certification Computing Fundamentals. Study Guide

MODERN MULTITHREADING

CliffsStudySolver Geometry. By David Alan Herzog

Microsoft Virtualization SECRETS

OS X El CapitanTM. Paul McFedries

THE ARCHITECTURE OF COMPUTER HARDWARE, SYSTEM SOFTWARE, AND NETWORKING

Making Everything Easier! Excel Microsoft. Pick the task, Find it fast, Get it DONE! Diane Koers

MySQL Enterprise Solutions

Professional Visual Studio 2013

Outlook Microsoft. Learn to: Bill Dyszel. Making Everything Easier! Navigate the interface and use the To-Do bar

BEGINNING CSS PART I THE BASICS PART II PROPERTIES PART III ADVANCED CSS AND ALTERNATIVE MEDIA

Visio 2007 Bible. Bonnie Biafore

Excel 2013 Power Programming with VBA

Transcription:

Linux Command Line and Shell Scripting Bible Richard Blum Wiley Publishing, Inc.

Linux Command Line and Shell Scripting Bible

Linux Command Line and Shell Scripting Bible Richard Blum Wiley Publishing, Inc.

Linux Command Line and Shell Scripting Bible Published by Wiley Publishing, Inc. 10475 Crosspoint Boulevard Indianapolis, IN 46256 www.wiley.com Copyright 2008 by Wiley Publishing, Inc., Indianapolis, Indiana Published simultaneously in Canada ISBN: 978-0-470-25128-7 Manufactured in the United States of America 10987654321 No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under Sections 107 or 108 of the 1976 United States Copyright Act, without either the prior written permission of the Publisher, or authorization through payment of the appropriate per-copy fee to the Copyright Clearance Center, 222 Rosewood Drive, Danvers, MA 01923, (978) 750-8400, fax (978) 646-8600. Requests to the Publisher for permission should be addressed to the Legal Department, Wiley Publishing, Inc., 10475 Crosspoint Blvd., Indianapolis, IN 46256, (317) 572-3447, fax (317) 572-4355, or online at http://www.wiley.com/go/permissions. Limit of Liability/Disclaimer of Warranty: The publisher and the author make no representations or warranties with respect to the accuracy or completeness of the contents of this work and specifically disclaim all warranties, including without limitation warranties of fitness for a particular purpose. No warranty may be created or extended by sales or promotional materials. The advice and strategies contained herein may not be suitable for every situation. This work is sold with the understanding that the publisher is not engaged in rendering legal, accounting, or other professional services. If professional assistance is required, the services of a competent professional person should be sought. Neither the publisher nor the author shall be liable for damages arising herefrom. The fact that an organization or Website is referred to in this work as a citation and/or a potential source of further information does not mean that the author or the publisher endorses the information the organization or Website may provide or recommendations it may make. Further, readers should be aware that Internet Websites listed in this work may have changed or disappeared between when this work was written and when it is read. For general information on our other products and services or to obtain technical support, please contact our Customer Care Department within the U.S. at (800) 762-2974, outside the U.S. at (317) 572-3993 or fax (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 of John Wiley & Sons, Inc. and/or its affiliates, in the United States and other countries, and may not be used without written permission. Linux is a registered trademark of Linus Torvald. All other trademarks are the property of their respective owners. Wiley Publishing, Inc., is not associated with any product or vendor mentioned in this book. Wiley also publishes its books in a variety of electronic formats. Some content that appears in print may notbeavailableinelectronicbooks.

To all the people who ve helped form my education. Parents, relatives, teachers, coworkers, and even anonymous posters on the Internet. Always be prepared to accept education from wherever you find it. Always continue to learn new things. For the LORD gives wisdom, and from his mouth come knowledge and understanding. Proverbs 2:6 (NIV)

About the Author Richard Blum has worked in the IT industry for over 19 years as both a systems and network administrator. 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 has written shell scripts in just about every Unix shell environment. Rich has a bachelor of science degree in Electrical Engineering, and a master of science degree in Management, specializing in management information systems, from Purdue University. He is the author of several Linux books, including sendmail for Linux, Running qmail, Postfix, Open Source E-mail Security, Network Performance Open Source Toolkit, andprofessional Assembly Language Programming. He s also a coauthor of Professional Linux Programming and Linux For Dummies, 8th Edition. When he s not being a computer nerd, Rich plays bass guitar for his church worship band and enjoys spending time with his wife, Barbara, and their two daughters, Katie Jane and Jessica.

Credits Acquisitions Editor Jenny Watson Senior Development Editor Tom Dinse Technical Editor John Kennedy Production Editor Angela Smith Copy Editor Foxxe Editorial Services Editorial Manager Mary Beth Wakefield Production Manager Tim Tate Vice President and Executive Group Publisher Richard Swadley Vice President and Executive Publisher Joseph B. Wikert Project Coordinator, Cover Lynsey Stanford Proofreader Word One New York Indexer Melanie Belkin

Acknowledgments............................ xxv Introduction............................... xxvii Part I The Linux Command Line Chapter 1: Starting with Linux Shells...3 Chapter 2: Getting to the Shell...25 Chapter 3: Basic bash Shell Commands...59 Chapter 4: More bash Shell Commands...91 Chapter 5: Using Linux Environment Variables...123 Chapter 6: Understanding Linux File Permissions...147 Chapter 7: Working with Editors...171 Part II Shell Scripting Basics Chapter 8: Basic Script Building...201 Chapter 9: Using Structured Commands...229 Chapter 10: More Structured Commands...255 Chapter 11: Handling User Input...285 Chapter 12: Presenting Data...313 Chapter 13: Script Control...335 Part III Advanced Shell Scripting Chapter 14: Creating Functions...363 Chapter 15: Adding Color to Scripts...385 Chapter 16: Introducing sed and gawk...419 Chapter 17: Regular Expressions...447 Chapter 18: Advanced sed...473 Chapter 19: Advanced gawk...501 Part IV Alternative Linux Shells Chapter 20: The ash Shell...533 Chapter 21: The tcsh Shell...557 Chapter 22: The Korn Shell...587 Chapter 23: The zsh Shell...611 ix

Contents at a Glance Part V Advanced Topics Chapter 24: Using a Database...639 Chapter 25: Using the Web...673 Chapter 26: Using E-Mail...701 Chapter 27: Shell Scripts for Administrators...725 Appendix A: Quick Guide to bash Commands...749 Appendix B: Quick Guide to sed and gawk... 759 Appendix C: Comparing Shells...771 Index...777 x

Acknowledgments............................ xxv Introduction............................... xxvii Part I The Linux Command Line Chapter 1: Starting with Linux Shells.................... 3 What Is Linux?...3 Looking into the Linux kernel...4 The GNU utilities...12 The Linux desktop environment...14 Linux Distributions...20 Core Linux distributions...21 Specialized Linux distributions...22 The Linux LiveCD...23 Summary...24 Chapter 2: Getting to the Shell....................... 25 Terminal Emulation...25 Graphics capabilities...27 The keyboard...30 The terminfo Database...31 The Linux Console...35 The xterm Terminal...36 Command line parameters...37 The xterm main menu...38 The VT options menu...41 The VT fonts menu...43 The Konsole Terminal...45 Command line parameters...45 Sessions...45 The menu bar...48 The GNOME Terminal...52 The command line parameters...52 Tabs...53 The menu bar...54 Summary...58 xi

Contents Chapter 3: Basic bash Shell Commands.................. 59 Starting the Shell...59 The Shell Prompt...60 The bash Manual...63 Filesystem Navigation...64 The Linux filesystem...64 Traversing directories...66 File and Directory Listing...69 Basic listing...69 Modifying the information presented...71 The complete parameter list...72 Filtering listing output...74 File Handling...75 Creating files...75 Copying files...76 Linking files...79 Renaming files...80 Deleting files...81 Directory Handling...82 Creating directories...82 Deleting directories... 82 Viewing File Contents...83 Viewing file statistics...84 Viewing the file type...84 Viewing the whole file...85 Viewing parts of a file...89 Summary...90 Chapter 4: More bash Shell Commands.................. 91 Monitoring Programs...91 Peeking at the processes...91 Real-time process monitoring...98 Stopping processes...101 Monitoring Disk Space...104 Mounting media...104 Using the df command...108 Using the du command...109 Working with Data Files...110 Sorting data...110 Searching for data...114 Compressing data...116 Archiving data...120 Summary...121 xii

Contents Chapter 5: Using Linux Environment Variables.............. 123 What Are Environment Variables?...123 Global environment variables...124 Local environment variables...125 Setting Environment Variables...127 Setting local environment variables...127 Setting global environment variables...129 Removing Environment Variables...129 Default Shell Environment Variables...130 Setting the PATH Environment Variable...134 Locating System Environment Variables... 135 Login shell...136 Interactive shell...139 Non-interactive shell...141 Variable Arrays...142 Using Command Aliases...143 Summary...144 Chapter 6: Understanding Linux File Permissions............. 147 Linux Security...147 The /etc/passwd file...148 The /etc/shadow file...150 Adding a new user...150 Removing a user...153 Modifying a user...154 Using Linux Groups...157 The /etc/group file...157 Creating new groups...158 Modifying groups...159 Decoding File Permissions...160 Using file permission symbols...160 Default file permissions...161 Changing Security Settings... 163 Changing permissions...163 Changing ownership...165 Sharing Files...166 Summary...168 Chapter 7: Working with Editors..................... 171 The vim Editor...171 The basics of vim...172 Editing data...174 Copy and paste...174 Search and substitute...175 xiii