Preface
First, you'll learn about Linux shells and why we chose the bash shell. Then, you'll learn how to write a simple bash script and how to edit your bash script using Linux editors.
Following this, you will learn how to define a variable and the visibility of a variable. After this, you will learn how to store command execution output into a variable, which is called command substitution. Also, you will learn how to debug your code using bash options and Visual Studio Code. You will learn how to make your bash script interactive to the user by accepting input from the user using the read command. Then, you will learn how to read options and its values if the user passed them to the script. Following this, you will learn how to write conditional statements such as if statements and how to use case statements. After this, you will learn how to create code snippets using vim and Visual Studio Code. For repetitive tasks, you will see how to write for loops, how to iterate over simple values, and how to iterate over directory content. Also, you will learn how to write nested loops. Along with this, you will write while and until loops. Then, we will move on to functions, the reusable chunks of code. You will learn how to write functions and how to use them. After this, you will be introduced to one of the best tools in Linux, which is Stream Editor. As we are still talking about text processing, we will introduce AWK, one of the best text processing tools in Linux that you will ever see.
After this, you will learn how to empower your text processing skills by writing better regular expressions. Finally, you will be introduced to Python as an alternative to bash scripting.
Who this book is for
This book targets system administrators and developers who would like to write a better shell script to automate their work. Some programming experience is preferable. If you don't have any background in shell scripting, no problem, the book will discuss everything from the beginning.
What this book covers
Chapter 1, The What and Why of Scripting with Bash, will introduce Linux shells, how to write your first shell script, how to prepare your editor, how to debug your shell script, and some basic bash programming, such as declaring variables, variable scope, and command substitution.
Chapter 2, Creating Interactive Scripts, covers how to read input from the user using read
command, how to pass options to your script, how to control the visibility of the entered text, and how to limit the number of entered characters.
Chapter 3, Conditions Attached, will introduce the if
statement, the case
statement, and other testing command such as else
and elif
.
Chapter 4, Creating Code Snippets, covers creating and using code snippets using editors, such as vim and Visual Studio Code.
Chapter 5, Alternative Syntax, will discuss advanced testing using [[
and how to perform arithmetic operations.
Chapter 6, Iterating with Loops, will teach you how to use for
loops, while
loops, and until
loops to iterate over simple values and complex values.
Chapter 7, Creating Building Blocks with Functions, will introduce functions and explains how to create a function, list builtin functions, pass parameters to functions, and writing recursive functions.
Chapter 8, Introducing the Stream Editor, will introduce the basics of sed tool to manipulate files, such as adding, replacing deleting, and transforming text.
Chapter 9, Automating Apache Virtual Hosts, contains a practical example of sed and explains how to create virtual hosts automatically using sed.
Chapter 10, AWK Fundamentals, will discuss AWK and how to filter file content using it. Also, we will discuss some AWK programming basics.
Chapter 11, Regular Expressions, covers regular expressions, their engines, and how to use them with sed and AWK to empower your script.
Chapter 12, Summarizing Logs with AWK, will show how to process the httpd.conf
Apache log file using AWK and extract useful well-formatted data.
Chapter 13, A Better lastlog with AWK, will show you how to use AWK to output beautiful reports using the lastlog command by filtering and processing the lastlog output.
Chapter 14, Using Python as a Bash Scripting Alternative, will discuss Python programming language basics and explains how to write some Python scripts as a bash script alternative.
To get the most out of this book
I assume that you have a little programming background. Even if you don't have a programming background, the book will start from the beginning.
You should know some Linux basics such as the basic commands such as ls
, cd
, and which
.
Download the example code files
You can download the example code files for this book from your account at www.packtpub.com. If you purchased this book elsewhere, you can visit www.packtpub.com/support and register to have the files emailed directly to you.
You can download the code files by following these steps:
- Log in or register at www.packtpub.com.
- Select the
SUPPORT
tab. - Click on
Code Downloads & Errata
. - Enter the name of the book in the
Search
box and follow the onscreen instructions.
Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:
- WinRAR/7-Zip for Windows
- Zipeg/iZip/UnRarX for Mac
- 7-Zip/PeaZip for Linux
The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/Mastering-Linux-Shell-Scripting-Second-Edition. In case there's an update to the code, it will be updated on the existing GitHub repository.
We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!
Download the color images
We also provide a PDF file that has color images of the screenshots/diagrams used in this book. You can download it from https://www.packtpub.com/sites/default/files/downloads/MasteringLinuxShellScriptingSecondEdition_ColorImages.pdf.
Conventions used
There are a number of text conventions used throughout this book.
CodeInText
: Indicates code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles. Here is an example: "Edit your script so that it reads like the following complete code block for $HOME/bin/hello2.sh
"
A block of code is set as follows:
if [ $file_compression = "L" ] ; then tar_opt=$tar_l elif [ $file_compression = "M" ]; then tar_opt=$tar_m else tar_opt=$tar_h fi
Any command-line input or output is written as follows:
$ type lsls is aliased to 'ls --color=auto'
Bold: Indicates a new term, an important word, or words that you see onscreen. For example, words in menus or dialog boxes appear in the text like this. Here is an example: "Another very useful feature is found on the Preferences
| Plugins
tab"
Note
Warnings or important notes appear like this.
Note
Tips and tricks appear like this.
Get in touch
Feedback from our readers is always welcome.
General feedback: Email [email protected]
and mention the book title in the subject of your message. If you have questions about any aspect of this book, please email us at [email protected]
.
Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you would report this to us. Please visit www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details.
Piracy: If you come across any illegal copies of our works in any form on the Internet, we would be grateful if you would provide us with the location address or website name. Please contact us at [email protected]
with a link to the material.
If you are interested in becoming an author: If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, please visit authors.packtpub.com.
Reviews
Please leave a review. Once you have read and used this book, why not leave a review on the site that you purchased it from? Potential readers can then see and use your unbiased opinion to make purchase decisions, we at Packt can understand what you think about our products, and our authors can see your feedback on their book. Thank you!
For more information about Packt, please visit packtpub.com.