Tải bản đầy đủ (.pdf) (723 trang)

Linux Command Line and Shell Scripting Bible 2nd edition potx

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (8.63 MB, 723 trang )

www.it-ebooks.info

www.it-ebooks.info

www.it-ebooks.info
Table of Contents
Title Page
Copyright
Dedication
Credits
About the Authors
About the Technical Editor
Acknowledgments
Introduction
Who Should Read This Book
How This Book Is Organized
Minimum Requirements
Where to Go from Here
Part I: The Linux Command Line
Chapter 1: Starting with Linux Shells
What Is Linux?
Linux Distributions
Summary
Chapter 2: Getting to the Shell
Terminal Emulation
The terminfo Database
The Linux Console
The xterm Terminal
The Konsole Terminal
The GNOME Terminal
Summary


www.it-ebooks.info
Chapter 3: Basic bash Shell Commands
Starting the Shell
The Shell Prompt
The bash Manual
Filesystem Navigation
File and Directory Listing
File Handling
Directory Handling
Viewing File Contents
Summary
Chapter 4: More bash Shell Commands
Monitoring Programs
Monitoring Disk Space
Working with Data Files
Summary
Chapter 5: Using Linux Environment Variables
What Are Environment Variables?
Setting Environment Variables
Removing Environment Variables
Default Shell Environment Variables
Setting the PATH Environment Variable
Locating System Environment Variables
Variable Arrays
Using Command Aliases
Summary
Chapter 6: Understanding Linux File Permissions
Linux Security
Using Linux Groups
Decoding File Permissions

Changing Security Settings
Sharing Files
Summary
Chapter 7: Managing Filesystems
Exploring Linux Filesystems
Working with Filesystems
Logical Volume Managers
Summary
www.it-ebooks.info
Chapter 8: Installing Software
Package Management Primer
The Debian-Based Systems
The Red Hat–Based Systems
Installing from Source Code
Summary
Chapter 9: Working with Editors
The vim Editor
The emacs Editor
The KDE Family of Editors
The GNOME Editor
Summary
Part II: Shell Scripting Basics
Chapter 10: Basic Script Building
Using Multiple Commands
Creating a Script File
Displaying Messages
Using Variables
Redirecting Input and Output
Pipes
Performing Math

Exiting the Script
Summary
Chapter 11: Using Structured Commands
Working with the if-then Statement
The if-then-else Statement
Nesting ifs
The test Command
Compound Condition Testing
Advanced if-then Features
The case Command
Summary
Chapter 12: More Structured Commands
The for Command
The C-Style for Command
www.it-ebooks.info
The while Command
The until Command
Nesting Loops
Looping on File Data
Controlling the Loop
Processing the Output of a Loop
Summary
Chapter 13: Handling User Input
Command Line Parameters
Special Parameter Variables
Being Shifty
Working with Options
Standardizing Options
Getting User Input
Summary

Chapter 14: Presenting Data
Understanding Input and Output
Redirecting Output in Scripts
Redirecting Input in Scripts
Creating Your Own Redirection
Listing Open File Descriptors
Suppressing Command Output
Using Temporary Files
Logging Messages
Summary
Chapter 15: Script Control
Handling Signals
Running Scripts in Background Mode
Running Scripts Without a Console
Job Control
Being Nice
Running Like Clockwork
Summary
Part III: Advanced Shell Scripting
Chapter 16: Creating Functions
Basic Script Functions
www.it-ebooks.info
Returning a Value
Using Variables in Functions
Array Variables and Functions
Function Recursion
Creating a Library
Using Functions on the Command Line
Summary
Chapter 17: Writing Scripts for Graphical Desktops

Creating Text Menus
Doing Windows
Getting Graphic
Summary
Chapter 18: Introducing sed and gawk
Text Manipulation
The sed Editor Basics
Summary
Chapter 19: Regular Expressions
What Are Regular Expressions?
Defining BRE Patterns
Extended Regular Expressions
Regular Expressions in Action
Summary
Chapter 20: Advanced sed
Multiline Commands
The Hold Space
Negating a Command
Changing the Flow
Pattern Replacement
Using sed in Scripts
Creating sed Utilities
Summary
Chapter 21: Advanced gawk
Using Variables
Working with Arrays
Using Patterns
Structured Commands
www.it-ebooks.info
Formatted Printing

Built-in Functions
User-Defined Functions
Summary
Chapter 22: Working with Alternative Shells
What Is the dash Shell?
The dash Shell Features
Scripting in dash
The zsh Shell
Parts of the zsh Shell
Scripting with zsh
Summary
Part IV: Advanced Shell Scripting Topics
Chapter 23: Using a Database
The MySQL Database
The PostgreSQL Database
Working with Tables
Using the Database in Your Scripts
Summary
Chapter 24: Using the Web
The Lynx Program
The cURL Program
Networking with zsh
Summary
Chapter 25: Using E-mail
The Basics of Linux E-Mail
Setting Up Your Server
Sending a Message with Mailx
The Mutt Program
Summary
Chapter 26: Writing Script Utilities

Monitoring Disk Space
Performing Backups
Managing User Accounts
www.it-ebooks.info
Summary
Chapter 27: Advanced Shell Scripts
Monitoring System Statistics
Problem-Tracking Database
Summary
Appendix A: Quick Guide to bash Commands
Built-in Commands
bash Commands
Environment Variables
Appendix B: Quick Guide to sed and gawk
The sed Editor
The gawk Program
Index
www.it-ebooks.info
www.it-ebooks.info

www.it-ebooks.info

www.it-ebooks.info
Linux
®
Command Line and Shell Scripting Bible, Second Edition
Published by
Wiley Publishing, Inc.
10475 Crosspoint Boulevard
Indianapolis, IN 46256

www.wiley.com
Copyright © 2011 by Wiley Publishing, Inc., Indianapolis, Indiana
Published simultaneously in Canada
ISBN: 978-1-118-00442-5
ISBN: 978-1-118-08777-0 (ebk)
ISBN: 978-1-118-08778-7 (ebk)
ISBN: 978-1-118-08779-4 (ebk)
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 Permissions Department,
John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030, (201) 748-6011, fax (201) 748-6008, or online at

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 Web site 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 please contact our Customer Care Department within the United
States at (877) 762-2974, outside the United States at (317) 572-3993 or fax (317) 572-4002.
Wiley also publishes its books in a variety of electronic formats. Some content that appears in print may not be available in
electronic books.
Library of Congress Control Number: 2011921770

Trademarks: Wiley and the Wiley logo 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 Torvalds. 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.
www.it-ebooks.info
To the Lord God Almighty, “in whom are hidden all the treasures of wisdom and knowledge.”
—Colossians 2:3
www.it-ebooks.info
Credits
Acquisitions Editor
Mary James
Project Editor
Brian Herrmann
Technical Editor
Jack Cox
Production Editor
Daniel Scribner
Copy Editor
Nancy Rapoport
Editorial Director
Robyn B. Siesky
Editorial Manager
Mary Beth Wakefield
Freelancer Editorial Manager
Rosemarie Graham
Marketing Manager
Ashley Zurcher
Production Manager
Tim Tate
Vice President and Executive Group Publisher

Richard Swadley
Vice President and Executive Publisher
Barry Pruett
Associate Publisher
Jim Minatel
Project Coordinator, Cover
www.it-ebooks.info
Katherine Crocker
Proofreader
Publication Services, Inc.
Indexer
Robert Swanson
Cover Designer
Ryan Sneed
Cover Image
Joyce Haughey
www.it-ebooks.info
About the Authors
Richard Blum has worked in the IT industry for more than 20 years as both a systems and network administrator, and has
published numerous Linux and open source books. He has administered UNIX, Linux, Novell, and Microsoft servers, as well as
helped design and maintain a 3,500 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 most of the common Linux shell
environments. Rich is an online instructor for an Introduction to Linux course that is used by colleges and universities across
the U.S. When he's not being a computer nerd, Rich plays electric bass in a couple of different church worship bands, and
enjoys spending time with his wife Barbara, and two daughters Katie Jane and Jessica.
Christine Bresnahan starting working with computers more than 25 years ago in the IT industry as a system
administrator. Christine is currently an Adjunct Professor at Ivy Tech Community College in Indianapolis, Indiana, teaching
Linux system administration, Linux security, and Windows security classes.
www.it-ebooks.info
About the Technical Editor

Jack Cox is a Senior Manager with CapTech Ventures in Richmond, VA. He has more than 25 years of experience in IT, spanning
a broad range of disciplines including mobile computing, transaction processing, RFID, Java development, and
cryptography. Jack enjoys life in Richmond with his lovely wife and rambunctious children. Outside of technology, his
interests include church, his children, and extended family.
www.it-ebooks.info
Acknowledgments
First, all glory and praise go to God, who through His Son, Jesus Christ, makes all things possible, and gives us the gift of
eternal life.
Many thanks go to the fantastic team of people at John Wiley & Sons for their outstanding work on this project. Thanks
to Mary James, the acquisitions editor, for offering us the opportunity to work on this book. Also thanks to Brian Herrmann,
the project editor, for keeping things on track and making this book more presentable. Thanks, Brian, for all your hard work
and diligence. The technical editor, Jack Cox, did a wonderful job of double checking all the work in the book, plus making
suggestions to improve the content. Thanks to Nancy Rapoport, the copy editor, for her endless patience and diligence to
make our work readable. We would also like to thank Carole McClendon at Waterside Productions, Inc., for arranging this
opportunity for us, and for helping us out in our writing careers.
Christine would like to thank her husband, Timothy, for his encouragement, patience, and willingness to listen, even when
he has no idea what she is talking about.
www.it-ebooks.info
Introduction
Welcome to the second edition of Linux Command Line and Shell Scripting Bible. Like all books in the Bible series, you can expect
to find both hands-on tutorials and real-world information, as well as reference and background information that provides
a context for what you are learning. This book is a fairly comprehensive resource on the Linux command line and shell
commands. By the time you have completed Linux Command Line and Shell Scripting Bible you will be well prepared to write
your own shell scripts that can automate practically any task on your Linux system.
Who Should Read This Book
If you're a system administrator in a Linux environment, you'll benefit greatly by knowing how to write shell scripts. The
book doesn't walk you through the process of setting up a Linux system, but once you have it running you'll want to start
automating some of the routine administrative tasks. That's where shell scripting comes in, and that's where this book will help
you out. This book will demonstrate how to automate any administrative task using shell scripts, from monitoring system
statistics and data files to generating reports for your boss.

If you're a home Linux enthusiast, you'll also benefit from Linux Command Line and Shell Scripting Bible. Nowadays, it's easy
to get lost in the graphical world of pre-built widgets. Most desktop Linux distributions try their best to hide the Linux
system from the typical user. However, there are times when you have to know what's going on under the hood. This book
shows you how to access the Linux command line prompt and what to do once you get there. Often performing simple tasks,
such as file management, can be done more quickly from the command line than from a fancy graphical interface. There's a
wealth of commands you can use from the command line, and this book shows you how to use them.
How This Book Is Organized
This book leads you through the basics of the Linux command line and into more complicated topics, such as creating your
own shell scripts. The book is divided into five parts, each one building on the previous parts.
Part I assumes that you either have a Linux system running, or are looking into getting a Linux system. Chapter 1,
“Starting with Linux Shells,” describes the parts of a total Linux system and shows how the shell fits in. After describing the
basics of the Linux system, this part continues with the following:
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)
Working with Linux filesystems from the command line (Chapter 7)
Installing and updating software from the command line (Chapter 8)
Using the Linux editors to start writing shell scripts (Chapter 9)
In Part II, you begin writing shell scripts. As you go through the chapters you'll do the following:
Learn how to create and run shell scripts (Chapter 10)
Alter the program flow in a shell script (Chapter 11)
Iterate through code sections (Chapter 12)
Handle data from the user in your scripts (Chapter 13)
See different methods for storing and displaying data from your Script (Chapter 14)
Control how and when your shell scripts run on the system (Chapter 15)
Part III dives into more advanced areas of shell script programming, including:
Creating your own functions to use in all your scripts (Chapter 16)
Utilizing the Linux graphical desktop for interacting with your script users (Chapter 17)

Using advanced Linux commands to filter and parse data files (Chapter 18)
Using regular expressions to define data (Chapter 19)
Learning advanced methods of manipulating data in your scripts (Chapter 20)
Generating reports from raw data (Chapter 21)
Modifying your shell scripts to run in other Linux shells (Chapter 22)
The last section of the book, Part IV, demonstrates how to use shell scripts in real-world environments. In this part, you
www.it-ebooks.info
The last section of the book, Part IV, demonstrates how to use shell scripts in real-world environments. In this part, you
will:
See how to use popular open source databases in your shell scripts (Chapter 23)
Learn how to extract data from Websites, and send data between systems (Chapter 24)
Use e-mail to send notifications and reports to external users (Chapter 25)
Write shell scripts to automate your daily system administration functions (Chapter 26)
Utilize all of the features you've learned from the book to create professional-quality shell scripts (Chapter 27)
Conventions and Features
There are many different organizational and typographical features throughout this book designed to help you get the
most of the information.
Notes and Warnings
Whenever the authors want to bring something important to your attention the information will appear in a Warning.
Warning
This information is important and is set off in a separate paragraph with a special icon.
Warnings provide information about things to watch out for, whether simply inconvenient
or potentially hazardous to your data or systems.
For additional items of interest that relate to the chapter text, the authors will use Notes.
Note
Notes provide additional, ancillary information that is helpful, but somewhat
outside of the current presentation of information.
Minimum Requirements
Linux Command Line and Shell Scripting Bible doesn't focus on any specific Linux distribution, so you'll be able to follow along
in the book using any Linux system you have available. The bulk of the book references the bash shell, which is the default

shell for most Linux systems.
Where to Go from Here
Once you've completed Linux Command Line and Shell Scripting Bible, you'll be well on your way to incorporating 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 will change, adding new features and removing older ones. To keep you knowledge
of Linux fresh, always stay well-informed. Find a good Linux forum site and monitor what's happening in the Linux world.
There are many popular Linux news sites, such as Slashdot and Distrowatch, that provide up-to-the-minute information
about new advances in Linux.
www.it-ebooks.info
Part I
The Linux Command Line
In This Part
Chapter 1: Starting with Linux Shells
Chapter 2: Getting to the Shell
Chapter 3: Basic bash Shell Commands
Chapter 4: More bash Shell Commands
Chapter 5: Using Linux Environment Variables
Chapter 6: Understanding Linux File Permissions
Chapter 7: Managing Filesystems
Chapter 8: Installing Software
Chapter 9: Working with Editors
www.it-ebooks.info
Chapter 1
Starting with Linux Shells
In this Chapter
What is Linux?
Parts of the Linux kernel
Exploring the Linux desktop
Visiting Linux distributions
Before you can dive into working with the Linux command line and shells, it's a good idea to first understand what Linux is,

where it came from, and how it works. This chapter walks you through what Linux is, and explains where the shell and
command line fit in the overall Linux picture.
What Is Linux?
If you've never worked with Linux before, you may be confused as to why there are so many different versions of it available.
I'm sure that you have heard various terms such as distribution, LiveCD, and GNU when looking at Linux packages and been
confused. Wading through the world of Linux for the first time can be a tricky experience. This chapter takes some of the
mystery out of the Linux system before you start working on commands 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 software
Each of these four parts has a specific job in the Linux system. Each of the parts by itself isn't very useful. Figure 1.1 shows a
basic diagram of how the parts fit together to create the overall Linux system.
Figure 1.1 The Linux system
www.it-ebooks.info
Figure 1.1 The Linux system

This section describes these four main parts in detail, and gives you an overview of how they work together to create a
complete Linux system.
Looking into the Linux Kernel
The core of the Linux system is the kernel. The kernel controls all of the hardware and software on the 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 student at the University of Helsinki. He intended it to
be a copy of the Unix system, at the time a popular operating system used at many universities.
After developing the Linux kernel, Linus released it to the Internet community and solicited suggestions for improving it.
This simple process started a revolution in the world of computer operating systems. Soon Linus was receiving suggestions
from students as well as professional programmers from around the world.
Allowing anyone to change programming code in the kernel would result in complete chaos. To simplify things, Linus

acted as a central point for all improvement suggestions. It was ultimately Linus's decision whether or not to incorporate
suggested code in the kernel. This same concept is still in place with the Linux kernel code, except that instead of just Linus
controlling the kernel code, a team of developers has taken on the task.
The kernel is primarily responsible for four main functions:
System memory management
Software program management
Hardware management
Filesystem management
www.it-ebooks.info

×