Professional Documents
Culture Documents
Ebook Mastering Linux Shell Scripting Second Edition Mokhtar Ebrahim Andrew Mallett Online PDF All Chapter
Ebook Mastering Linux Shell Scripting Second Edition Mokhtar Ebrahim Andrew Mallett Online PDF All Chapter
Ebook Mastering Linux Shell Scripting Second Edition Mokhtar Ebrahim Andrew Mallett Online PDF All Chapter
https://ebookmeta.com/product/mastering-linux-shell-scripting-
second-edition-mokhtar-ebrahim-andrew-mallett/
https://ebookmeta.com/product/linux-shell-scripting-bootcamp-1st-
edition-james-kent-lewis/
https://ebookmeta.com/product/linux-shell-scripting-cookbook-2nd-
edition-shantanu-tushar-sarath-lakshman/
https://ebookmeta.com/product/linux-shell-scripting-cookbook-
quick-answers-to-common-problems-shantanu-tushar/
Linux introduce to beginners guide UNIX operating
system Linux shell scripting and command line Linux
System 1st Edition Erickson Karnel & Coding Hood
https://ebookmeta.com/product/linux-introduce-to-beginners-guide-
unix-operating-system-linux-shell-scripting-and-command-line-
linux-system-1st-edition-erickson-karnel-coding-hood/
https://ebookmeta.com/product/salt-open-automating-your-
enterprise-and-your-network-1st-edition-andrew-mallett/
https://ebookmeta.com/product/salt-open-automating-your-
enterprise-and-your-network-1st-edition-andrew-mallett-2/
https://ebookmeta.com/product/pro-bash-learn-to-script-and-
program-the-gnu-linux-shell-3rd-edition-jayant-varma/
https://ebookmeta.com/product/linux-fundamentals-second-edition-
richard-blum/
Mastering Linux Shell Scripting
Second Edition
Mokhtar Ebrahim
Andrew Mallett
BIRMINGHAM - MUMBAI
Mastering Linux Shell
Scripting Second Edition
Copyright © 2018 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any
form or by any means, without the prior written permission of the publisher, except in the case of brief
quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information
presented. However, the information contained in this book is sold without warranty, either express or implied.
Neither the authors, nor Packt Publishing or its dealers and distributors, will be held liable for any damages
caused or alleged to have been caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products
mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the
accuracy of this information.
ISBN 978-1-78899-055-4
www.packtpub.com
mapt.io
Mapt is an online digital library that gives you full access to over
5,000 books and videos, as well as industry leading tools to help you
plan your personal development and advance your career. For more
information, please visit our website.
Why subscribe?
Spend less time learning and more time coding with practical
eBooks and Videos from over 4,000 industry professionals
Improve your learning with Skill Plans built especially for you
developers and tech professionals, just like you, to help them share
their insight with the global tech community. You can make a
general application, apply for a specific hot topic that we are
recruiting an author for, or submit your own idea.
Table of Contents
Title Page
Packt Upsell
Why subscribe?
PacktPub.com
Contributors
Conventions used
Get in touch
Reviews
1. The What and Why of Scripting with Bash
Technical requirements
Command type
Command PATH
Configuring vim
Configuring nano
Configuring gedit
Hello World!
Hello Dolly!
Declaring variables
User-defined variables
Environment variables
Variable scope
Command substitution
Debugging your scripts
Summary
Questions
Further reading
2. Creating Interactive Scripts
Technical requirements
Script comments
Enhancing scripts with read prompts
Passing options
Passing parameters with options
Connecting to a server
Version 1 – ping
Reading files
Summary
Questions
Further reading
3. Conditions Attached
Technical requirements
Checking strings
Checking files and directories
Checking numbers
Combining tests
Summary
Questions
Further reading
4. Creating Code Snippets
Technical requirements
Abbreviations
Summary
Questions
Further reading
5. Alternative Syntax
Technical requirement
Pattern matching
Regular expressions
Regular expression script
Arithmetic operations using ((
Simple math
Parameter manipulation
Standard arithmetic tests
Summary
Questions
Further reading
6. Iterating with Loops
Technical requirement
for loops
Advanced for loops
The IFS
Counting directories and files
Summary
Questions
Further reading
7. Creating Building Blocks with Functions
Technical requirements
Introducing functions
Passing arrays
Variable scope
Summary
Questions
Further reading
8. Introducing the Stream Editor
Technical requirements
Global replacement
Limiting substitution
Questions
Further reading
9. Automating Apache Virtual Hosts
Technical requirements
Apache name-based Virtual Hosts
First steps
Isolating lines
Summary
Questions
Further reading
10. AWK Fundamentals
Technical requirements
User-defined variables
Conditional statements
The if command
while loops
for loops
Formatting output
Built-in functions
Summary
Questions
Further reading
11. Regular Expressions
Technical requirements
Regular expression engines
Anchor characters
The dot character
Ranges of characters
Special character classes
The asterisk
Expression grouping
Using grep
Summary
Questions
Further reading
12. Summarizing Logs with AWK
Technical requirements
Questions
Further reading
13. A Better lastlog with AWK
Technical requirements
XML catalog
Summary
Questions
Further reading
14. Using Python as a Bash Scripting Alternative
Technical requirements
What is Python?
Saying Hello World the Python way
Pythonic arguments
Supplying arguments
Counting arguments
Significant whitespace
Reading user input
String manipulation
Summary
Questions
Further reading
Assessments
Chapter 1
Chapter 2
Chapter 3
Chapter 4
Chapter 5
Chapter 6
Chapter 7
Chapter 8
Chapter 9
Chapter 10
Chapter 11
Chapter 12
Chapter 13
Chapter 14
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
, The What and Why of Scripting with Bash, will introduce
Chapter 1
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.
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 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.
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.
, A Better lastlog with AWK, will show you how to use AWK
Chapter 13
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.
Once the file is downloaded, please make sure that you unzip or
extract the folder using the latest version of:
The code bundle for the book is also hosted on GitHub at https://githu
b.com/PacktPublishing/Mastering-Linux-Shell-Scripting-Second-Edition. In case
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 ht
tps://www.packtpub.com/sites/default/files/downloads/MasteringLinuxShellScriptingSec
ondEdition_ColorImages.pdf .
Conventions used
There are a number of text conventions used throughout this book.
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
copyright@packtpub.com with a link to the material.
Like in any other scripting language, variables are the basic blocks of
coding. You will learn how to declare variables such as integers,
strings, and arrays. Furthermore, you will learn how to export these
variables and extend their scope outside the running process.
Finally, you will see how to visually debug your code using Visual
Studio Code.
You can use VS Code as an editor instead of vim and nano; it's up to
you.
Install bashdb, which is a required package for the bash debug plugin.
If you are using a Red Hat-based distribution, you can install it like
this:
$ sudo yum install bashdb
Install the plugin for VS Code, called bash debug, from https://marketpl
ace.visualstudio.com/items?itemName=rogalmic.bash-debug. This plugin will be
https://github.com/PacktPublishing/Mastering-Linux-Shell-Scripting-Second-Edition/tr
ee/master/Chapter01
Types of Linux shells
As you know, Linux consists of some major parts, such as the kernel,
the shell, and the GUI interface (Gnome, KDE, and so on).
The shell translates your commands and sends them to the system.
Most Linux distributions are shipped with many shells.
Every shell has its own features, and some of them are very popular
among developers today. These are some of the popular ones:
Now we know the types of shells and we know that we are going to
use bash, so what is bash scripting?
What is bash scripting?
The basic idea of bash scripting is to execute multiple commands to
automate a specific job.
As you might know, you can run multiple commands from the shell
by separating them with semi colons (;):
ls ; pwd
You can say that the shell is the glue that binds these commands
together.
The bash command hierarchy
When working on the bash shell and when you are sitting
comfortably at your prompt eagerly waiting to type a command, you
will most likely feel that it is a simple matter of typing and hitting the
Enter key. You should know better than to think this, as things are
never quite as simple as we imagine.
Command type
For example, if we type and enter ls to list files, it is reasonable to
think that we were running the command. It is possible, but we
often will be running an alias. Aliases exist in memory as a shortcut
to commands or commands with options; these aliases are used
before we even check for the file. Bash's built-in type command can
come to our aid here. The type command will display the type of
command for a given word entered at the command line. The types
of command are listed as follows:
Alias
Function
Shell built-in
Keyword
File
We can extend this further to display all the matches for the given
command:
$ type -a ls
ls is aliased to 'ls --color=auto'
ls is /bin/ls
If we need to just type in the output, we can use the -t option. This
is useful when we need to test the command type from within a
script and only need the type to be returned. This excludes any
superfluous information, and thus makes it easier for us humans to
read. Consider the following command and output:
$ type -t ls
alias
The output is clear and simple, and is just what a computer or script
requires.
The built-in type can also be used to identify shell keywords such as
if, and case. The following command shows type being used against
You can also see that the function definition is printed when we
stumble across a function when using type.
Command PATH
Linux will check for executables in the PATH environment only when
the full or relative path to the program is supplied. In general, the
current directory is not searched unless it is in the PATH. It is possible
to include our current directory within the PATH by adding the
directory to the PATH variable. This is shown in the following
command example:
$ export PATH=$PATH:.
This appends the current directory to the value of the PATH variable;
each item in the PATH is separated using a colon. Now your PATH has
been updated to include the current working directory and, each
time you change directories, the scripts can be executed easily. In
general, organizing scripts into a structured directory hierarchy is
probably a great idea. Consider creating a subdirectory called bin
within your home directory and add the scripts into that folder.
Adding $HOME/bin to your PATH variable will enable you to find the scripts
by name and without the file path.
Un beau matin, il reçut une lettre de son frère aîné, avec lequel il
n’avait cessé de correspondre, de loin en loin, il est vrai, mais d’une
façon régulière. Il lui annonçait la mort de leur père, et lui disait que
la succession étant réglée, il lui revenait, pour sa part, environ trois
mille lires.
C’était, pour lui, la fortune et le moyen de réaliser son nouveau
rêve. Justement, dans le quartier sicilien, un café était à louer, déjà
très achalandé et dont la salle se prêterait merveilleusement à
l’installation d’une scène, ce qui lui permettrait d’en faire un théâtre
par intermittence.
Il le loua incontinent, et, trois mois après, il s’y installait avec sa
femme toujours résignée, toujours aussi douce et aimante.
Encore qu’il eût beaucoup de peine à trouver, parmi les Italiens
de la colonie, des artistes suffisants, et n’ayant guère à sa
disposition que des amateurs bénévoles, grâce à la beauté de la
Madalena, le succès dépassa ses espérances.
Il eut, d’ailleurs, la très heureuse inspiration, comme on était aux
approches de la semaine sainte, de débuter par une de ces
Passions, dont raffolent non seulement les Siciliens, mais tout le
peuple de l’Italie méridionale.
Et il recommença l’année suivante…
. . . . . . . . . . . . . . . . . . . .
*
* *