You are previewing Mastering Linux Shell Scripting.
O'Reilly logo
Mastering Linux Shell Scripting

Book Description

Master the complexities of Bash shell scripting and unlock the power of shell for your enterprise

About This Book

  • Identify the high level steps such as verifying user input, using command lines and conditional statements in creating and executing simple shell scripts

  • Create and edit dynamic shell scripts to manage complex and repetitive tasks

  • Learn about scripting in Perl and programming in Python as a BASH scripting alternative with this practical, step-by-step guide

  • Who This Book Is For

    Mastering Linux Shell Scripting has been written for Linux administrators who want to automate tasks in their daily lives, saving time and effort. You’ll need to have command-line experience and be familiar with the tasks that you need to automate.

    What You Will Learn

  • Use the type command to identify the order of command evaluation

  • Create interactive scripts that prompt for user input

  • Foster menu structures for operators with little command-line experience

  • Develop scripts that dynamically edit web configuration files to produce a new virtual host

  • Write scripts that use AWK to search and reports on log files

  • Draft effective scripts using functions as building blocks, reducing maintenance and build time

  • Make informed choices by comparing different script languages such as Perl and Python with BASH

  • In Detail

    Shell scripting is a quick method to prototype a complex application or a problem by automating tasks when working on Linux-based systems. Using both simple one-line commands and command sequences complex problems can be solved with ease, from text processing to backing up sysadmin tools.

    In this book, you’ll discover everything you need to know to master shell scripting and make informed choices about the elements you employ. Get to grips with the fundamentals of creating and running a script in normal mode, and in debug mode. Learn about various conditional statements' code snippets, and realize the power of repetition and loops in your shell script. Implement functions and edit files using the Stream Editor, script in Perl, program in Python – as well as complete coverage of other scripting languages to ensure you can choose the best tool for your project.

    Style and approach

    The book will capture your attention and keep you engaged with the simplicity and clarity of each explanation. Every step is accompanied with screen captures so you can cross-check the results before moving on.

    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 If you purchased this book elsewhere, you can visit and register to have the code file.

    Table of Contents

    1. Mastering Linux Shell Scripting
      1. Table of Contents
      2. Mastering Linux Shell Scripting
      3. Credits
      4. About the Author
      5. About the Reviewer
        1. Support files, eBooks, discount offers, and more
          1. Why subscribe?
          2. Free access for Packt account holders
      7. Preface
        1. What this book covers
        2. What you need for this book
        3. Who this book is for
        4. Conventions
        5. Reader feedback
        6. Customer support
          1. Downloading the example code
          2. Downloading the color images of this book
          3. Errata
          4. Piracy
          5. Questions
      8. 1. What and Why of Scripting with Bash
        1. Bash vulnerabilities
        2. The bash command hierarchy
          1. Command type
          2. Command PATH
        3. Preparing text editors for scripting
          1. Configuring vim
          2. Configuring nano
          3. Configuring gedit
        4. Creating and executing scripts
          1. Hello World!
          2. Executing the script
          3. Checking the exit status
          4. Ensuring a unique name
          5. Hello Dolly!
            1. Running the script with arguments
            2. The importance of correct quotes
          6. Printing the script name
        5. Debugging your scripts
        6. Summary
      9. 2. Creating Interactive Scripts
        1. Using echo with options
        2. Basic script using read
        3. Script comments
        4. Enhancing scripts with read prompts
        5. Limiting the number of entered characters
        6. Controlling the visibility of entered text
        7. Enhancing learning with simple scripts
          1. Backing-up with scripts
          2. Connecting to a server
          3. Version 1 – ping
          4. Version 2 – SSH
          5. Version 3 – MySQL/MariaDB
        8. Summary
      10. 3. Conditions Attached
        1. Simple decision paths using command-line lists
        2. Verifying user input with lists
        3. Using the test shell builtin
          1. Testing strings
          2. Testing integers
          3. Testing file types
        4. Creating conditional statements using if
        5. Extending if with else
        6. More conditions with elif
          1. Creating the using elif
        7. Using case statements
        8. Script – building a front-end with grep
        9. Summary
      11. 4. Creating Code Snippets
        1. Abbreviations
        2. Using code snippets
          1. Bringing color to the terminal
        3. Summary
      12. 5. Alternative Syntax
        1. Recapping test
          1. Testing files
          2. Adding logic
          3. Square brackets as not seen before
        2. Providing parameter defaults
          1. Variables
          2. Special parameters
          3. Setting defaults
        3. When in doubt – Quote!
        4. Advanced test using [[
          1. Whitespace
          2. Other advanced features
            1. Pattern matching
            2. Regular expressions
            3. Regular expression script
        5. Arithmetic operations using ((
          1. Simple math
          2. Parameter manipulation
          3. Standard arithmetic tests
        6. Summary
      13. 6. Iterating with Loops
        1. For loops
        2. Controlling the loop
          1. While loops and until loops
        3. Reading input from files
        4. Creating operator menus
        5. Summary
      14. 7. Creating Building Blocks with Functions
        1. Introducing functions
        2. Passing parameters to functions
        3. Returning values from functions
        4. Using functions in menus
        5. Summary
      15. 8. Introducing sed
        1. Using grep to display text
          1. Displaying received data on an interface
          2. Displaying user account data
          3. Listing the number of CPUs in a system
          4. Parsing CSV files
            1. The CSV file
            2. Isolating catalog entries
        2. Using regular expressions
          1. Working with alternate spellings
          2. How many words have four consecutive vowels?
          3. RE anchors
        3. Understanding the basics of sed
          1. Substituting command
          2. Editing the file
        4. Summary
      16. 9. Automating Apache Virtual Hosts
        1. Apache name-based Virtual Hosts
          1. Creating the Virtual Hosts template
          2. First steps
          3. Isolating lines
          4. sed script files
        2. Automating Virtual Host creation
          1. Prompting for data during site creation
        3. Summary
      17. 10. Awk Fundamentals
        1. The history behind awk
        2. Displaying and filtering content from files
        3. Formatting output
        4. Further filtering to display users by UID
        5. Awk control files
        6. Summary
      18. 11. Summarizing Logs with Awk
        1. The HTTPD log file format
        2. Displaying data from web logs
          1. Selecting entries by date
          2. Summarizing 404 errors
          3. Summarizing HTTP access codes
        3. Displaying the highest ranking IP address
        4. Displaying the browser data
        5. Working with e-mail logs
        6. Summary
      19. 12. A Better lastlog with Awk
        1. Using awk ranges to exclude data
          1. The lastlog command
          2. Horizontal filtering rows with awk
          3. Counting matched rows
        2. Conditions based on the number of fields
        3. Manipulating the awk record separator to report on XML data
          1. Apache Virtual Hosts
          2. XML catalog
        4. Summary
      20. 13. Using Perl as a Bash Scripting Alternative
        1. What is Perl?
        2. Hello World
        3. Perl arrays
          1. Program name?
          2. Argument arrays
          3. Counting elements in an array
          4. Looping through an array
          5. Creating arrays
        4. Conditional statements in Perl
          1. Replacing command line lists
          2. If and unless
        5. Using functions within Perl
          1. Prompt for user input
          2. Creating the function
        6. Summary
      21. 14. Using Python as a Bash Scripting Alternative
        1. What is Python?
        2. Saying Hello World the Python way
        3. Pythonic arguments
          1. Supplying arguments
          2. Counting arguments
        4. Significant whitespace
        5. Reading user input
          1. Using Python to write to files
        6. Summary
      22. Index