Mastering Linux Shell Scripting Second Edition Mokhtar Ebrahim - The ebook is ready for download, no waiting required
Mastering Linux Shell Scripting Second Edition Mokhtar Ebrahim - The ebook is ready for download, no waiting required
https://ebookultra.com/download/linux-shell-scripting-cookbook-second-
edition-shantanu-tushar/
https://ebookultra.com/download/learning-linux-shell-scripting-second-
editionn-edition-ganesh-naik/
https://ebookultra.com/download/linux-command-line-and-shell-
scripting-bible-3rd-edition-richard-blum/
https://ebookultra.com/download/beginning-shell-scripting-1st-edition-
anderson/
Linux shell scripting cookbook over 110 practical recipes
to solve real world shell problems guaranteed to make you
wonder how you ever lived without them 2. ed Edition
Lakshman
https://ebookultra.com/download/linux-shell-scripting-cookbook-
over-110-practical-recipes-to-solve-real-world-shell-problems-
guaranteed-to-make-you-wonder-how-you-ever-lived-without-them-2-ed-
edition-lakshman/
https://ebookultra.com/download/beginning-portable-shell-scripting-
from-novice-to-professional-1st-edition-peter-seebach/
https://ebookultra.com/download/windows-powershell-cookbook-the-
complete-guide-to-scripting-microsoft-s-command-shell-3rd-edition-
holmes/
https://ebookultra.com/download/scripting-your-world-the-official-
guide-to-second-life-scripting-1st-edition-dana-moore/
https://ebookultra.com/download/linux-system-administration-second-
edition-craig-hunt-linux-library-vicki-stanfield/
Mastering Linux Shell Scripting Second Edition Mokhtar
Ebrahim Digital Instant Download
Author(s): Mokhtar Ebrahim, Andrew Mallett
ISBN(s): 9781788990554, 1788990552
Edition: 2
File Details: PDF, 13.64 MB
Year: 2018
Language: english
Mastering Linux Shell Scripting
Second Edition
"QSBDUJDBMHVJEFUP-JOVYDPNNBOEMJOF#BTITDSJQUJOHBOE
4IFMMQSPHSBNNJOH
Mokhtar Ebrahim
Andrew Mallett
BIRMINGHAM - MUMBAI
Mastering Linux Shell Scripting
Second Edition
Copyright a 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
XXXQBDLUQVCDPN
NBQUJP
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
PacktPub.com
Did you know that Packt offers eBook versions of every book published, with PDF and
ePub files available? You can upgrade to the eBook version at XXX1BDLU1VCDPN and as a
print book customer, you are entitled to a discount on the eBook copy. Get in touch with us
at TFSWJDF!QBDLUQVCDPN for more details.
At XXX1BDLU1VCDPN, you can also read a collection of free technical articles, sign up for a
range of free newsletters, and receive exclusive discounts and offers on Packt books and
eBooks.
Contributors
I would like to thank my wife for helping me with all her efforts to finish this book. Thank
you, Doaa, for being a part of that. Also, I would like to thank everyone at Packt for
working with me to make sure the book is released. Last but not least, I'd like to thank
Brian Fox, the author of the bash shell, for creating such an awesome piece of software;
without it, such a book would not exist.
Andrew Mallett is the owner of The Urban Penguin, and he is a comprehensive provider of
professional Linux software development, training, and services. Having always been a
command-line fan, he feels that so much time can be saved through knowing command-line
shortcuts and scripting. TheUrbanPenguin YouTube channel, maintained by Andrew, has
well over 800 videos to support this, and he has authored four other Packt titles.
About the reviewer
Sebastiaan Tammer is a Linux enthusiast from The Netherlands. After attaining his BSc in
Information Sciences, he graduated with MSc in Business Informatics, both from Utrecht
University. His professional career started in Java development before he pivoted into
Linux.
He has worked on number of technologies, such as Puppet, Chef, Docker, and Kubernetes.
He spends a lot of time in and around his terminal of choice: bash. Whether it is creating
complex scripting solutions or just automating simple tasks, there is hardly anything he
hasn't done with bash!
I would like to thank my girlfriend, Sanne, for all the help and support she has given me
throughout the years. She has had to endure the late nights studying, me fixing stuff
(which I had inevitably broken only hours earlier), and my endless storytelling about all
those exciting new technologies. Thanks for the enormous amount of patience and love, I
could not have done it without you!
[ ii ]
Table of Contents
[ iii ]
Table of Contents
Questions 119
Further reading 120
Chapter 7: Creating Building Blocks with Functions 121
Technical requirements 121
Introducing functions 122
Passing parameters to functions 125
Passing arrays 128
Variable scope 129
Returning values from functions 130
Recursive functions 132
Using functions in menus 133
Summary 135
Questions 135
Further reading 136
Chapter 8: Introducing the Stream Editor 137
Technical requirements 138
Using grep to display text 138
Displaying received data on an interface 138
Displaying user account data 139
Listing the number of CPUs in a system 140
Parsing CSV files 143
The CSV file 143
Isolating catalog entries 145
Understanding the basics of sed 146
The substitute command 147
Global replacement 148
Limiting substitution 150
Editing the file 150
Other sed commands 151
The delete command 151
The insert and append commands 152
The change command 153
The transform command 153
Multiple sed commands 154
Summary 154
Questions 155
Further reading 156
Chapter 9: Automating Apache Virtual Hosts 157
Technical requirements 158
Apache name-based Virtual Hosts 158
Creating the virtual host template 159
First steps 160
[ iv ]
Table of Contents
[v]
Table of Contents
Questions 210
Further reading 210
Chapter 12: Summarizing Logs with AWK 211
Technical requirements 211
The HTTPD log file format 212
Displaying data from web logs 213
Selecting entries by date 213
Summarizing 404 errors 215
Summarizing HTTP access codes 215
Resources hits 217
Identify image hotlinking 218
Displaying the highest ranking IP address 219
Displaying the browser data 220
Working with email logs 221
Summary 221
Questions 222
Further reading 222
Chapter 13: A Better lastlog with AWK 223
Technical requirements 223
Using AWK ranges to exclude data 224
The lastlog command 224
Horizontally filtering rows with AWK 225
Counting matched rows 225
Conditions based on the number of fields 226
Manipulating the AWK record separator to report on XML data 228
Apache Virtual Hosts 228
XML catalog 229
Summary 231
Questions 232
Further reading 232
Chapter 14: Using Python as a Bash Scripting Alternative 233
Technical requirements 233
What is Python? 234
Saying Hello World the Python way 236
Pythonic arguments 236
Supplying arguments 237
Counting arguments 237
Significant whitespace 239
Reading user input 240
Using Python to write to files 241
String manipulation 242
[ vi ]
Table of Contents
Summary 243
Questions 244
Further reading 244
Assessments 245
Other Books You May Enjoy 252
Index 255
[ vii ]
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.
$IBQUFS, Creating Interactive Scripts, covers how to read input from the user using SFBE
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.
$IBQUFS, Conditions Attached, will introduce the JG statement, the DBTF statement, and
other testing command such as FMTF and FMJG.
$IBQUFS, Creating Code Snippets, covers creating and using code snippets using editors,
such as vim and Visual Studio Code.
$IBQUFS, Alternative Syntax, will discuss advanced testing using << and how to perform
arithmetic operations.
$IBQUFS, Iterating with Loops, will teach you how to use GPS loops, XIJMF loops, and
VOUJM loops to iterate over simple values and complex values.
$IBQUFS, 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.
$IBQUFS, Introducing the Stream Editor, will introduce the basics of sed tool to manipulate
files, such as adding, replacing deleting, and transforming text.
$IBQUFS, Automating Apache Virtual Hosts, contains a practical example of sed and
explains how to create virtual hosts automatically using sed.
$IBQUFS, AWK Fundamentals, will discuss AWK and how to filter file content using it.
Also, we will discuss some AWK programming basics.
$IBQUFS, Regular Expressions, covers regular expressions, their engines, and how to use
them with sed and AWK to empower your script.
$IBQUFS, Summarizing Logs with AWK, will show how to process the IUUQEDPOG
Apache log file using AWK and extract useful well-formatted data.
[2]
Preface
$IBQUFS, 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.
$IBQUFS, 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.
You should know some Linux basics such as the basic commands such as MT, DE, and
XIJDI.
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 IUUQTHJUIVCDPN
1BDLU1VCMJTIJOH.BTUFSJOH-JOVY4IFMM4DSJQUJOH4FDPOE&EJUJPO. 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 IUUQTHJUIVCDPN1BDLU1VCMJTIJOH. Check them out!
[3]
Preface
Conventions used
There are a number of text conventions used throughout this book.
$PEF*O5FYU: 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 )0.&CJOIFMMPTI"
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"
[4]
Preface
Get in touch
Feedback from our readers is always welcome.
General feedback: Email GFFECBDL!QBDLUQVCDPN 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 RVFTUJPOT!QBDLUQVCDPN.
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 XXXQBDLUQVCDPNTVCNJUFSSBUB, 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 DPQZSJHIU!QBDLUQVCDPN 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
BVUIPSTQBDLUQVCDPN.
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!
[5]
1
The What and Why of Scripting
with Bash
Welcome to the what and why of bash scripting. In this chapter, you will discover the types
of shells in Linux and why we chose bash. You will learn what bash is, how to write your
first bash script, and how to run it. Also, you will see how to configure Linux editors, such
as vim and nano, in order to type your code.
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.
Technical requirements
You'll need a running Linux box. It doesn't matter which distribution you use, since all
Linux distributions are shipped nowadays with the bash shell.
Download and install Visual Studio Code, which is free from Microsoft. You can download
it from here: IUUQTDPEFWJTVBMTUVEJPDPN.
You can use VS Code as an editor instead of vim and nano; it's up to you.
We prefer to use VS Code because it has a lot of features such as code completion,
debugging, and many more besides.
Install CBTIEC, 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
If you are using a Debian-based distribution, you can install it like this:
$ sudo apt-get install bashdb
The shell translates your commands and sends them to the system. Most Linux
distributions are shipped with many shells.
[7]
Other documents randomly have
different content
gone some distance farther on in life's journey, while she is in the
very prime and flush of girlhood? These thoughts flash through his
mind, and make him appear nervous and absent as he begins to talk
about last night's party. But his mind is made up.
'We missed you, Miss Grey. Will you pardon us that you had no
invitation? My daughter is not much accustomed to sending them
out.'
'Please, don't mention it, Sir Herbert. I am very glad to go to
Government House when I'm wanted there; but one cannot always
be invited, you know.'
'But I like you always to come. The omission shall not happen again.
We had a wretchedly stupid gathering. Spare me similar
disappointments in future, Miss Grey, by—by taking the right of
arranging these matters into your own hands.'
The girl looks up inquiringly. Nothing can be more unsuspecting and
guileless than the questioning eyes that meet Sir Herbert's.
'Will you take the right, Katie? My life has grown strangely desolate
and lonely of late; will you cheer it with your presence? In short, will
you be my wife?'
The question is asked now, eagerly and impassion'dly, and Miss
Grey's eyes droop under the Admiral's gaze. This vision has been
dazzling her mind so long; she has dreamt of it, thought of it; and
now the offer of marriage has really come! Though the triumph is
making her heart throb, she can hardly tell whether she is glad or
sorry. But she does not draw back. For the treasure of Sir Herbert's
loyal affection, for his true earnest love, she will give in exchange
her youth and beauty. She thinks the bargain a fair one, and
wonders can anything more be required.
When Sir Herbert leaves his affianced wife, he goes down to her
father, to tell him of what he calls his 'good fortune.'
'Yes; and mamma and Helen shall hear all about it from me. Won't
they be surprised!' adds the young lady with a short low laugh, as
the Admiral goes out of the room. She hears him close the library
door, and then says to herself with another little spasmodic laugh:
'Every one will be surprised, as I am myself, to think how quickly it
has all come about. Last evening I was excluded from Government
House, and now I have promised to rule and reign there. Which has
conquered—Laura Best or I?'
A. C. S.
1.D. The copyright laws of the place where you are located also
govern what you can do with this work. Copyright laws in most
countries are in a constant state of change. If you are outside
the United States, check the laws of your country in addition to
the terms of this agreement before downloading, copying,
displaying, performing, distributing or creating derivative works
based on this work or any other Project Gutenberg™ work. The
Foundation makes no representations concerning the copyright
status of any work in any country other than the United States.
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
ebookultra.com