
Bash Cookbook
Leverage Bash scripting to automate daily tasks and improve productivity
- 264 pages
- English
- ePUB (mobile friendly)
- Available on iOS & Android
Bash Cookbook
Leverage Bash scripting to automate daily tasks and improve productivity
About this book
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.
Tools to learn more effectively

Saving Books

Keyword Search

Annotating Text

Listen to it instead
Information
Acting Like a Typewriter and File Explorer
- 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
Introduction
- Removing trailing characters
- Replacing sections of words (substrings)
- Searching for strings in files
- Finding files
- Testing file types (directory, file, empty, and so on)
- Performing small calculations
- Limiting the scope of searches or data (filtering)
- Modifying the contents of variables (strings inside of string variables)
# e.g. File system
# / (start here)
# /home (oh we found home)
# /home/user (neat there is a directory inside it called user)
# /home/user/.. (even better, user has files - lets look in them too)
# /etc/ # We are done with /home and its "children" so lets look in /etc
# ... # Until we are done
Basic searching for strings and files
ASCII and UTF are not the only types your target data might be in. In various types of files, you may encounter different types of encoding of data. This is a different problem that's specific to your data and will need additional considerations.
Getting ready
$ ~/
$ wget --recursive --no-parent https://www.packtpub.com www.packtpub.com # Takes awhile
$ traceroute packtpub.com > traceroute.txt
$ mkdir -p www.packtpub.com/filedir www.packtpub.com/emptydir
$ touch www.packtpub.com/filedir/empty.txt
$ touch www.packtpub.com/findme.xml; echo "<xml>" www.packtpub.com/findme.xml
How to do it...
- Next, open up a terminal and create the following script:
#!/bin/bash
# Let's find all the files with the string "Packt"
DIRECTORY="www.packtpub.com/"
SEARCH_TERM="Packt"
# Can we use grep?
grep "${SEARCH_TERM}" ~/* > result1.txt 2&> /dev/null
# Recursive check
grep -r "${SEARCH_TERM}" "${DIRECTORY}" > result2.txt
# What if we want to check for multiple terms?
grep -r -e "${SEARCH_TERM}" -e "Publishing" "${DIRECTORY}" > result3.txt
# What about find?
find "${DIRECTORY}" -type f -print | xargs grep "${SEARCH_TERM}" > result4.txt
# What about find and looking for the string inside of a specific type of content?
find "${DIRECTORY}" -type f -name "*.xml" ! -name "*.css" -print | xargs grep "${SEARCH_TERM}" > result5.txt
# Can this also be achieved with wildcards and subshell?
grep "${SEARCH_TERM}" $(ls -R "${DIRECTORY}"*.{html,txt}) > result6.txt
RES=$?
if [ ${RES} -eq 0 ]; then
echo "We found results!"
else
echo "It broke - it shouldn't happen (Packt is everywhere)!"
fi
# Or for bonus points - a personal favorite
history | grep "ls" # This is really handy to find commands you ran yesterday!
# Aaaannnd the lesson is:
echo "We can do a lot with grep!"
exit 0
- If you remain in your home directory (~/) and run the script, the output should be similar to the following:
$ bash search.sh; ls -lah result*.txt
We found results!
We can do a lot with grep!
-rw-rw-r-- 1 rbrash rbrash 0 Nov 14 14:33 result1.txt
-rw-rw-r-- 1 rbrash rbrash 1.2M Nov 14 14:33 result2.txt
-rw-rw-r-- 1 rbrash rbrash 1.2M Nov 14 14:33 result3.txt
-rw-rw-r-- 1 rbrash rbrash 1.2M Nov 14 14:33 result4.txt
-rw-rw-r-- 1 rbrash rbrash 33 Nov 14 14:33 result5.txt
-rw-rw-r-- 1 rbrash rbrash 14K Nov 14 14:33 result6.txt
How it works...
Table of contents
- Title Page
- Copyright and Credits
- Packt Upsell
- Contributors
- Preface
- Crash Course in Bash
- Acting Like a Typewriter and File Explorer
- Understanding and Gaining File System Mastery
- Making a Script Behave Like a Daemon
- Scripts for System Administration Tasks
- Scripts for Power Users
- Writing Bash to Win and Profit
- Advanced Scripting Techniques
- Other Books You May Enjoy
Frequently asked questions
- Essential is ideal for learners and professionals who enjoy exploring a wide range of subjects. Access the Essential Library with 800,000+ trusted titles and best-sellers across business, personal growth, and the humanities. Includes unlimited reading time and Standard Read Aloud voice.
- Complete: Perfect for advanced learners and researchers needing full, unrestricted access. Unlock 1.4M+ books across hundreds of subjects, including academic and specialized titles. The Complete Plan also includes advanced features like Premium Read Aloud and Research Assistant.
Please note we cannot support devices running on iOS 13 and Android 7 or earlier. Learn more about using the app