Mastering UNIX® Shell Scripting: Bash, Bourne, and Korn Shell Scripting for Programmers, System Administrators, and UNIX Gurus, Second Edition

Book description

This ebook does not provide access to the companion materials.

UNIX expert Randal K. Michael guides you through every detail of writing shell scripts to automate specific tasks. Each chapter begins with a typical, everyday UNIX challenge, then shows you how to take basic syntax and turn it into a shell scripting solution. Covering Bash, Bourne, and Korn shell scripting, this updated edition provides complete shell scripts plus detailed descriptions of each part. UNIX programmers and system administrators can tailor these to build tools that monitor for specific system events and situations, building solid UNIX shell scripting skills to solve real-world system administration problems.

Table of contents

  1. Cover Page
  2. Title page
  3. Copyright
  4. Dedication
  5. About the Author
  6. Credits
  7. Contents
  8. Acknowledgments
  9. Introduction
    1. Overview of the Book and Technology
    2. How This Book Is Organized
    3. Who Should Read this Book
    4. Tools You Will Need
    5. What's on the Web Site
    6. Summary
  10. Part I: The Basics of Shell Scripting
    1. CHAPTER 1: Scripting Quick Start and Review
      1. Case Sensitivity
      2. UNIX Special Characters
      3. Shells
      4. Shell Scripts
      5. Functions
      6. Running a Shell Script
      7. Comments and Style in Shell Scripts
      8. Control Structures
      9. Using break, continue, exit, and return
      10. Here Document
      11. Shell Script Commands
      12. Symbol Commands
      13. Variables
      14. Command-Line Arguments
      15. shift Command
      16. Special Parameters $* and $@
      17. Double Quotes, Forward Tics, and Back Tics
      18. Using awk on Solaris
      19. Using the echo Command Correctly
      20. Math in a Shell Script
      21. Built-In Mathematical Functions
      22. File Permissions, suid and sgid Programs
      23. Running Commands on a Remote Host
      24. Setting Traps
      25. User-Information Commands
      26. ps Command
      27. Communicating with Users
      28. Uppercase or Lowercase Text for Easy Testing
      29. Check the Return Code
      30. Time-Based Script Execution
      31. Output Control
      32. Catching a Delayed Command Output
      33. Fastest Ways to Process a File Line-by-Line
      34. Using Command Output in a Loop
      35. Mail Notification Techniques
      36. Creating a Progress Indicator
      37. Working with Record Files
      38. Creating a Pseudo-Random Number
      39. Checking for Stale Disk Partitions in AIX
      40. Automated Host Pinging
      41. Highlighting Specific Text in a File
      42. Keeping the Printers Printing
      43. Automated FTP File Transfer
      44. Using rsync to Replicate Data
      45. Capturing a List of Files Larger than $MEG
      46. Capturing a User's Keystrokes
      47. Using the bc Utility for Floating-Point Math
      48. Number Base Conversions
      49. Create a Menu with the select Command
      50. Removing Repeated Lines in a File
      51. Removing Blank Lines from a File
      52. Testing for a Null Variable
      53. Directly Access the Value of the Last Positional Parameter, $#
      54. Remove the Column Headings in a Command Output
      55. Arrays
      56. Testing a String
      57. Summary
    2. CHAPTER 2: 24 Ways to Process a File Line-by-Line
      1. Command Syntax
      2. 24 Methods to Parse a File Line-by-Line
      3. Timing Each Method
      4. What about Using Command Input Instead of File Input?
      5. Summary
      6. Lab Assignments
    3. CHAPTER 3: Automated Event Notification
      1. Basics of Automating Event Notification
      2. Problems with Outbound Mail
      3. Dial-Out Modem Software
      4. SNMP Traps
      5. Summary
      6. Lab Assignments
    4. CHAPTER 4: Progress Indicators Using a Series of Dots, a Rotating Line, or Elapsed Time
      1. Indicating Progress with a Series of Dots
      2. Indicating Progress with a Rotating Line
      3. Indicating Progress with Elapsed Time
      4. Combining Feedback Methods
      5. Other Options to Consider
      6. Summary
      7. Lab Assignments
  11. Part II: Scripts for Programmers, Testers, and Analysts
    1. CHAPTER 5: Working with Record Files
      1. What Is a Record File?
      2. Processing the Record Files
      3. Tasks for Records and Record Files
      4. The Merge Process
      5. Putting It All Together
      6. Other Things to Consider
      7. Summary
      8. Lab Assignments
    2. CHAPTER 6: Automated FTP Stuff
      1. Syntax
      2. Automating File Transfers and Remote Directory Listings
      3. What about Encryption?
      4. Other Things to Consider
      5. Summary
      6. Lab Assignments
    3. CHAPTER 7: Using rsync to Efficiently Replicate Data
      1. Syntax
      2. Generic rsync Shell Script
      3. Replicating Multiple Directories with rsync
      4. Replicating Multiple Filesystems with rsync
      5. Replicating an Oracle Database with rsync
      6. rsync Copy Shell Script
      7. Summary
      8. Lab Assignments
    4. CHAPTER 8: Automating Interactive Programs with Expect and Autoexpect
      1. Downloading and Installing Expect
      2. The Basics of Talking to an Interactive Script or Program
      3. Using autoexpect to Automatically Create an Expect Script
      4. Working with Variables
      5. What about Conditional Tests?
      6. Using Expect Scripts with Sun Blade Chassis and JumpStart
      7. Summary
      8. Lab Assignments
    5. CHAPTER 9: Finding Large Files and Files of a Specific Type
      1. Syntax
      2. Remember That File and Directory Permissions Thing
      3. Don't Be Shocked by the Size of the Files
      4. Creating the Script
      5. Other Options to Consider
      6. Summary
      7. Lab Assignments
    6. CHAPTER 10: Process Monitoring and Enabling Pre-Processing, Startup, and Post-Processing Events
      1. Syntax
      2. Monitoring for a Process to Start
      3. Monitoring for a Process to End
      4. Monitor and Log as a Process Starts and Stops
      5. Timed Execution for Process Monitoring, Showing Each PID, and Timestamp with Event and Timing Capability
      6. Other Options to Consider
      7. Summary
      8. Lab Assignments
    7. CHAPTER 11: Pseudo-Random Number and Data Generation
      1. What Makes a Random Number?
      2. The Methods
      3. Shell Script to Create Pseudo-Random Numbers
      4. Creating Unique Filenames
      5. Creating a File Filled with Random Characters
      6. Other Things to Consider
      7. Summary
      8. Lab Assignments
    8. CHAPTER 12: Creating Pseudo-Random Passwords
      1. Randomness
      2. Creating Pseudo-Random Passwords
      3. Syntax
      4. Building the Password-Creation Script
      5. Other Options to Consider
      6. Summary
      7. Lab Assignments
    9. CHAPTER 13: Floating-Point Math and the bc Utility
      1. Syntax
      2. Creating Some Shell Scripts Using bc
      3. Other Options to Consider
      4. Summary
      5. Lab Assignments
    10. CHAPTER 14: Number Base Conversions
      1. Syntax
      2. Scripting the Solution
      3. An Easy, Interactive Script to Convert Between Bases
      4. Using the bc Utility for Number Base Conversions
      5. Other Options to Consider
      6. Summary
      7. Lab Assignments
    11. CHAPTER 15: hgrep: Highlighted grep Script
      1. Reverse Video Control
      2. Building the hgrep.bash Shell Script
      3. Other Options to Consider
      4. Summary
      5. Lab Assignments
    12. CHAPTER 16: Monitoring Processes and Applications
      1. Monitoring Local Processes
      2. Remote Monitoring with Secure Shell and Remote Shell
      3. What about Waiting for Something to Complete Executing?
      4. Other Things to Consider
      5. Summary
      6. Lab Assignments
  12. Part III: Scripts for Systems Administrators
    1. CHAPTER 17: Filesystem Monitoring
      1. Syntax
      2. Adding Exceptions Capability to Monitoring
      3. Using the MB-of-Free-Space Method
      4. Using MB of Free Space with Exceptions
      5. Percentage Used — MB Free and Large Filesystems
      6. Running Filesystem Scripts on AIX, Linux, HP-UX, OpenBSD, and Solaris
      7. Other Options to Consider
      8. Summary
      9. Lab Assignments
    2. CHAPTER 18: Monitoring Paging and Swap Space
      1. Syntax
      2. Creating the Shell Scripts
      3. Other Options to Consider
      4. Summary
      5. Lab Assignments
    3. CHAPTER 19: Monitoring System Load
      1. Installing the System-Statistics Programs in Linux
      2. Syntax
      3. Scripting the Solutions
      4. Other Options to Consider
      5. Summary
      6. Lab Assignments
    4. CHAPTER 20: Monitoring for Stale Disk Partitions (AIX-Specific)
      1. AIX Logical Volume Manager (LVM)
      2. The Commands and Methods
      3. Other Options to Consider
      4. Summary
      5. Lab Assignment
    5. CHAPTER 21: Turning On/Off SSA Identification Lights
      1. Syntax
      2. The Scripting Process
      3. Other Things to Consider
      4. Summary
      5. Lab Assignment
    6. CHAPTER 22: Automated Hosts Pinging with Notification of Failure
      1. Syntax
      2. Creating the Shell Script
      3. Other Options to Consider
      4. Summary
      5. Lab Assignments
    7. CHAPTER 23: Creating a System-Configuration Snapshot
      1. Syntax
      2. Creating the Shell Script
      3. Other Options to Consider
      4. Summary
      5. Lab Assignment
    8. CHAPTER 24: Compiling, Installing, Configuring, and Using sudo
      1. The Need for sudo
      2. Configuring sudo on Solaris
      3. Downloading and Compiling sudo
      4. Compiling sudo
      5. Configuring sudo
      6. Using sudo
      7. Using sudo in a Shell Script
      8. Logging to the syslog with sudo
      9. The sudo Log File
      10. Summary
      11. Lab Assignments
    9. CHAPTER 25: Print-Queue Hell: Keeping the Printers Printing
      1. System V versus BSD versus CUPS Printer Systems
      2. Putting It All Together
      3. Other Options to Consider
      4. Summary
      5. Lab Assignments
    10. CHAPTER 26: Those Pesky Sarbanes-Oxley (SOX) Audits
      1. What to Expect
      2. How to Work with the Auditors
      3. What the Auditors Want to See
      4. Some Handy Commands
      5. Using the dirname and basename Commands
      6. Other Things to Consider
      7. Summary
      8. Lab Assignments
    11. CHAPTER 27: Using Dirvish with rsync to Create Snapshot-Type Backups
      1. How Does Dirvish Work?
      2. Configuring Dirvish
      3. Performing a Full System Backup
      4. Using Dirvish on the Command Line
      5. A Menu-Interface Shell Script to Control Dirvish
      6. Putting It All Together
      7. Using the dirvish_ctrl Shell Script
      8. Other Things to Consider
      9. Summary
      10. Lab Assignments
    12. CHAPTER 28: Monitoring and Auditing User Keystrokes
      1. Syntax
      2. Scripting the Solution
      3. Other Options to Consider
      4. Summary
      5. Lab Assignments
      6. A Closing Note from the Author
  13. APPENDIX A: What's on the Web Site
    1. Shell Scripts
    2. Functions
  14. Index

Product information

  • Title: Mastering UNIX® Shell Scripting: Bash, Bourne, and Korn Shell Scripting for Programmers, System Administrators, and UNIX Gurus, Second Edition
  • Author(s): Randal K. Michael
  • Release date: June 2008
  • Publisher(s): Wiley
  • ISBN: 9780470183014