[FREE PDF sample] Mastering Linux Shell Scripting a practical guide to Linux command line Bash scripting and Shell programming Ebrahim ebooks
[FREE PDF sample] Mastering Linux Shell Scripting a practical guide to Linux command line Bash scripting and Shell programming Ebrahim ebooks
com
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/linux-shell-scripting-cookbook-third-
edition-clif-flynt/
textbookfull.com
https://textbookfull.com/product/bash-script-linux-command-line-
reference-1st-edition-aditya-tiwari/
textbookfull.com
https://textbookfull.com/product/fullstack-d3-and-data-visualization-
build-beautiful-data-visualizations-and-dashboards-with-d3-amelia-
wattenberger/
textbookfull.com
Design and Analysis of Experiments with R Ninth Edition
John Lawson
https://textbookfull.com/product/design-and-analysis-of-experiments-
with-r-ninth-edition-john-lawson/
textbookfull.com
https://textbookfull.com/product/chinas-expansion-in-international-
business-the-geopolitical-impact-on-the-world-economy-peter-balaz/
textbookfull.com
https://textbookfull.com/product/the-palgrave-handbook-of-prison-and-
the-family-marie-hutton/
textbookfull.com
https://textbookfull.com/product/the-gun-digest-2016-70th-edition-
jacobs/
textbookfull.com
Toxicology and Biological Monitoring of Metals in Humans
Including Feasibility and Need Bonnie L. Carson
https://textbookfull.com/product/toxicology-and-biological-monitoring-
of-metals-in-humans-including-feasibility-and-need-bonnie-l-carson/
textbookfull.com
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
Lay them for half an hour or more into a pan of strong brine, with
the stalk ends uppermost; this will destroy the small snails and other
insects which cluster in the leaves, and they will fall out and sink to
the bottom. A pound and a half of salt to the gallon of water will
answer for this purpose, and if strained daily it will last for some time.
TO BOIL VEGETABLES GREEN.
After they have been properly prepared and washed, throw them
into plenty of boiling water which has been salted and well skimmed;
and keep them uncovered and boiling fast until they are done, taking
every precaution against their being smoked. Should the water be
very hard, a small half-teaspoonful of carbonate of soda, may be
added with the salt, for every two quarts, and will greatly improve the
colour of the vegetables; but if used in undue proportion it will injure
them; green peas especially will be quickly reduced to a mash if
boiled with too large a quantity.
Water, 1 gallon; salt, 2 oz.; soda, 1/4 oz.; or carbonate of soda, 1
teaspoonful.
POTATOES.
104. We cannot refrain from a few words of remark here on the daily waste of
wholesome food in this country which constitutes one of the most serious
domestic abuses that exist amongst us; and one which it is most painful to
witness while we see at the same time the half-starvation of large masses of
our people. It is an evil which the steady and resolute opposition of the
educated classes would soon greatly check; and which ought not vainly to
appeal to their good sense and good feeling, augmenting, as it must, the
privations of the scantily-fed poor; for the “waste” of one part of the
community cannot fail to increase the “want” of the remainder.
TO BOIL POTATOES.
(As in Ireland.)
Potatoes, to boil well together, should be all of the same sort, and
as nearly equal in size as may be. Wash off the mould, and scrub
them very clean with a hard brush, but neither scoop nor apply a
knife to them in any way, even to clear the eyes.[105] Rinse them
well, and arrange them compactly in a saucepan, so that they may
not lie loose in the water, and that a small quantity may suffice to
cover them. Pour this in cold, and when it boils, throw in about a
large teaspoonful of salt to the quart, and simmer the potatoes until
they are nearly done, but for the last two or three minutes let them
boil rapidly. When they are tender quite through, which may be
known by probing them with a fork, pour all the water from them
immediately, lift the lid of the saucepan to allow the steam to escape,
and place them on a trivet, high over the fire, or by the side of it, until
the moisture has entirely evaporated; then peel, and send them to
table as quickly as possible, either in a hot napkin, or in a dish, of
which the cover is so placed that the steam can pass off. There
should be no delay in serving them after they are once taken from
the fire. Irish families always prefer them served in their skins. Some
kinds will be sufficiently boiled in twenty minutes, others in not less
than three quarters of an hour.
105. “Because,” in the words of our clever Irish correspondent, “the water through
these parts is then admitted into the very heart of the vegetable; and the
latent heat, after cooking, is not sufficient to throw it off; this renders the
potatoes very unwholesome.”
These are never good unless freshly dug. Take them of equal size,
and rub off the skins with a brush or a very coarse cloth, wash them
clean, and put them without salt into boiling, or at least, quite hot
water; boil them softly, and when they are tender enough to serve,
pour off the water entirely, strew some fine salt over them, give them
a shake, and let them stand by the fire in the saucepan for a minute;
then dish and serve them immediately. Some cooks throw in a small
slice of fresh butter, with the salt, and toss them gently in it after it is
dissolved. This is a good mode, but the more usual one is to send
melted butter to table with them, or to pour white sauce over them
when they are very young, and served early in the season.
Very small, 10 to 15 minutes: moderate sized, 15 to 20 minutes.
Obs.—We always, for our own eating, have new potatoes
steamed for ten minutes or longer after the water is poured from
them, and think they are much improved by the process. They
should be thoroughly boiled before this is done.
NEW POTATOES IN BUTTER.
Rub off the skins, wash the potatoes well and wipe them dry; put
them with three ounces of good butter, for a small dish, and with four
ounces or more for a large one, into a well-tinned stewpan or Keep
them well shaken or tossed, that they may be equally done, and
throw in some salt when they begin to stew. This is a good mode of
dressing them when they are very young and watery.
TO BOIL POTATOES.
108. Vegetables and fruit are now so generally forced and brought so early into
our markets, that there is little need of these expedients at present.
CRISPED POTATOES, OR POTATO-RIBBONS. (ENTREMETS.)
(A Plainer Receipt.)
After having washed them, wipe and pare some raw potatoes, cut
them in slices of equal thickness, or into thin shavings, and throw
them into plenty of boiling butter, or very pure clarified dripping. Fry
them of a fine light brown, and very crisp; lift them out with a
skimmer, drain them on a soft warm cloth, dish them very hot, and
sprinkle fine salt over them. This is an admirable way of dressing
potatoes, very common on the Continent, but less so in England
than it deserves to be. Pared in ribbons or shavings of equal width,
as in the receipt above, and served dry and well fried, lightly piled in
a dish, they make a handsome appearance, and are excellent
eating. If sliced they should be something less than a quarter of an
inch thick.
MASHED POTATOES.
Boil them perfectly tender quite through, pour off the water, and
steam them very dry by the directions already given in the receipt of
page 310, peel them quickly, take out every speck, and while they
are still hot, press the potatoes through an earthen cullender, or
bruise them to a smooth mash with a strong wooden fork or spoon,
but never pound them in a mortar, as that will reduce them to a close
heavy paste. Let them be entirely free from lumps, for nothing can be
more indicative of carelessness or want of skill on the part of the
cook, than mashed potatoes sent to table full of these. Melt in a
clean saucepan a slice of good butter with a few spoonsful of milk,
or, better still, of cream; put in the potatoes after having sprinkled
some fine salt upon them, and stir the whole over a gentle fire with a
wooden spoon, until the ingredients are well-mixed, and the whole is
very hot. It may then be served directly; or heaped high in a dish, left
rough on the surface, and browned before the fire; or it may be
pressed into a well buttered mould of handsome form, which has
been strewed with the finest bread-crumbs, and shaken free from the
loose ones, then turned out, and browned in a Dutch or common
oven. More or less liquid will be required to moisten sufficiently
potatoes of various kinds.
Potatoes mashed, 2 lbs.; salt, 1 teaspoonful; butter, 1 to 2 oz.; milk
or cream, 1/4 pint.
Obs.—Mashed potatoes are often moulded with a cup, and then
equally browned: any other shape will answer the purpose as well,
and many are of better appearance.
(Good.)
Boil some good potatoes as dry as possible, or let them be
prepared by Captain Kater’s receipt; mash a pound of them very
smoothly, and mix with them while they are still warm, two ounces of
fresh butter, a teaspoonful of salt, a little nutmeg, the beaten and
strained yolks of four eggs, and last of all the whites thoroughly
whisked. Mould the mixture with a teaspoon and drop it into a small
pan of boiling butter, or of very pure lard, and fry the boulettes for
five minutes over a moderate fire: they should be of a fine pale
brown, and very light. Drain them well and dish them on a hot
napkin.
Potatoes, 1 lb.; butter, 2 oz.; salt, 1 teaspoonful; eggs, 4: 5
minutes.
Obs.—These boulettes are exceeding light and delicate, and make
an excellent dish for the second course; but we think that a few
spoonsful of sweet fresh cream boiled with them until the mixture
becomes dry, would both enrich them and improve their flavour. They
should be dropped into the pan with the teaspoon, as they ought to
be small, and they will swell in the cooking.
POTATO RISSOLES.
(French.)
Mash and season the potatoes with salt, and white pepper or
cayenne, and mix with them plenty of minced parsley, and a small
quantity of green onions, or eschalots; add sufficient yolks of eggs to
bind the mixture together, roll it into small balls, and fry them in
plenty of lard or butter over a moderate fire, or they will be too much
browned before they are done through. Ham, or any other kind of
meat finely minced, may be substituted for the herbs, or added to
them.
POTATOES À LA MAÎTRE D’HÔTEL.
Boil in the usual manner some potatoes of a firm kind, peel, and
let them cool; then cut them equally into quarter-inch slices. Dissolve
in a very clean stewpan or saucepan from two to four ounces of
good butter, stir to it a small dessertspoonful of flour, and shake the
pan over the fire for two or three minutes; add by slow degrees a
small cupful of boiling water, some pepper, salt, and a tablespoonful
of minced parsley; put in the potatoes, and toss them gently over a
clear fire until they are quite hot, and the sauce adheres well to
them: at the instant of serving add a dessertspoonful of strained
lemon-juice. Pale veal gravy may be substituted for the water; and
the potatoes after being thickly sliced, may be quickly cut of the
same size with a small round cutter.
POTATOES À LA CRÈME.