Mastering Linux Shell Scripting a practical guide to Linux command line Bash scripting and Shell programming Ebrahim download
Mastering Linux Shell Scripting a practical guide to Linux command line Bash scripting and Shell programming Ebrahim download
https://textbookfull.com/product/mastering-linux-shell-scripting-
a-practical-guide-to-linux-command-line-bash-scripting-and-shell-
programming-ebrahim/
https://textbookfull.com/product/mastering-linux-shell-scripting-
a-practical-guide-to-linux-command-line-bash-scripting-and-shell-
programming-second-edition-edition-mokhtar-ebrahim/
https://textbookfull.com/product/linux-shell-scripting-cookbook-
third-edition-clif-flynt/
https://textbookfull.com/product/bash-script-linux-command-line-
reference-1st-edition-aditya-tiwari/
https://textbookfull.com/product/shell-programming-in-unix-linux-
and-os-x-stephen-g-kochan/
The Linux Command Line: A Complete Introduction 2nd
Edition William E. Shotts
https://textbookfull.com/product/the-linux-command-line-a-
complete-introduction-2nd-edition-william-e-shotts/
https://textbookfull.com/product/linux-basics-for-hackers-
getting-started-with-networking-scripting-and-security-in-kali-
occupytheweb/
https://textbookfull.com/product/wicked-cool-shell-
scripts-101-scripts-for-linux-os-x-and-unix-systems-dave-taylor/
https://textbookfull.com/product/computing-with-the-raspberry-pi-
command-line-and-gui-linux-1st-edition-brian-schell/
https://textbookfull.com/product/linux-basics-for-hackers-
getting-started-with-networking-scripting-and-security-in-
kali-1st-edition-occupytheweb/
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
Technical requirements
Command PATH
Configuring vim
Configuring nano
Configuring gedit
Hello Dolly!
Declaring variables
User-defined variables
Environment variables
Variable scope
Command substitution
Summary
Questions
Further reading
Technical requirements
Script comments
Try to be standard
Enhancing learning with simple scripts
Further reading
3. Conditions Attached
Technical requirements
Simple decision paths using command-line lists
Testing strings
Testing integers
Checking strings
Checking files and directories
Checking numbers
Combining tests
Summary
Questions
Further reading
4. Creating Code Snippets
Technical requirements
Abbreviations
Questions
Further reading
5. Alternative Syntax
Technical requirement
Recapping the test command
Testing files
Adding logic
Square brackets as not seen before
Setting defaults
When in doubt – quote!
Advanced tests using [[
White space
Summary
Questions
Further reading
6. Iterating with Loops
Technical requirement
for loops
Advanced for loops
The IFS
Summary
Questions
Further reading
7. Creating Building Blocks with Functions
Technical requirements
Introducing functions
Passing parameters to functions
Passing arrays
Variable scope
Returning values from functions
Recursive functions
Limiting substitution
Editing the file
Other sed commands
The delete command
Summary
Questions
Further reading
9. Automating Apache Virtual Hosts
Technical requirements
Apache name-based Virtual Hosts
Creating the virtual host template
First steps
Isolating lines
sed script files
Automating virtual host creation
Prompting for data during site creation
Summary
Questions
Further reading
10. AWK Fundamentals
Technical requirements
The history behind AWK
Displaying and filtering content from files
AWK variables
User-defined variables
Conditional statements
The if command
while loops
for loops
Formatting output
Further filtering to display users by UID
AWK control files
Built-in functions
Summary
Questions
Further reading
Anchor characters
The dot character
The character class
Ranges of characters
Using grep
Summary
Questions
Further reading
12. Summarizing Logs with AWK
Technical requirements
Resources hits
Identify image hotlinking
Summary
Questions
Further reading
Summary
Questions
Further reading
What is Python?
Supplying arguments
Counting arguments
Significant whitespace
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.
In the Trenches.
The systematic laying out of street mains in the first company district
was begun in the summer of 1881. It must not be thought, of course,
that these old-time conduits resembled strikingly those of the present
day. The method then used was to dig a trench in which were laid the
pipes measuring twenty feet in length. Through these the conductors
were drawn, two half-round copper wires kept in place first by heavy
cardboard and afterward by rope. The conductors having been drawn
in, a preparation of asphaltum and linseed oil was forced into the
piping to serve as insulation. The spending of three and four arduous
nights a week in these trenches by Mr. Edison and his associates
suggests the rigor of the later European warfare. This work, together
with that incident to the operation of the new station, often proved
too much even for Edison’s phenomenal endurance. At such times he
slept on a cot close beside the running engines, while the rest of the
crew crawled in on the lower row of field-magnet coils of the
dynamos, a place warm enough, though a trifle bumpy. One of the
inventor’s early assistants tells of going to sleep standing up, leaning
against a door frame—this, after forty-eight hours of uninterrupted
work.
The Dynamo Room of the First Edison Electric Lighting Station
in New York
September 4th saw a full 400 lamps turned on from the Pearl Street
station. From that day on the station supplied current continuously
until 1895, with but two brief interruptions. One of these happened in
1883 and lasted three hours. The other resulted from the serious fire
of January 2, 1890, and lasted less than half a day. The record in the
second case would appear astounding, as no less a handicap
occurred than the burning down of the station itself. The situation
was saved, however, by the presence of an auxiliary plant that had
already been opened on Liberty Street.
The seventeen great gas engines are operated by gas from the blast furnaces
which was formerly allowed to escape. Each engine drives a 2,500-kilowatt
dynamo.
Early Growth.
Within two years from the opening of the station the demand for
service had so increased that over one hundred applications were
filed in excess of what could be accepted, because the plant was
taxed already to its utmost capacity. Allusion has already been made
to the auxiliary plant at Liberty Street, a station of 2,000 lights’
capacity which was instituted in 1886. By 1887, not only a second but
a third district had been mapped out, the whole extending from
Eighteenth to Forty-fifth Street. All the underground system in the
two new districts was laid according to Edison’s new three-wire
patent; and it was presently announced that customers would be
supplied with power as well as with light.
Six months after the disastrous fire of 1890, in which the Pearl Street
station was burned, the site was chosen for the Edison Duane Street
building on which operations were so hastened that machines were
installed and current turned on the first of May the following year.
Construction.
Not only are passengers conveyed up and down by electric elevators
in skyscrapers, but the buildings themselves are erected by means of
electricity. Recent examples of such construction are the Woolworth
and Equitable buildings in New York City; in this last instance a
thousand horse-power was used in digging the foundations alone.
[280]
Loft Manufacturing.
Aside from these special instances of electricity in construction, one
must think of electricity as responsible for nearly all the
manufacturing, large and small, that goes on in the ever-increasing
number of loft-buildings throughout all large cities. For example, New
York City serves as the center of the garment-making industry for the
entire country, there being fully a quarter of a million garment-trade
workers in the Greater City. Along Fifth and Fourth Avenues are found
the large establishments, electrically equipped throughout for cutting,
stitching and pressing, while even in the smallest shops on the East
Side foot-power machines have become almost a thing of the past.
Electric Heating.
The commercial use of electric heating is one of the more recent
electrical developments. For the most part, this also applies to the
garment trade and its closely allied clothing industries. In the
modernly equipped factories one finds electric flat irons, velvet
steamers and coffee urns. In the printing trade, electrically heated
linotype melting pots are being introduced successfully, while glue-
pots and sealing-wax melters can be seen in binderies and banking
institutions. Absence of fire risk accounts for the introduction of
electric heating units of different kinds into the motion-picture film
manufacturing industry, a rapidly growing province. The same
element of safety where inflammable substances are employed has
produced the electric japan oven and similar apparatus.
Electricity in Medicine.
Another domain of electric enterprise of the greatest value for the
country at large is the increasing use of electricity in medicine. The
most conspicuous element in this is the wide-spread acceptance of
the X-ray as a necessary tool of the medical profession. Newspapers
and magazines were full of the remarkable X-ray achievements of
surgeons in charge of the various European war hospitals. Those, of
course, were spectacular instances, but it should not be forgotten
that every day, in our great hospitals, the X-ray is proving itself
almost indispensable in the examination of the sick and injured.
Besides utilizing X-ray in the diagnosis of disease, the rays themselves
are employed in treatment of cancer and skin diseases. The oculist,
the dentist, indeed medical specialists of all kinds, are coming to
recognize the immense aid that electricity can give in its various
forms and applications.
The Great Press Room of “The New York Times” is all
Electrically Operated
Electric Vehicles.
The electric truck has already demonstrated itself as a safer and less
expensive rival of the gasoline delivery truck in many kinds of service.
In the boroughs of Manhattan and the Bronx alone, in New York City,
there were more than 2,000 such trucks in operation in 1916.
Counting both pleasure and business vehicles, the borough of
Manhattan boasted about 2,500 storage-battery driven wagons in
active use. It is rather interesting to note that Chicago operates many
more electric pleasure cars than New York, while New York does far
more of its business by means of the electric vehicle. Recently, there
was established in New York an electric co-operative garage, the joint
enterprise of the electric passenger car manufacturers and an electric
company. It was believed that by providing proper and adequate
facilities for garaging electric pleasure vehicles the use of passenger-
electrics in New York City would be greatly increased.
[284]
One Type of Electric Construction on Railroads
The system shown here is used upon the New York, New Haven and Hartford
Railroad. It consists of pairs of wire cables supported by bridges placed about 300
feet apart. Rigid triangles of iron pipe are secured to these cables and the trolley
wire attached to the triangles. The trolley wire is kept rigid and free from slack in
this manner.
A Group of Folding Machines which Automatically Grasp the Flat Sheet and Fold it up
to the Size of the Magazine
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.
textbookfull.com