Linux Command Line and Shell Scripting Techniques

Book description

Practical and actionable recipes for using shell and command-line scripting on your Linux OS with confidence

Key Features

  • Learn how to use the command line and write and debug Linux Shell scripts
  • Automate complex repetitive tasks and backups, and learn networking and security
  • A practical approach to system administration, and virtual machine and software management

Book Description

Linux Command Line and Shell Scripting Techniques begins by taking you through the basics of the shell and command-line utilities. You'll start by exploring shell commands for file, directory, service, package, and process management. Next, you'll learn about networking - network, firewall and DNS client configuration, ssh, scp, rsync, and vsftpd, as well as some network troubleshooting tools.

You'll also focus on using the command line to find and manipulate text content, via commands such as cut, egrep, and sed. As you progress, you'll learn how to use shell scripting. You'll understand the basics - input and output, along with various programming concepts such as loops, variables, arguments, functions, and arrays. Later, you'll learn about shell script interaction and troubleshooting, before covering a wide range of examples of complete shell scripts, varying from network and firewall configuration, through to backup and concepts for creating live environments. This includes examples of performing scripted virtual machine installation and administration, LAMP (Linux, Apache, MySQL, PHP) stack provisioning and bulk user creation for testing environments.

By the end of this Linux book, you'll have gained the knowledge and confidence you need to use shell and command-line scripts.

What you will learn

  • Get an introduction to the command line, text editors, and shell scripting
  • Focus on regular expressions, file handling, and automating complex tasks
  • Automate common administrative tasks
  • Become well-versed with networking and system security scripting
  • Get to grips with repository management and network-based file synchronization
  • Use loops, arguments, functions, and arrays for task automation

Who this book is for

This book is for anyone looking to learn about Linux administration via CLI and scripting. Those with no Linux command-line interface (CLI) experience will benefit from it by learning from scratch. More experienced Linux administrators or engineers will also find this book useful, as it will help them organize their knowledge, fill in any gaps, and work efficiently with shell scripts to increase productivity.

Table of contents

  1. Linux Command Line and Shell Scripting Techniques
  2. Contributors
  3. About the authors
  4. About the reviewers
  5. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
    4. Download the color images
    5. Conventions used
    6. Get in touch
    7. Share Your Thoughts
  6. Chapter 1: Basics of Shell and Text Terminal
    1. Technical requirements
    2. Accessing the shell
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. See also
    3. Setting up the user shell
      1. Getting ready
      2. How to do it…
      3. How it works…
    4. Setting up the Bash shell
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
    5. Using the most common shell commands
      1. Getting ready
      2. How to do it…
      3. How it works...
    6. Using screen
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
  7. Chapter 2: Using Text Editors
    1. Technical requirements
    2. Learning the basics of the Vi(m) Editor
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. See also
    3. Learning the basics of the nano editor
      1. Getting ready
      2. How to do it…
      3. There's more…
    4. Going through the advanced Vi(m) settings
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
  8. Chapter 3: Using Commands and Services for Process Management
    1. Technical requirements
    2. Process management tools
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. See also
    3. Managing background jobs
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
    4. Managing process priorities
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
    5. Configuring crond
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
  9. Chapter 4: Using Shell to Configure and Troubleshoot a Network
    1. Technical requirements
    2. Using nmcli and netplan
      1. Getting ready
      2. How to do it
      3. How it works
      4. There's more
    3. Using firewall-cmd and ufw
      1. Getting ready
      2. How to do it
      3. How it works
      4. There's more
    4. Working with open ports and connections
      1. Getting ready
      2. How to do it
      3. How it works
      4. There's more
    5. Configuring /etc/hosts and DNS resolving
      1. Getting ready
      2. How to do it
      3. How it works
      4. There's more
    6. Using network diagnostic tools
      1. Getting ready
      2. How to do it
      3. How it works
      4. There's more
  10. Chapter 5: Using Commands for File, Directory, and Service Management
    1. Technical requirements
    2. Basic file and directory-based commands
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. See also
    3. Additional commands for manipulating file/directory security aspects
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. See also
    4. Manipulating text files by using commands
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
    5. Finding files and folders
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
    6. Archiving and compressing files and folders
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
    7. Managing services and targets
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
  11. Chapter 6: Shell-Based Software Management
    1. Technical requirements
    2. Using dnf and apt for package management
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
    3. Using additional repositories, streams, and profiles
      1. Getting ready
      2. How to do it…
      3. How it works…
    4. Creating custom repositories
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
    5. Compiling third-party software
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
  12. Chapter 7: Network-Based File Synchronization
    1. Technical requirements
    2. Learning how to use SSH and SCP
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
    3. Learning how to use rsync
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
    4. Using vsftpd
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more
  13. Chapter 8: Using the Command Line to Find, Extract, and Manipulate Text Content
    1. Technical requirements
    2. Using text commands to merge file content
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
    3. Converting DOS text to Linux text and vice versa
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
    4. Using cut
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
    5. Using egrep
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
    6. Using sed
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
  14. Chapter 9: An Introduction to Shell Scripting
    1. Technical requirements
    2. Writing your first Bash shell script
      1. Getting ready
      2. How to do it…
      3. How it works…
    3. Serializing basic commands – from simple to complex
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
    4. Manipulating shell script input, output, and errors
      1. Getting ready
      2. How to do it…
      3. How it works…
    5. Shell script hygiene
      1. Getting ready
      2. How to do it…
      3. There's more…
  15. Chapter 10: Using Loops
    1. The for loop
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. See also
    2. break and continue
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. See also
    3. The while loop
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. See also
    4. The test-if loop
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. See also
    5. The case loop
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. See also
    6. Logical looping with and, or, and not
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. See also
  16. Chapter 11: Working with Variables
    1. Technical requirements
    2. Using shell variables
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. See also
    3. Using variables in shell scripting
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. See also
    4. Quoting in the shell
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. See also
    5. Performing operations on variables
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. See also
    6. Variables via external commands
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. See also
  17. Chapter 12: Using Arguments and Functions
    1. Technical requirements
    2. Using custom functions in shell script code
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. See also
    3. Passing arguments to a function
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. See also
    4. Local and global variables
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. See also
    5. Working with returns from a function
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. See also
    6. Loading an external function to a shell script
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. See also
    7. Implementing commonly used procedures via functions
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. See also
  18. Chapter 13: Using Arrays
    1. Technical requirements
    2. Basic array manipulation
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. See also
    3. Advanced array manipulation
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. See also
  19. Chapter 14: Interacting with Shell Scripts
    1. Technical requirements
    2. Creating text-based interactive scripts
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. See also
    3. Using expect to automate repetitive tasks based on text output
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. See also
    4. Using dialog for menu-driven interactive scripts
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. See also
  20. Chapter 15: Troubleshooting Shell Scripts
    1. Technical requirements
    2. Common scripting mistakes
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. See also…
    3. Simple debugging approach – echoing values during script execution
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. See also
    4. Using the bash -x and -v options
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. See also…
    5. Using set to debug a part of the script
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. See also…
  21. Chapter 16: Shell Script Examples for Server Management, Network Configuration, and Backups
    1. Technical requirements
    2. Creating a file and folder inventory
      1. How to do it…
      2. See also
    3. Checking if you're running as root
      1. Getting ready
      2. How to do it…
      3. See also
    4. Displaying server stats
      1. Getting ready
      2. How to do it…
      3. There's more…
    5. Finding files by name, ownership, or content type and copying them to a specified directory
      1. Getting ready
      2. How to do it…
      3. There's more…
    6. Parsing date and time data
      1. Getting ready
      2. How to do it…
    7. Configuring the most common firewall settings interactively
      1. Getting ready
      2. How to do it…
      3. There's more…
    8. Configuring network settings interactively
      1. Getting ready
      2. How to do it…
      3. There's more…
    9. Backing up the current directory with shell script arguments and variables
      1. Getting ready
      2. How to do it…
      3. There's more…
    10. Creating a current backup based on the user input for the backup source and destination
      1. Getting ready
      2. How to do it…
      3. There's more…
  22. Chapter 17: Advanced Shell Script Examples
    1. Technical requirements
    2. Implementing a web server service and security settings
      1. Getting ready
      2. How to do it…
      3. See also
    3. Creating users and groups and forcing users to change them on the next login
      1. Getting ready
      2. How to do it…
      3. See also
    4. Creating users and groups from a standardized input file and a random password for each user
      1. Getting ready
      2. How to do it…
      3. See also
    5. Scripted VM installation on KVM
      1. Getting ready
      2. How to do it…
      3. See also
    6. Using a shell script to provision SSH keys
      1. Getting ready
      2. How to do it…
      3. See also
    7. A shell script for VM administration
      1. Getting ready
      2. How to do it…
      3. See also
    8. Why subscribe?
  23. Other Books You May Enjoy
    1. Packt is searching for authors like you
    2. Share Your Thoughts

Product information

  • Title: Linux Command Line and Shell Scripting Techniques
  • Author(s): Vedran Dakic, Jasmin Redzepagic
  • Release date: March 2022
  • Publisher(s): Packt Publishing
  • ISBN: 9781800205192