O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Wicked Cool Shell Scripts, 2nd Edition

Book Description

Useful and fun Linux, UNIX, and Mac OS X shell scripts.

Table of Contents

  1. Cover
  2. Title Page
  3. Copyright Page
  4. About the Authors
  5. About the Technical Reviewer
  6. Brief Contents
  7. Contents in Detail
  8. Acknowledgments for the First Edition
  9. Acknowledgments for the Second Edition
  10. Introduction
    1. What to Take Away
    2. This Book Is for You If . . .
    3. Organization of This Book
    4. Finally . . .
  11. Chapter 0: A Shell Scripts Crash Course
    1. What Is a Shell Script, Anyway?
    2. Running Commands
    3. Configuring Your Login Script
    4. Running Shell Scripts
    5. Making Shell Scripts More Intuitive
    6. Why Shell Scripts?
    7. Let’s Get Cracking
  12. Chapter 1: The Missing Code Library
    1. What Is POSIX?
    2. #1 Finding Programs in the PATH
      1. The Code
      2. How It Works
      3. Running the Script
      4. The Results
      5. Hacking the Script
    3. #2 Validating Input: Alphanumeric Only
      1. The Code
      2. How It Works
      3. Running the Script
      4. The Results
      5. Hacking the Script
    4. #3 Normalizing Date Formats
      1. The Code
      2. How It Works
      3. Running the Script
      4. The Results
      5. Hacking the Script
    5. #4 Presenting Large Numbers Attractively
      1. The Code
      2. How It Works
      3. Running the Script
      4. The Results
      5. Hacking the Script
    6. #5 Validating Integer Input
      1. The Code
      2. How It Works
      3. Running the Script
      4. The Results
      5. Hacking the Script
    7. #6 Validating Floating-Point Input
      1. The Code
      2. How It Works
      3. Running the Script
      4. The Results
      5. Hacking the Script
    8. #7 Validating Date Formats
      1. The Code
      2. How It Works
      3. Running the Script
      4. The Results
      5. Hacking the Script
    9. #8 Sidestepping Poor echo Implementations
      1. The Code
      2. Running the Script
      3. The Results
      4. Hacking the Script
    10. #9 An Arbitrary-Precision Floating-Point Calculator
      1. The Code
      2. How It Works
      3. Running the Script
      4. The Results
    11. #10 Locking Files
      1. The Code
      2. How It Works
      3. Running the Script
      4. The Results
      5. Hacking the Script
    12. #11 ANSI Color Sequences
      1. The Code
      2. How It Works
      3. Running the Script
      4. The Results
      5. Hacking the Script
    13. #12 Building a Shell Script Library
      1. The Code
      2. How It Works
      3. Running the Script
      4. The Results
    14. #13 Debugging Shell Scripts
      1. The Code
      2. How It Works
      3. Running the Script
      4. The Results
      5. Hacking the Script
  13. Chapter 2: Improving on User Commands
    1. #14 Formatting Long Lines
      1. The Code
      2. How It Works
      3. Running the Script
      4. The Results
    2. #15 Backing Up Files as They’re Removed
      1. The Code
      2. How It Works
      3. Running the Script
      4. The Results
      5. Hacking the Script
    3. #16 Working with the Removed File Archive
      1. The Code
      2. How It Works
      3. Running the Script
      4. The Results
      5. Hacking the Script
    4. #17 Logging File Removals
      1. The Code
      2. How It Works
      3. Running the Script
      4. The Results
      5. Hacking the Script
    5. #18 Displaying the Contents of Directories
      1. The Code
      2. How It Works
      3. Running the Script
      4. The Results
      5. Hacking the Script
    6. #19 Locating Files by Filename
      1. The Code
      2. How It Works
      3. Running the Script
      4. The Results
      5. Hacking the Script
    7. #20 Emulating Other Environments: MS-DOS
      1. The Code
      2. How It Works
      3. Running the Script
      4. The Results
      5. Hacking the Script
    8. #21 Displaying Time in Different Time Zones
      1. The Code
      2. How It Works
      3. Running the Script
      4. The Results
      5. Hacking the Script
  14. Chapter 3: Creating Utilities
    1. #22 A Reminder Utility
      1. The Code
      2. How It Works
      3. Running the Script
      4. The Results
      5. Hacking the Script
    2. #23 An Interactive Calculator
      1. The Code
      2. How It Works
      3. Running the Script
      4. The Results
      5. Hacking the Script
    3. #24 Converting Temperatures
      1. The Code
      2. How It Works
      3. Running the Script
      4. The Results
      5. Hacking the Script
    4. #25 Calculating Loan Payments
      1. The Code
      2. How It Works
      3. Running the Script
      4. The Results
      5. Hacking the Script
    5. #26 Keeping Track of Events
      1. The Code
      2. How It Works
      3. Running the Script
      4. The Results
      5. Hacking the Script
  15. Chapter 4: Tweaking Unix
    1. #27 Displaying a File with Line Numbers
      1. The Code
      2. How It Works
      3. Running the Script
      4. The Results
      5. Hacking the Script
    2. #28 Wrapping Only Long Lines
      1. The Code
      2. How It Works
      3. Running the Script
      4. The Results
    3. #29 Displaying a File with Additional Information
      1. The Code
      2. How It Works
      3. Running the Script
      4. The Results
    4. #30 Emulating GNU-Style Flags with quota
      1. The Code
      2. How It Works
      3. Running the Script
      4. The Results
    5. #31 Making sftp Look More Like ftp
      1. The Code
      2. How It Works
      3. Running the Script
      4. The Results
      5. Hacking the Script
    6. #32 Fixing grep
      1. The Code
      2. How It Works
      3. Running the Script
      4. The Results
      5. Hacking the Script
    7. #33 Working with Compressed Files
      1. The Code
      2. How It Works
      3. Running the Script
      4. The Results
      5. Hacking the Script
    8. #34 Ensuring Maximally Compressed Files
      1. The Code
      2. How It Works
      3. Running the Script
      4. The Results
  16. Chapter 5: System Administration: Managing Users
    1. #35 Analyzing Disk Usage
      1. The Code
      2. How It Works
      3. Running the Script
      4. The Results
      5. Hacking the Script
    2. #36 Reporting Disk Hogs
      1. The Code
      2. How It Works
      3. Running the Script
      4. The Results
      5. Hacking the Script
    3. #37 Improving the Readability of df Output
      1. The Code
      2. How It Works
      3. Running the Script
      4. The Results
      5. Hacking the Script
    4. #38 Figuring Out Available Disk Space
      1. The Code
      2. How It Works
      3. Running the Script
      4. The Results
      5. Hacking the Script
    5. #39 Implementing a Secure locate
      1. The Code
      2. How It Works
      3. Running the Script
      4. The Results
      5. Hacking the Script
    6. #40 Adding Users to the System
      1. The Code
      2. How It Works
      3. Running the Script
      4. The Results
      5. Hacking the Script
    7. #41 Suspending a User Account
      1. The Code
      2. How It Works
      3. Running the Script
      4. The Results
    8. #42 Deleting a User Account
      1. The Code
      2. How It Works
      3. Running the Script
      4. The Results
      5. Hacking the Script
    9. #43 Validating the User Environment
      1. The Code
      2. How It Works
      3. Running the Script
      4. The Results
    10. #44 Cleaning Up After Guests Leave
      1. The Code
      2. How It Works
      3. Running the Script
      4. The Results
  17. Chapter 6: System Administration: System Maintenance
    1. #45 Tracking Set User ID Applications
      1. The Code
      2. How It Works
      3. Running the Script
      4. The Results
    2. #46 Setting the System Date
      1. The Code
      2. How It Works
      3. Running the Script
      4. The Results
    3. #47 Killing Processes by Name
      1. The Code
      2. How It Works
      3. Running the Script
      4. The Results
      5. Hacking the Script
    4. #48 Validating User crontab Entries
      1. The Code
      2. How It Works
      3. Running the Script
      4. The Results
      5. Hacking the Script
    5. #49 Ensuring that System cron Jobs Are Run
      1. The Code
      2. How It Works
      3. Running the Script
      4. The Results
      5. Hacking the Script
    6. #50 Rotating Log Files
      1. The Code
      2. How It Works
      3. Running the Script
      4. The Results
      5. Hacking the Script
    7. #51 Managing Backups
      1. The Code
      2. How It Works
      3. Running the Script
      4. The Results
    8. #52 Backing Up Directories
      1. The Code
      2. How It Works
      3. Running the Script
      4. The Results
  18. Chapter 7: Web and Internet Users
    1. #53 Downloading Files via FTP
      1. The Code
      2. How It Works
      3. Running the Script
      4. The Results
      5. Hacking the Script
    2. #54 Extracting URLs from a Web Page
      1. The Code
      2. How It Works
      3. Running the Script
      4. The Results
      5. Hacking the Script
    3. #55 Getting GitHub User Information
      1. The Code
      2. How It Works
      3. Running the Script
      4. The Results
      5. Hacking the Script
    4. #56 ZIP Code Lookup
      1. The Code
      2. How It Works
      3. Running the Script
      4. The Results
      5. Hacking the Script
    5. #57 Area Code Lookup
      1. The Code
      2. How It Works
      3. Running the Script
      4. The Results
      5. Hacking the Script
    6. #58 Keeping Track of the Weather
      1. The Code
      2. How It Works
      3. Running the Script
      4. The Results
      5. Hacking the Script
    7. #59 Digging Up Movie Info from IMDb
      1. The Code
      2. How It Works
      3. Running the Script
      4. The Results
      5. Hacking the Script
    8. #60 Calculating Currency Values
      1. The Code
      2. How It Works
      3. Running the Script
      4. The Results
      5. Hacking the Script
    9. #61 Retrieving Bitcoin Address Information
      1. The Code
      2. How It Works
      3. Running the Script
      4. The Results
      5. Hacking the Script
    10. #62 Tracking Changes on Web Pages
      1. The Code
      2. How It Works
      3. Running the Script
      4. The Results
      5. Hacking the Script
  19. Chapter 8: Webmaster Hacks
    1. Running the Scripts in This Chapter
    2. #63 Seeing the CGI Environment
      1. The Code
      2. How It Works
      3. Running the Script
      4. The Results
    3. #64 Logging Web Events
      1. The Code
      2. How It Works
      3. Running the Script
      4. The Results
      5. Hacking the Script
    4. #65 Building Web Pages on the Fly
      1. The Code
      2. How It Works
      3. Running the Script
      4. The Results
      5. Hacking the Script
    5. #66 Turning Web Pages into Email Messages
      1. The Code
      2. How It Works
      3. Running the Script
      4. The Results
      5. Hacking the Script
    6. #67 Creating a Web-Based Photo Album
      1. The Code
      2. How It Works
      3. Running the Script
      4. The Results
      5. Hacking the Script
    7. #68 Displaying Random Text
      1. The Code
      2. How It Works
      3. Running the Script
      4. The Results
      5. Hacking the Script
  20. Chapter 9: Web and Internet Administration
    1. #69 Identifying Broken Internal Links
      1. The Code
      2. How It Works
      3. Running the Script
      4. The Results
      5. Hacking the Script
    2. #70 Reporting Broken External Links
      1. The Code
      2. How It Works
      3. Running the Script
      4. The Results
    3. #71 Managing Apache Passwords
      1. The Code
      2. How It Works
      3. Running the Script
      4. The Results
      5. Hacking the Script
    4. #72 Syncing Files with SFTP
      1. The Code
      2. How It Works
      3. Running the Script
      4. The Results
      5. Hacking the Script
  21. Chapter 10: Internet Server Administration
    1. #73 Exploring the Apache access_log
      1. The Code
      2. How It Works
      3. Running the Script
      4. The Results
      5. Hacking the Script
    2. #74 Understanding Search Engine Traffic
      1. The Code
      2. How It Works
      3. Running the Script
      4. The Results
      5. Hacking the Script
    3. #75 Exploring the Apache error_log
      1. The Code
      2. How It Works
      3. Running the Script
      4. The Results
    4. #76 Avoiding Disaster with a Remote Archive
      1. The Code
      2. How It Works
      3. Running the Script
      4. The Results
      5. Hacking the Script
    5. #77 Monitoring Network Status
      1. The Code
      2. How It Works
      3. Running the Script
      4. The Results
      5. Hacking the Script
    6. #78 Renicing Tasks by Process Name
      1. The Code
      2. How It Works
      3. Running the Script
      4. The Results
      5. Hacking the Script
  22. Chapter 11: OS X Scripts
    1. #79 Automating screencapture
      1. The Code
      2. How It Works
      3. Running the Script
      4. The Results
      5. Hacking the Script
    2. #80 Setting the Terminal Title Dynamically
      1. The Code
      2. How It Works
      3. Running the Script
      4. The Results
      5. Hacking the Script
    3. #81 Producing Summary Listings of iTunes Libraries
      1. The Code
      2. How It Works
      3. Running the Script
      4. The Results
      5. Hacking the Script
    4. #82 Fixing the open Command
      1. The Code
      2. How It Works
      3. Running the Script
      4. The Results
      5. Hacking the Script
  23. Chapter 12: Shell Script Fun and Games
    1. #83 Unscramble: A Word Game
      1. The Code
      2. How It Works
      3. Running the Script
      4. The Results
      5. Hacking the Script
    2. #84 Hangman: Guess the Word Before It’s Too Late
      1. The Code
      2. How It Works
      3. Running the Script
      4. The Results
      5. Hacking the Script
    3. #85 A State Capitals Quiz
      1. The Code
      2. How It Works
      3. Running the Script
      4. The Results
      5. Hacking the Script
    4. #86 Is That Number a Prime?
      1. The Code
      2. How It Works
      3. Running the Script
      4. The Results
      5. Hacking the Script
    5. #87 Let’s Roll Some Dice
      1. The Code
      2. How It Works
      3. Running the Script
      4. Hacking the Script
    6. #88 Acey Deucey
      1. The Code
      2. How It Works
      3. Running the Script
      4. The Results
      5. Hacking the Script
  24. Chapter 13: Working with the Cloud
    1. #89 Keeping Dropbox Running
      1. The Code
      2. How It Works
      3. Running the Script
      4. The Results
      5. Hacking the Script
    2. #90 Syncing Dropbox
      1. The Code
      2. How It Works
      3. Running the Script
      4. The Results
      5. Hacking the Script
    3. #91 Creating Slide Shows from Cloud Photo Streams
      1. The Code
      2. How It Works
      3. Running the Script
      4. The Results
      5. Hacking the Script
    4. #92 Syncing Files with Google Drive
      1. The Code
      2. How It Works
      3. Running the Script
      4. The Results
      5. Hacking the Script
    5. #93 The Computer Says . . .
      1. The Code
      2. How It Works
      3. Running the Script
      4. The Results
      5. Hacking the Script
  25. Chapter 14: Imagemagick and Working with Graphics Files
    1. #94 A Smarter Image Size Analyzer
      1. The Code
      2. How It Works
      3. Running the Script
      4. The Results
      5. Hacking the Script
    2. #95 Watermarking Images
      1. The Code
      2. How It Works
      3. Running the Script
      4. The Results
      5. Hacking the Script
    3. #96 Framing Images
      1. The Code
      2. How It Works
      3. Running the Script
      4. The Results
      5. Hacking the Script
    4. #97 Creating Image Thumbnails
      1. The Code
      2. How It Works
      3. Running the Script
      4. The Results
      5. Hacking the Script
    5. #98 Interpreting GPS Geolocation Information
      1. The Code
      2. How It Works
      3. Running the Script
      4. The Results
      5. Hacking the Script
  26. Chapter 15: Days and Dates
    1. #99 Finding the Day of a Specific Date in the Past
      1. The Code
      2. How It Works
      3. Running the Script
      4. Hacking the Script
    2. #100 Calculating Days Between Dates
      1. The Code
      2. How It Works
      3. Running the Script
      4. Hacking the Script
    3. #101 Calculating Days Until a Specified Date
      1. The Code
      2. How It Works
      3. Running the Script
      4. Hacking the Script
  27. Appendix A: Installing Bash on Windows 10
    1. Turning On Developer Mode
    2. Installing Bash
    3. Microsoft’s Bash Shell vs. a Linux Distro
  28. Appendix B: Bonus Scripts
    1. #102 Bulk-Renaming Files
      1. The Code
      2. How It Works
      3. Running the Script
      4. The Results
      5. Hacking the Script
    2. #103 Bulk-Running Commands on Multiprocessor Machines
      1. The Code
      2. How It Works
      3. Running the Script
      4. The Results
      5. Hacking the Script
    3. #104 Finding the Phase of the Moon
      1. The Code
      2. How It Works
      3. Running the Script
      4. The Results
      5. Hacking the Script
  29. Index
  30. Resources
  31. 101 Time-Saving, Problem-Solving Shell Scripts
  32. Footnote
    1. Chapter 5: System Administr Ation: Managing Users