Learn Linux Shell Scripting - Fundamentals of Bash 4.4

Book description

Create and maintain powerful Bash scripts for automation and administration.

Key Features

  • Get up and running with Linux shell scripting using real-world examples
  • Leverage command-line techniques and methodologies to automate common yet complex administration tasks
  • A practical guide with exposure to scripting constructs and common scripting patterns

Book Description

Shell scripts allow us to program commands in chains and have the system execute them as a scripted event, just like batch files. This book will start with an overview of Linux and Bash shell scripting, and then quickly deep dive into helping you set up your local environment, before introducing you to tools that are used to write shell scripts. The next set of chapters will focus on helping you understand Linux under the hood and what Bash provides the user. Soon, you will have embarked on your journey along the command line. You will now begin writing actual scripts instead of commands, and will be introduced to practical applications for scripts. The final set of chapters will deep dive into the more advanced topics in shell scripting. These advanced topics will take you from simple scripts to reusable, valuable programs that exist in the real world. The final chapter will leave you with some handy tips and tricks and, as regards the most frequently used commands, a cheat sheet containing the most interesting flags and options will also be provided.

After completing this book, you should feel confident about starting your own shell scripting projects, no matter how simple or complex the task previously seemed. We aim to teach you how to script and what to consider, to complement the clear-cut patterns that you can use in your daily scripting challenges.

What you will learn

  • Understand Linux and Bash basics as well as shell scripting fundamentals
  • Learn to write simple shell scripts that interact with Linux operating system
  • Build, maintain, and deploy scripts in a Linux environment
  • Learn best practices for writing shell scripts
  • Avoid common pitfalls associated with Bash scripting
  • Gain experience and the right toolset to write your own complex shell scripts

Who this book is for

This book targets new and existing Linux system administrators, Windows system administrators or developers who are interested in automating administrative tasks. No prior shell scripting experience is needed but in case you do this book will make a pro quickly. Readers should have a basic understanding of the command line.

Table of contents

  1. Title Page
  2. Copyright and Credits
    1. Learn Linux Shell Scripting – Fundamentals of Bash 4.4
  3. About Packt
    1. Why subscribe?
    2. PacktPub.com
  4. Contributors
    1. About the author
    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
    5. Disclaimer
  6. Introduction
    1. What is Linux?
    2. What is Bash?
    3. Summary
  7. Setting Up Your Local Environment
    1. Technical requirements
    2. Choosing between a virtual machine and a physical installation
    3. Setting up VirtualBox
    4. Creating an Ubuntu virtual machine
      1. Creating the virtual machine in VirtualBox
      2. Installing Ubuntu on the virtual machine
      3. Accessing the virtual machine via SSH
    5. Summary
    6. Questions
    7. Further reading
  8. Choosing the Right Tools
    1. Technical requirements
    2. Using graphical editors for shell scripting
      1. Atom
        1. Atom installation and configuration
      2. Notepad++
    3. Using command-line editors
      1. Vim
        1. Vim summary
        2. .vimrc
        3. Vim cheat sheet
      2. nano
    4. Combining graphical editors with command-line editors when writing shell scripts
    5. Summary
    6. Questions
    7. Further reading
  9. The Linux Filesystem
    1. Technical requirements
    2. The Linux filesystem explained
      1. What is a filesystem?
      2. What makes the Linux filesystem unique?
    3. Structure of the Linux filesystem
      1. Tree structure
      2. Overview of top-level directories
        1. What about multiple partitions?
        2. /bin/, /sbin/, and /usr/
        3. /etc/
        4. /opt/, /tmp/, and /var/
    4. Everything is a file
      1. Different types of files
    5. Summary
    6. Questions
    7. Further reading
  10. Understanding the Linux Permissions Scheme
    1. Technical requirements
    2. Read, write, and execute
      1. RWX
    3. Users, groups, and others
    4. Manipulating file permissions and ownership
      1. chmod, umask
      2. sudo, chown, and chgrp
        1. sudo
        2. chown, chgrp
    5. Working with multiple users
    6. Advanced permissions
      1. File attributes
      2. Special file permissions
      3. Access Control Lists (ACLs)
    7. Summary
    8. Questions
    9. Further reading
  11. File Manipulation
    1. Technical requirements
    2. Common file operations
      1. Copying
      2. Removing
      3. Renaming, moving, and linking
    3. Archiving
    4. Finding files
      1. locate
      2. find
    5. Summary
    6. Questions
    7. Further reading
  12. Hello World!
    1. Technical requirements
    2. First steps
      1. The shebang
      2. Running scripts
    3. Readability
      1. Comments
        1. Script header
      2. Verbosity
        1. Verbosity in comments
        2. Verbosity of commands
        3. Verbosity of command output
    4. Keep It Simple, Stupid (KISS)
    5. Summary
    6. Questions
    7. Further reading
  13. Variables and User Input
    1. Technical requirements
    2. What is a variable?
      1. Why do we need variables?
      2. Variables or constants?
    3. Variable naming
    4. Dealing with user input
      1. Basic input
      2. Parameters and arguments
    5. Interactive versus non-interactive scripts
      1. Combining positional arguments and read
    6. Summary
    7. Questions
    8. Further reading
  14. Error Checking and Handling
    1. Technical requirements
    2. Error checking
      1. Exit status
      2. Functional checks
      3. Test shorthand
        1. Variable refresher
        2. Bash debugging
    3. Error handling
      1. if-then-exit
      2. if-then-else
      3. Shorthand syntax
    4. Error prevention
      1. Checking arguments
      2. Managing absolute and relative paths
      3. Dealing with y/n
    5. Summary
    6. Questions
    7. Further reading
  15. Regular Expressions
    1. Technical requirements
    2. Introducing regular expressions
      1. What is a regular expression?
        1. grep
        2. Greediness
        3. Character matching
        4. Line anchors
        5. Character classes
    3. Globbing
      1. What is globbing?
      2. Similarities with regular expressions
      3. More globbing
      4. Advanced globbing
      5. Disabling globbing, and other options
    4. Using regular expressions with egrep and sed
      1. Advanced grep
      2. Introducing egrep
      3. sed, the stream editor
        1. Stream editing
        2. In-place editing
        3. Line manipulation
    5. Final remarks
    6. Summary
    7. Questions
    8. Further reading
  16. Conditional Testing and Scripting Loops
    1. Technical requirements
    2. Advanced if-then-else
      1. A recap on if-then-else 
      2. Using regular expressions in tests
      3. The elif condition
      4. Nesting
      5. Getting help
    3. The while loop
      1. The until loop
      2. Creating an interactive while loop
    4. The for loop
      1. Globbing and the for loop
    5. Loop control
      1. Breaking the loop
      2. The continue keyword
      3. Loop control and nesting
    6. Summary
    7. Questions
    8. Further reading
  17. Using Pipes and Redirection in Scripts
    1. Technical requirements
    2. Input/output redirection
      1. File descriptors
      2. Redirecting output
        1. stdout
        2. stderr
        3. Redirect all output
        4. Special output redirection
          1. /dev/null
          2. /dev/zero
      3. Input redirection
        1. Generating a password
      4. Advanced redirecting
        1. Redirecting redirections
        2. Command substitution
        3. Process substitution
    3. Pipes
      1. Binding stdout to stdin
      2. Practical examples
        1. Yet another password generator
        2. Setting passwords in a script
        3. tee
    4. Here documents
      1. Heredocs and variables
      2. Using heredocs for script input
      3. Here strings
    5. Summary
    6. Questions
    7. Further reading
  18. Functions
    1. Technical requirements
    2. Functions explained
      1. Hello world!
      2. More complexity
      3. Variable scopes
      4. Practical examples
        1. Error handling
    3. Augmenting functions with parameters
      1. Colorful
      2. Returning values
    4. Function libraries
      1. Source
      2. More practical examples
        1. Current working directory
        2. Type checking
        3. Yes-no check
    5. Summary
    6. Questions
    7. Further reading
  19. Scheduling and Logging
    1. Technical requirements
    2. Scheduling with at and cron
      1. at
        1. Time syntax
        2. The at queue
        3. at output
      2. cron
        1. crontab
          1. Syntax for the crontab
    3. Logging script results
      1. Crontab environment variables
        1. PATH
        2. SHELL
        3. MAILTO
      2. Logging with redirection
      3. Final logging considerations
        1. A note on verbosity
    4. Summary
    5. Questions
    6. Further reading
  20. Parsing Bash Script Arguments with getopts
    1. Technical requirements
    2. Positional parameters versus flags
      1. Using flags on the command line
    3. The getopts shell builtin
      1. The getopts syntax
      2. Multiple flags
      3. Flags with arguments
      4. Combining flags with positional arguments
    4. Summary
    5. Questions
    6. Further reading
  21. Bash Parameter Substitution and Expansion
    1. Technical requirements
    2. Parameter expansion
      1. Parameter substitutions – recap
      2. Default values
        1. Input checking
      3. Parameter length
    3. Variable manipulation
      1. Pattern substitution
      2. Pattern removal
      3. Case modification
      4. Substring expansion
    4. Summary
    5. Questions
    6. Further reading
  22. Tips and Tricks with Cheat Sheet
    1. Technical requirements
    2. General tips and tricks
      1. Arrays
      2. The history command
      3. Creating your own aliases
    3. Command-line shortcuts
      1. Fun with exclamation marks
        1. Running commands from the history
      2. Keyboard shortcuts
        1. Copying and pasting from the terminal
        2. Reverse search
    4. Cheat sheet for interactive commands
      1. Navigation
        1. cd
        2. ls
        3. pwd
      2. File manipulation
        1. cat
        2. less
        3. touch
        4. mkdir
        5. cp
        6. rm
        7. mv
        8. ln
        9. head
        10. tail
      3. Permissions and ownership
        1. chmod
        2. umask
        3. chown
        4. chgrp
        5. sudo
        6. su
        7. useradd
        8. groupadd
        9. usermod
    5. Summary
    6. Final words
  23. Assessments
    1. Chapter 2
    2. Chapter 3
    3. Chapter 4
    4. Chapter 5
    5. Chapter 6
    6. Chapter 7
    7. Chapter 8
    8. Chapter 9
    9. Chapter 10
    10. Chapter 11
    11. Chapter 12
    12. Chapter 13
    13. Chapter 14
    14. Chapter 15
    15. Chapter 16
  24. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think

Product information

  • Title: Learn Linux Shell Scripting - Fundamentals of Bash 4.4
  • Author(s): Sebastiaan Tammer
  • Release date: December 2018
  • Publisher(s): Packt Publishing
  • ISBN: 9781788995597