Pro Bash Programming, Scripting the GNU-Linux Shell, Johnson C., Varma J.
Pro Bash Programming, Scripting the GNU-Linux Shell, Johnson C., Varma J.
Фрагмент из книги:
A shell script is a file containing one or more commands that you would type on the command line. This chapter describes how to create such a file and make it executable. It also covers some other issues surrounding shell scripts, including what to name the files, where to put them, and how to run them. I will begin with the first program traditionally demonstrated in every computer language: a program that prints “Hello, World!” in your terminal. It’s a simple program, but it is enough to demonstrate a number of important concepts. The code itself is the simplest part of this chapter. Naming the file and deciding where to put it are not complicated tasks, but they are important.
For most of this chapter, you will be working in a terminal. It could be a virtual terminal, a terminal window, or even a dumb terminal. In your terminal, the shell will immediately execute any commands you type (after you press Enter, of course).
Choosing and Using a Text Editor.
For many people, one of the most important pieces of computer software is a word processor. Although I am using one to write this book (LibreOffice Writer), it’s not something I use often. The last time I used a word processor was five years ago when I wrote the first edition of this book. A text editor, on the other hand, is an indispensable tool. I use one for writing e-mail, Usenet articles, shell scripts, PostScript programs, web pages, and more.
A text editor operates on plain-text files. It stores only the characters you type; it doesn’t add any hidden formatting codes. If I type A and press Enter in a text editor and save it, the file will contain exactly two characters: A and a newline. A word-processor file containing the same text would be thousands of times larger. (With abiword, the file contains 2,526 bytes; the LibreOffice.org file contains 7,579 bytes.)
You can write scripts in any text editor, from the basic e3 or nano to the full-featured emacs or nedit. The better text editors allow you to have more than one file open at a time. They make editing code easier with, for example, syntax highlighting, automatic indentation, autocompletion, spell checking, macros, search and replace, and undo. Ultimately, which editor you choose is a matter of personal preference. I use GNU emacs (see Figure 1-1).
Оглавление.
About the Authors.
Acknowledgments.
Chapter 1: Hello, World: Your First Shell Program.
Chapter 2: Input, Output, and Throughput.
Chapter 3: Looping and Branching.
Chapter 4: Command-Line Parsing and Expansion.
Chapter 5: Parameters and Variables.
Chapter 6: Shell Functions.
Chapter 7: String Manipulation.
Chapter 8: File Operations and Commands.
Chapter 9: Reserved Words and Built-In Commands.
Chapter 10: Writing Bug-Free Scripts and Debugging the Rest.
Chapter 11: Programming for the Command Line.
Chapter 12: Runtime Configuration.
Chapter 13: Data Processing.
Chapter 14: Scripting the Screen.
Chapter 15: Entry-Level Programming.
Appendix A: Shell Variables.
Index.
Бесплатно скачать электронную книгу в удобном формате, смотреть и читать:
Скачать книгу Pro Bash Programming, Scripting the GNU-Linux Shell, Johnson C., Varma J. — fileskachat.com, быстрое и бесплатное скачивание.
Скачать pdf
Ниже можно купить эту книгу по лучшей цене со скидкой с доставкой по всей России. Купить эту книгу
Linux Command Line and Shell Scripting Bible
1 Linux Command Line and Shell Scripting Bible Richard Blum Wiley Publishing, Inc.
3 Linux Command Line and Shell Scripting Bible
5 Linux Command Line and Shell Scripting Bible Richard Blum Wiley Publishing, Inc.
6 Linux Command Line and Shell Scripting Bible Published by Wiley Publishing, Inc Crosspoint Boulevard Indianapolis, IN Copyright 2008 by Wiley Publishing, Inc., Indianapolis, Indiana Published simultaneously in Canada ISBN: Manufactured in the United States of America 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) , fax (978) Requests to the Publisher for permission should be addressed to the Legal Department, Wiley Publishing, Inc., Crosspoint Blvd., Indianapolis, IN 46256, (317) , fax (317) , 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 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) , outside the U.S. at (317) or fax (317) 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.
7 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)
8 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 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.
9 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
11 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 Chapter 6: Understanding Linux File Permissions Chapter 7: Working with Editors Part II Shell Scripting Basics Chapter 8: Basic Script Building Chapter 9: Using Structured Commands Chapter 10: More Structured Commands Chapter 11: Handling User Input Chapter 12: Presenting Data Chapter 13: Script Control Part III Advanced Shell Scripting Chapter 14: Creating Functions Chapter 15: Adding Color to Scripts Chapter 16: Introducing sed and gawk Chapter 17: Regular Expressions Chapter 18: Advanced sed Chapter 19: Advanced gawk Part IV Alternative Linux Shells Chapter 20: The ash Shell Chapter 21: The tcsh Shell Chapter 22: The Korn Shell Chapter 23: The zsh Shell ix
12 Contents at a Glance Part V Advanced Topics Chapter 24: Using a Database Chapter 25: Using the Web Chapter 26: Using Chapter 27: Shell Scripts for Administrators Appendix A: Quick Guide to bash Commands Appendix B: Quick Guide to sed and gawk Appendix C: Comparing Shells Index x
13 Acknowledgments xxv Introduction xxvii Part I The Linux Command Line Chapter 1: Starting with Linux Shells 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 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
14 Contents Chapter 3: Basic bash Shell Commands 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 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 Monitoring Programs. 91 Peeking at the processes. 91 Real-time process monitoring. 98 Stopping processes Monitoring Disk Space Mounting media Using the df command Using the du command Working with Data Files Sorting data Searching for data Compressing data Archiving data Summary xii
15 Contents Chapter 5: Using Linux Environment Variables What Are Environment Variables? Global environment variables Local environment variables Setting Environment Variables Setting local environment variables Setting global environment variables Removing Environment Variables Default Shell Environment Variables Setting the PATH Environment Variable Locating System Environment Variables Login shell Interactive shell Non-interactive shell Variable Arrays Using Command Aliases Summary Chapter 6: Understanding Linux File Permissions Linux Security The /etc/passwd file The /etc/shadow file Adding a new user Removing a user Modifying a user Using Linux Groups The /etc/group file Creating new groups Modifying groups Decoding File Permissions Using file permission symbols Default file permissions Changing Security Settings Changing permissions Changing ownership Sharing Files Summary Chapter 7: Working with Editors The vim Editor The basics of vim Editing data Copy and paste Search and substitute xiii