You are previewing Linux Shell Scripting with Bash.
O'Reilly logo
Linux Shell Scripting with Bash

Book Description

According to IDC, over 170,000 new servers were shipped in the first quarter of 2003. That also means that there were potentially more than 170,000 people like you charged with administering these systems without the proper knowledge or training to do so. Linux Shell Scripting with Bash will help you learn to not only get your system up and running quickly by employing the Bash shell, but it will also show you professional scripting solutions through the use of structured programming and standard Linux development tools. The book focuses on the Linux environment, which the shell relies on to function, and the robust Linux tool set, making this book ideal for learning shell scripting. Real-world scripts are included that are readable, extendable and easy to debug. This book is an asset to any Linux user.

Table of Contents

  1. Copyright
    1. Dedication
  2. About the Author
  3. Acknowledgments
  4. We Want to Hear from You!
  5. Reader Services
  6. Introduction COSC 101
    1. Notation Used in This Book
    2. The Origin of Bash
    3. When Is a Program a Shell Script?
    4. The Necessity of Structured Shell Programming
    5. Installing Bash
    6. Bash and Other Scripting Tools
  7. 1. The Linux Environment
    1. The Origin of Linux
    2. Files and File Systems
    3. Directories
    4. Inodes and Links
    5. Pipe and Socket Files
    6. Device Files
  8. 2. Operating the Shell
    1. Bash Keywords
    2. Command Basics
    3. Command-Line Editing
    4. Variable Assignments and Displaying Messages
    5. Multiple Commands
    6. Command History
    7. Directory Commands
    8. Specialized Navigation and History
    9. The Colon Command
    10. Reference Section
      1. date Command Switches
      2. stty Command Switches
      3. history Command Switches
      4. pwd Command Switches
      5. dirs Command Switches
  9. 3. Files, Users, and Shell Customization
    1. Listing Files
    2. printf Command
    3. Getting Help
    4. Fixing the Display
    5. Working with Files
    6. Working with People
    7. Shell Aliases
    8. The Bash Hash Table
    9. Customizing Your Prompt
    10. Long Command Lines
    11. Customizing Command-Line Editing
    12. Your Session Profile
    13. Reference Section
      1. ls Command Switches
      2. printf Formatting Codes
      3. printf Backslash Codes
      4. rm Command Switches
      5. cp Command Switches
      6. mv Command Switches
  10. 4. Script Basics
    1. Creating a Script
    2. Creating a Well-Behaved Script
    3. The Header
    4. Global Declarations
    5. Sanity Checks
    6. The Main Script
    7. Cleanup
    8. Stopping a Script
    9. Reading Keyboard Input
    10. Basic Redirection
    11. Standard Output, Error, and Input
    12. Built-In Versus Linux Commands
    13. The Set and Shopt Commands
    14. Reference Section
      1. command Command Switches
      2. enable Command Switches
      3. read Command Switches
      4. suspend Command Switches
  11. 5. Variables
    1. Variable Basics
    2. Predefined Variables
    3. The Effect of Quotations
    4. Variable Attributes
    5. Arrays
    6. Exporting Variables and the Linux Environment
    7. The eval Command
    8. story.bash: A Story Generator
    9. Reference Section
      1. Declare Command Switches
      2. Bash Predefined Variables
  12. 6. Expressions
    1. Expansions
    2. The Basic if Command
    3. File Expressions
    4. Multiple Tests
    5. Strings
    6. Arithmetic Expressions
    7. Logical Expressions
    8. Relational Operations
    9. Bitwise Operations
    10. Self-Referential Operations
    11. Other let Features
    12. temperature.bash: Converting Fahrenheit to Celsius
    13. Arithmetic Tests
    14. Pattern Recognition
    15. Globbing Options
    16. Filename Brace Expansion ( {..} )
    17. Dollar Sign Substitutions
      1. ANSI C Escape Expansion ($')
      2. Locale Translation ($")
      3. Variable Name Matching (!*)
      4. Variable Length (#)
      5. Default Values (:-)
      6. Assignment of Default Values (:=)
      7. Variable Existence Check (:?)
      8. Overriding a Default Value (:+)
      9. Substrings (:n)
      10. Substring Removal by Pattern (%, #, %%, and ##)
      11. Substring Replacement by Pattern (//)
      12. Command Result Substitution ( (..) )
    18. Arithmetic Expression Substitution ( ((..)) )
      1. Other Test Expressions
    19. mixer.bash: HTML Color Mixer
    20. Reference Section
      1. Test Command Switches
      2. Test Command String Tests
      3. Character Classes
      4. ASCII C Escape Expansion
  13. 7. Compound Commands
    1. Command Status Codes
    2. if Command
    3. case Command
    4. while Loop
    5. until Loop
    6. for Loops
    7. Embedded let ( ((..)) )
    8. Grouping Commands ( {..} )
    9. report.bash: Report Formatter
  14. 8. Debugging and Version Control
    1. Shell Debugging Features
    2. Debug Traps
    3. Version Control (CVS)
    4. Creating Transcripts
    5. Watching Running Scripts
    6. Timing Execution with Time
    7. Creating Man Pages
    8. Source Code Patches
    9. Shell Archives
    10. Reference Section
      1. tee Command Switches
      2. Linux Time Command Switches
      3. Bash Time Command Format Codes
      4. Linux Time Command Format Codes
      5. Shell Debugging Options
      6. shar Command Switches
  15. 9. Parameters and Subshells
    1. Positional Parameters
    2. The getopts Command
    3. The getopt Command
    4. Subshells
    5. Reference Section
      1. getopt Command Switches
  16. 10. Job Control and Signals
    1. Job Control
    2. Signals
    3. The suspend Command
    4. Traps
    5. Exit Handlers
    6. The killall Command
    7. Being Nice
    8. Process Status
    9. Reference Section
      1. jobs Command Switches
      2. kill Command Switches
      3. renice Command Switches
      4. ps Command Switches
      5. ps Command Sort Codes
  17. 11. Text File Basics
    1. Working with Pathnames
    2. File Truncation
    3. Identifying Files
    4. Creating and Deleting Files
    5. Moving and Copying Files
    6. More Information About Files
    7. Transferring Files Between Accounts (wget)
    8. Transferring Files with FTP
    9. Transferring Files with Secure FTP (sftp)
    10. Verifying Files
    11. Splitting Large Files
    12. Tabs and Spaces
    13. Temporary Files
    14. Lock Files
    15. Named Pipes
    16. Process Substitution
    17. Opening Files
    18. Using head and tail
    19. File Statistics
    20. Cutting
    21. Pasting
    22. Columns
    23. Folding
    24. Joining
    25. Merging
    26. Reference Section
      1. type Command Switches
      2. file Command Switches
      3. stat Command Switches
      4. statftime Command Format Codes
      5. wget Command Switches
      6. ftp Command Switches
      7. csplit Command Switches
      8. expand Command Switches
      9. unexpand Command Switches
      10. mktemp Command Switches
      11. head Command Switches
      12. tail Command Switches
      13. wc Command Switches
      14. cut Command Switches
      15. paste Command Switches
      16. join Command Switches
      17. merge Command Switches
  18. 12. Text File Processing
    1. Finding Lines
    2. Locating Files
    3. Finding Files
    4. Sorting
    5. Character Editing (tr)
    6. File Editing (sed)
    7. Compressing Files
    8. Reference Section
      1. grep Command Switches
      2. find Command Switches
      3. find -printf Formatting Codes
      4. sort Command Switches
      5. tar Command Switches
      6. tr Command Switches
      7. sed Command Switches
      8. sed Editing Codes
  19. 13. Console Scripting
    1. The Linux Console
    2. The Console Keyboard
    3. The Console Display
    4. tput
    5. select Menus
    6. Custom Menus
    7. Reference Section
      1. showkey Command Switches
      2. setleds Command Switches
      3. dumpkeys Command Switches
      4. setterm Command Switches
      5. dialog Command Switches
  20. 14. Functions and Script Execution
    1. Running Scripts
    2. The Linux Execution Environment
    3. The Source Command (.)
    4. Switching Scripts with exec
    5. Writing Recurring Scripts
    6. Writing Continually Executing Scripts
    7. Shell Functions
    8. Local Variables
    9. Recursion and Nested Functions
    10. Function Attributes
    11. Reference Section
      1. exec Command Switches
      2. crontab Command Switches
  21. 15. Shell Security
    1. The Basic Linux Security Model
    2. Knowing Who You Are (id)
    3. Transferring Ownership (chown/chgrp)
    4. Changing Access Rights (chmod)
    5. Default Access Rights (umask)
    6. setuid/setgid and Scripts
    7. The chroot Command
    8. Resource Limits (ulimit)
    9. Restricted Shells
    10. Secure File Deletion (wipe)
    11. Reference Section
      1. id Command Switches
      2. chown Command Switches
      3. chmod Command Switches
      4. ulimit Command Switches
      5. wipe Command Switches
  22. 16. Network Programming
    1. Sockets
    2. Client-Server and Peer-to-Peer
    3. Network Clients
    4. CGI Scripting
    5. CGI Environment Variables
    6. Processing Forms
    7. Basic Web Page Stripping (lynx)
    8. Reference Section
      1. host Command Switches
      2. Common CGI Variables
      3. lynx Page-Stripping Switches
  23. 17. Data Structures and Databases
    1. Associative Arrays Using Bash Arrays
    2. Hash Tables Using Bash Arrays
    3. Binary Trees Using Bash Arrays
    4. Working with PostgreSQL Databases (psql)
    5. Working with MySQL Databases
    6. Reference Section
      1. psql Command Switches
      2. mysql Command Switches
  24. 18. Final Topics
    1. The echo Command
    2. More Uses for set
    3. Date Conversions
    4. Completions
    5. Locales
    6. The du Command
    7. Memory Usage
    8. noclobber and Forced Overwriting
    9. The fc Command
    10. ! Word Designators and Modifiers
    11. Running Scripts from C
    12. Journey's End
    13. Reference Section
      1. echo Command Switches
      2. echo Escape Codes
      3. compgen Command Switches
      4. compgen Action Types
      5. complete Command Switches
      6. du Command Switches
      7. ! Word Modifiers
  25. A. A Complete Example
  26. B. Summary of Bash Built-In Commands
  27. C. Bash Options
  28. D. Error Codes
  29. E. Signals
  30. F. ASCII Table
  31. Glossary