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

Bash Cookbook

Book Description

Create simple to advanced shell scripts and enhance your system functionality with effective recipes

Key Features

  • Automate tedious and repetitive tasks
  • Create several novel applications ranging from a simple IRC logger to a Web Scraper
  • Manage your system efficiently by becoming a seasoned Bash user

Book Description

In Linux, one of the most commonly used and most powerful tools is the Bash shell. With its collection of engaging recipes, Bash Cookbook takes you through a series of exercises designed to teach you how to effectively use the Bash shell in order to create and execute your own scripts.

The book starts by introducing you to the basics of using the Bash shell, also teaching you the fundamentals of generating any input from a command. With the help of a number of exercises, you will get to grips with the automation of daily tasks for sysadmins and power users. Once you have a hands-on understanding of the subject, you will move on to exploring more advanced projects that can solve real-world problems comprehensively on a Linux system. In addition to this, you will discover projects such as creating an application with a menu, beginning scripts on startup, parsing and displaying human-readable information, and executing remote commands with authentication using self-generated Secure Shell (SSH) keys.

By the end of this book, you will have gained significant experience of solving real-world problems, from automating routine tasks to managing your systems and creating your own scripts.

What you will learn

  • Understand the basics of Bash shell scripting on a Linux system
  • Gain working knowledge of how redirections and pipes interact
  • Retrieve and parse input or output of any command
  • Automate tasks such as data collection and creating and applying a patch
  • Create a script that acts like a program with different features
  • Customize your Bash shell and discover neat tricks to extend your programs
  • Compile and install shell and log commands on your system's console using Syslog

Who this book is for

The Bash Cookbook is for you if you are a power user or system administrator involved in writing Bash scripts in order to automate tasks. This book is also ideal if you are interested in learning how to automate complex daily tasks.

Downloading the example code for this book You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the files e-mailed directly to you.

Table of Contents

  1. Title Page
  2. Copyright and Credits
    1. Bash Cookbook
  3. Packt Upsell
    1. Why subscribe?
    2. PacktPub.com
  4. Contributors
    1. About the authors
    2. About the reviewer
    3. Packt is searching for authors like you
  5. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
      1. Download the example code files
      2. Conventions used
    4. Sections
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    5. Get in touch
      1. Reviews
  6. Crash Course in Bash
    1. Getting started with Bash and CLI fundamentals
      1. Your first Bash script with Vim
    2. Creating and using basic variables
      1. Hands-on variable assignment
    3. Hidden Bash variables and reserved words
    4. Conditional logic using if, else, and elseif
      1. Evaluating binary numbers
      2. Evaluating strings
      3. Nested if statements
    5. Case/switch statements and loop constructs
      1. Basic case statement
      2. Basic loops
        1. For loop
        2. Do while loop
        3. Until loop
    6. Using functions and parameters
      1. Using a function with parameters within a for loop
    7. Including source files
      1. Including/importing a library script and using external functions
    8. Retrieving return codes and output
      1. Return code 101
    9. Linking commands, pipes, and input/output
      1. Redirection and pipe bonzanza
    10. Getting program input parameters 
      1. Passing your program flags
    11. Getting additional information about commands
    12. Summary
  7. Acting Like a Typewriter and File Explorer
    1. Introduction
    2. Basic searching for strings and files
      1. Getting ready
      2. How to do it...
      3. How it works...
    3. Using wildcards and regexes
      1. Getting ready
      2. How to do it...
      3. How it works...
    4. Math and calculations in script
      1. Getting ready
      2. How to do it...
      3. How it works...
    5. Striping/altering/sorting/deleting/searching strings with Bash only
      1. Getting ready
      2. How to do it...
      3. How it works...
    6. Using SED and AWK to remove/replace substrings
      1. Getting ready
      2. How to do it...
      3. How it works...
    7. Formatting your data/output using echo and printf
      1. Getting ready
      2. How to do it...
      3. How it works...
    8. Readying your script for different languages with internationalization
      1. Getting ready
      2. How to do it...
      3. How it works...
    9. Calculating statistics and reducing duplicates based on file contents
      1. Getting ready
      2. How to do it...
      3. How it works...
    10. Using file attributes with conditional logic
      1. Getting ready
      2. How to do it...
      3. How it works...
    11. Reading delimited data and altered output format
      1. Getting ready
      2. How to do it...
      3. How it works...
  8. Understanding and Gaining File System Mastery
    1. Introduction
    2. Viewing files from various angles – head, tail, less, and more
      1. Getting ready
      2. How to do it...
      3. How it works...
    3. Searching for files by name and/or extension
      1. Getting ready
      2. How to do it...
      3. How it works...
    4. Creating a diff of two files and patching
      1. Getting ready
      2. How to do it...
      3. How it works...
    5. Creating symbolic links and using them effectively
      1. How to do it...
      2. How it works...
    6. Crawling filesystem directories and printing a tree
      1. Getting ready
      2. How to do it...
      3. How it works...
    7. Finding and deleting duplicate files or directories
      1. Getting ready
      2. How to do it...
      3. How it works...
    8. Joining and splitting files at arbitrary positions
      1. Getting ready
      2. How to do it...
      3. How it works...
    9. Generating datasets and random files of various size
      1. Getting ready
      2. How to do it...
      3. How it works...
  9. Making a Script Behave Like a Daemon
    1. Introduction
    2. Running a program continuously (forever) using looping constructs or recursion
      1. Getting ready
      2. How to do it...
      3. How it works...
    3. Keeping programs/scripts running after logoff
      1. Getting ready
      2. How to do it...
      3. How it works...
    4. Invoking commands when they require permissions
      1. Getting ready
      2. How to do it...
      3. How it works...
    5. Sanitizing user input and for repeatable results
      1. Getting ready
      2. How to do it...
      3. How it works...
    6. Making a simple multi-level user menu using select
      1. Getting ready
      2. How to do it...
      3. How it works...
    7. Generating and trapping signals for cleanup
      1. Getting ready
      2. How to do it...
      3. How it works...
    8. Using temporary files and lock files in your program
      1. Getting ready
      2. How to do it...
      3. How it works...
    9. Leveraging timeout when waiting for command completion
      1. Getting ready
      2. How to do it...
      3. How it works...
    10. Creating a file-in-file-out program and running processes in parallel
      1. Getting ready
      2. How to do it...
      3. How it works...
    11. Executing your script on startup
      1. Getting ready
      2. How to do it...
      3. How it works...
  10. Scripts for System Administration Tasks
    1. Introduction
    2. Gathering and aggregating system information
      1. Getting ready
      2. How to do it...
      3. How it works...
    3. Gathering network information and connectivity diagnostics
      1. Getting ready
      2. How to do it...
      3. How it works...
    4. Configuring basic network connectivity
      1. Getting ready
      2. How to do it...
      3. How it works...
    5. Monitoring directories and files
      1. Getting ready
      2. How to do it...
      3. How it works...
    6. Compressing and archiving files
      1. Getting ready
      2. How to do it...
      3. How it works...
    7. Rotating files from RAM to storage for log rotation
      1. Getting ready
      2. How to do it...
        1. Adding configuration to /etc/logrotate.d/
      3. How it works...
    8. Using Linux iptables for a firewall
      1. Getting ready
      2. How to do it...
      3. How it works...
    9. Accessing SQL databases remotely or locally
      1. Getting ready
      2. How to do it...
      3. How it works...
    10. Creating SSH keys for password less remote access
      1. Getting ready
      2. How to do it...
    11. Creating and configuring cron Jobs for task scheduling
      1. How to do it...
      2. How it works...
    12. Creating users and groups systematically
      1. How to do it...
      2. How it works...
  11. Scripts for Power Users
    1. Introduction
    2. Creating Syslog entries and generating an alarm  
      1. Getting ready
      2. How to do it...
      3. How it works...
    3. Backing up and erasing media, disks, and partitions with DD
      1. Getting ready
      2. How to do it...
      3. How it works...
    4. Creating graphics and presentations on the CLI
      1. Getting ready
      2. How to do it...
      3. How it works...
    5. Checking for file integrity and tampering
      1. Getting ready
      2. How to do it...
      3. How it works...
    6. Mounting network file systems and retrieving files
      1. Getting ready
      2. How to do it...
      3. How it works...
    7. Browsing the web from the CLI
      1. Getting ready
      2. How to do it...
      3. How it works...
    8. Capturing network traffic headlessly
      1. Getting ready
      2. How to do it...
      3. How it works...
    9. Finding binary dependencies
      1. Getting ready
      2. How to do it...
      3. How it works...
    10. Fetching time from different locations
      1. Getting ready
      2. How to do it...
      3. How it works...
    11. Encrypting/decrypting files from a script
      1. Getting ready
      2. How to do it...
      3. How it works...
  12. Writing Bash to Win and Profit
    1. Introduction
    2. Creating a lame utility HTTP server
      1. Getting ready
      2. How to do it…
      3. How it works…
    3. Parsing RSS feeds and output HTML
      1. Getting ready
      2. How to do it…
      3. How it works…
    4. Scraping the web and collecting files
      1. Getting ready
      2. How to do it…
      3. How it works…
    5. Making a simple IRC chat bot logger
      1. Getting ready
      2. How to do it…
      3. How it works…
    6. Blocking IP addresses from failed SSH attempts
      1. Getting ready
      2. How to do it…
      3. How it works…
    7. Playing and managing audio from Bash
      1. Getting ready
      2. How to do it…
      3. How it works…
    8. Creating a simple NAT and DMZ firewall
      1. Getting ready
      2. How to do it…
      3. How it works…
    9. Parsing a GitHub project and generate a report
      1. Getting ready
      2. How to do it…
      3. How it works…
    10. Creating a poor man's incremental remote backup
      1. Getting ready
      2. How to do it…
      3. How it works…
    11. Using Bash scripts to monitor udev input
      1. Getting ready
      2. How to do it…
      3. How it works…
    12. Using Bash to monitor battery life and optimize it
      1. Getting ready
      2. How to do it…
      3. How it works…
    13. Using chroot and restricted Bash shells to secure scripts
      1. Getting ready
      2. How to do it…
      3. How it works…
  13. Advanced Scripting Techniques
    1. Introduction
    2. Calculating and reducing the runtime of a script
      1. Getting ready
      2. How to do it...
      3. How it works...
    3. Writing one-line conditional statements and loops
      1. Getting ready
      2. How to do it...
      3. How it works...
    4. Avoiding command not found warnings/errors and improving portability
      1. Getting ready
      2. How to do it...
      3. How it works...
    5. Creating a config file and using it in tandem with your scripts
      1. Getting ready
      2. How to do it...
      3. How it works...
    6. Improving your shell – GCC and command line colors
      1. Getting ready
      2. How to do it...
      3. How it works...
    7. Adding aliases, and altering user paths/variables
      1. Getting ready
      2. How to do it...
      3. How it works...
    8. Echoing output to raw terminal devices
      1. Getting ready
      2. How to do it...
      3. How it works...
    9. Creating simple frontend GUIs for Bash scripts
      1. Getting ready
      2. How to do it...
      3. How it works...
    10. Compiling and installing your own Bash shell
      1. Getting ready
      2. How to do it...
      3. How it works...
    11. Recording terminal sessions for automation
      1. Getting ready
      2. How to do it...
      3. How it works...
    12. Writing high-quality scripts by example
      1. Getting ready
      2. How to do it...
      3. How it works...
  14. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think