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.
Table of contents
- Title Page
- Copyright and Credits
- Packt Upsell
- Contributors
- Preface
-
Crash Course in Bash
- Getting started with Bash and CLI fundamentals
- Creating and using basic variables
- Hidden Bash variables and reserved words
- Conditional logic using if, else, and elseif
- Case/switch statements and loop constructs
- Using functions and parameters
- Including source files
- Retrieving return codes and output
- Linking commands, pipes, and input/output
- Getting program input parameters 
- Getting additional information about commands
- Summary
-
Acting Like a Typewriter and File Explorer
- Introduction
- Basic searching for strings and files
- Using wildcards and regexes
- Math and calculations in script
- Striping/altering/sorting/deleting/searching strings with Bash only
- Using SED and AWK to remove/replace substrings
- Formatting your data/output using echo and printf
- Readying your script for different languages with internationalization
- Calculating statistics and reducing duplicates based on file contents
- Using file attributes with conditional logic
- Reading delimited data and altered output format
-
Understanding and Gaining File System Mastery
- Introduction
- Viewing files from various angles – head, tail, less, and more
- Searching for files by name and/or extension
- Creating a diff of two files and patching
- Creating symbolic links and using them effectively
- Crawling filesystem directories and printing a tree
- Finding and deleting duplicate files or directories
- Joining and splitting files at arbitrary positions
- Generating datasets and random files of various size
-
Making a Script Behave Like a Daemon
- Introduction
- Running a program continuously (forever) using looping constructs or recursion
- Keeping programs/scripts running after logoff
- Invoking commands when they require permissions
- Sanitizing user input and for repeatable results
- Making a simple multi-level user menu using select
- Generating and trapping signals for cleanup
- Using temporary files and lock files in your program
- Leveraging timeout when waiting for command completion
- Creating a file-in-file-out program and running processes in parallel
- Executing your script on startup
-
Scripts for System Administration Tasks
- Introduction
- Gathering and aggregating system information
- Gathering network information and connectivity diagnostics
- Configuring basic network connectivity
- Monitoring directories and files
- Compressing and archiving files
- Rotating files from RAM to storage for log rotation
- Using Linux iptables for a firewall
- Accessing SQL databases remotely or locally
- Creating SSH keys for password less remote access
- Creating and configuring cron Jobs for task scheduling
- Creating users and groups systematically
-
Scripts for Power Users
- Introduction
- Creating Syslog entries and generating an alarm  
- Backing up and erasing media, disks, and partitions with DD
- Creating graphics and presentations on the CLI
- Checking for file integrity and tampering
- Mounting network file systems and retrieving files
- Browsing the web from the CLI
- Capturing network traffic headlessly
- Finding binary dependencies
- Fetching time from different locations
- Encrypting/decrypting files from a script
-
Writing Bash to Win and Profit
- Introduction
- Creating a lame utility HTTP server
- Parsing RSS feeds and output HTML
- Scraping the web and collecting files
- Making a simple IRC chat bot logger
- Blocking IP addresses from failed SSH attempts
- Playing and managing audio from Bash
- Creating a simple NAT and DMZ firewall
- Parsing a GitHub project and generate a report
- Creating a poor man's incremental remote backup
- Using Bash scripts to monitor udev input
- Using Bash to monitor battery life and optimize it
- Using chroot and restricted Bash shells to secure scripts
-
Advanced Scripting Techniques
- Introduction
- Calculating and reducing the runtime of a script
- Writing one-line conditional statements and loops
- Avoiding command not found warnings/errors and improving portability
- Creating a config file and using it in tandem with your scripts
- Improving your shell – GCC and command line colors
- Adding aliases, and altering user paths/variables
- Echoing output to raw terminal devices
- Creating simple frontend GUIs for Bash scripts
- Compiling and installing your own Bash shell
- Recording terminal sessions for automation
- Writing high-quality scripts by example
- Other Books You May Enjoy
Product information
- Title: Bash Cookbook
- Author(s):
- Release date: July 2018
- Publisher(s): Packt Publishing
- ISBN: 9781788629362
You might also like
book
bash Cookbook
The key to mastering any Unix system, especially Linux and Mac OS X, is a thorough …
book
bash Cookbook, 2nd Edition
For system administrators, programmers, and end users, shell command or carefully crafted shell script can save …
video
Bash Scripting
The second edition of this video title is available. Please go to Bash Shell Scripting, 2nd …
book
Linux Shell Scripting Cookbook - Third Edition
Do amazing things with the shell About This Book Become an expert in creating powerful shell …