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

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 high-level steps such as verifying user input
  • Using the command line and conditional statements in creating/executing simple shell scripts
  • Create and edit dynamic shell scripts to manage complex and repetitive tasks
  • Leverage the command-line to bypass GUI and automate common tasks
Who This Book Is For

If you are a Linux administrator or a system administrator and are interested in automating tasks in your daily lives, saving time and effort, this book is for you. Basic shell scripting and command-line experience will be required. Familiarity with the tasks you need to automate will be helpful.

What You Will Learn
  • Make, execute, and debug your first Bash script
  • 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 Python with BASH
In Detail

In this book, you'll discover everything you need to know to master shell scripting and make informed choices about the elements you employ.

Grab your favorite editor and start writing your best Bash scripts step by step. 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. You will also learn to write complex shell scripts. This book will also deep dive into file system administration, directories, and system administration like networking, process management, user authentications, and package installation and regular expressions. Towards the end of the book, you will learn how to use Python as a BASH Scripting alternative.

By the end of this book, you will know shell scripts at the snap of your fingers and will be able to automate and communicate with your system with keyboard expressions.

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 by screenshots 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 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. Mastering Linux Shell Scripting Second Edition
  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. Download the color images
      3. Conventions used
    4. Get in touch
      1. Reviews
  6. The What and Why of Scripting with Bash
    1. Technical requirements
    2. Types of Linux shells
    3. What is bash scripting?
    4. The bash command hierarchy
      1. Command type
      2. Command PATH
    5. Preparing text editors for scripting
      1. Configuring vim
      2. Configuring nano
      3. Configuring gedit
    6. 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
    7. Declaring variables
      1. User-defined variables
      2. Environment variables
    8. Variable scope
    9. Command substitution
    10. Debugging your scripts
    11. Summary
    12. Questions
    13. Further reading
  7. Creating Interactive Scripts
    1. Technical requirements
    2. Using echo with options
    3. Basic script using read
    4. Script comments
    5. Enhancing scripts with read prompts
    6. Limiting the number of entered characters
    7. Controlling the visibility of the entered text
    8. Passing options
      1. Passing parameters with options
      2. Read options values
    9. Try to be standard
    10. 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
      6. Reading files
    11. Summary
    12. Questions
    13. Further reading
  8. Conditions Attached
    1. Technical requirements
    2. Simple decision paths using command-line lists
    3. Verifying user input with lists
    4. Using the test shell built-in
      1. Testing strings
      2. Testing integers
      3. Testing file types
    5. Creating conditional statements using if
    6. Extending if with else
    7. Test command with the if command
      1. Checking strings
      2. Checking files and directories
      3. Checking numbers
      4. Combining tests
    8. More conditions with elif
      1. Creating the backup2.sh using elif
    9. Using case statements
    10. Recipe – building a frontend with grep
    11. Summary
    12. Questions
    13. Further reading
  9. Creating Code Snippets
    1. Technical requirements
    2. Abbreviations
    3. Using code snippets
      1. Bringing color to the Terminal
    4. Creating snippets using VS Code
    5. Summary
    6. Questions
    7. Further reading
  10. Alternative Syntax
    1. Technical requirement
    2. Recapping the test command
      1. Testing files
      2. Adding logic
      3. Square brackets as not seen before
    3. Providing parameter defaults
      1. Variables
      2. Special parameters
      3. Setting defaults
    4. When in doubt – quote!
    5. Advanced tests using [[
      1. White space
      2. Other advanced features
        1. Pattern matching
        2. Regular expressions
        3. Regular expression script
    6. Arithmetic operations using ((
      1. Simple math
      2. Parameter manipulation
      3. Standard arithmetic tests
    7. Summary
    8. Questions
    9. Further reading
  11. Iterating with Loops
    1. Technical requirement
    2. for loops
    3. Advanced for loops
    4. The IFS
    5. Counting directories and files
    6. C-style for loops 
    7. Nested loops
    8. Redirecting loop output
      1. Controlling the loop
    9. while loops and until loops
    10. Reading input from files
    11. Creating operator menus
    12. Summary
    13. Questions
    14. Further reading
  12. Creating Building Blocks with Functions
    1. Technical requirements
    2. Introducing functions
    3. Passing parameters to functions
      1. Passing arrays
    4. Variable scope
    5. Returning values from functions
    6. Recursive functions
    7. Using functions in menus
    8. Summary
    9. Questions
    10. Further reading
  13. Introducing the Stream Editor
    1. Technical requirements
    2. 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
    3. Understanding the basics of sed
      1. The substitute command
      2. Global replacement
      3. Limiting substitution
      4. Editing the file
    4. Other sed commands
      1. The delete command
      2. The insert and append commands
      3. The change command
      4. The transform command
    5. Multiple sed commands
    6. Summary
    7. Questions
    8. Further reading
  14. Automating Apache Virtual Hosts
    1. Technical requirements
    2. Apache name-based Virtual Hosts
      1. Creating the virtual host template
      2. First steps
      3. Isolating lines
      4. sed script files
    3. Automating virtual host creation
      1. Prompting for data during site creation
    4. Summary
    5. Questions
    6. Further reading
  15. AWK Fundamentals
    1. Technical requirements
    2. The history behind AWK
    3. Displaying and filtering content from files
    4. AWK variables
      1. User-defined variables
    5. Conditional statements
      1. The if command
      2. while loops
      3. for loops
    6. Formatting output
    7. Further filtering to display users by UID
    8. AWK control files
      1. Built-in functions
    9. Summary
    10. Questions
    11. Further reading
  16. Regular Expressions
    1. Technical requirements
    2. Regular expression engines
    3. Defining BRE patterns
      1. Anchor characters
      2. The dot character
      3. The character class
      4. Ranges of characters
      5. Special character classes
      6. The asterisk
    4. Defining ERE patterns
      1. The question mark
      2. The plus sign
      3. Curly braces
      4. The pipe character
      5. Expression grouping
    5. Using grep
    6. Summary
    7. Questions
    8. Further reading
  17. Summarizing Logs with AWK
    1. Technical requirements
    2. The HTTPD log file format
    3. Displaying data from web logs
      1. Selecting entries by date
      2. Summarizing 404 errors
      3. Summarizing HTTP access codes
      4. Resources hits
      5. Identify image hotlinking
    4. Displaying the highest ranking IP address
    5. Displaying the browser data
    6. Working with email logs
    7. Summary
    8. Questions
    9. Further reading
  18. A Better lastlog with AWK
    1. Technical requirements
    2. Using AWK ranges to exclude data
      1. The lastlog command
      2. Horizontally filtering rows with AWK
      3. Counting matched rows
    3. Conditions based on the number of fields
    4. Manipulating the AWK record separator to report on XML data
      1. Apache Virtual Hosts
      2. XML catalog
    5. Summary
    6. Questions
    7. Further reading
  19. Using Python as a Bash Scripting Alternative
    1. Technical requirements
    2. What is Python?
    3. Saying Hello World the Python way
    4. Pythonic arguments
    5. Supplying arguments
    6. Counting arguments
    7. Significant whitespace
    8. Reading user input
    9. Using Python to write to files
    10. String manipulation
    11. Summary
    12. Questions
    13. Further reading
  20. Assessments
    1. Chapter 1
    2. Chapter 2
    3. Chapter 3
    4. Chapter 4
    5. Chapter 5
    6. Chapter 6
    7. Chapter 7
    8. Chapter 8
    9. Chapter 9
    10. Chapter 10
    11. Chapter 11
    12. Chapter 12
    13. Chapter 13
    14. Chapter 14
  21. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think