Mastering Linux Shell Scripting a practical guide to Linux command line Bash scripting and Shell programming Ebrahim instant download
Mastering Linux Shell Scripting a practical guide to Linux command line Bash scripting and Shell programming Ebrahim instant 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.
Exploring the Variety of Random
Documents with Different Content
Sir William Hervy, and the well-known Sir George Drenghard, one of
the Drenghard family before- mentioned. They had, curiously
enough, all been equally honoured with the distinction of
knighthood, and their schemes for seeing her were manifold, each
fearing that one of the others would steal a march over himself. Not
content with calling, on every imaginable excuse, at the house of the
relative with whom she sojourned, they intercepted her in rides and
in walks; and if any one of them chanced to surprise another in the
act of paying her marked attentions, the encounter often ended in
an altercation of great violence. So heated and impassioned, indeed,
would they become, that the lady hardly felt herself safe in their
company at such times, notwithstanding that she was a brave and
buxom damsel, not easily put out, and with a daring spirit of humour
in her composition, if not of coquetry.
At one of these altercations, which had place in her relative's
grounds, and was unusually bitter, threatening to result in a duel,
she found it necessary to assert herself. Turning haughtily upon the
pair of disputants, she declared that whichever should be the first to
break the peace between them, no matter what the provocation,
that man should never be admitted to her presence again; and thus
would she effectually stultify the aggressor by making the promotion
of a quarrel a distinct bar to its object.
While the two knights were wearing rather a crest-fallen
appearance at her reprimand, the third, never far off, came upon the
scene, and she repeated her caveat to him also. Seeing, then, how
great was the concern of all at her peremptory mood, the lady's
manner softened, and she said with a roguish smile-
'Have patience, have patience, you foolish men! Only bide your
time quietly, and, in faith, I will marry you all in turn!'
They laughed heartily at this sally, all three together, as though
they were the best of friends; at which she blushed, and showed
some embarrassment, not having realized that her arch jest would
have sounded so strange when uttered. The meeting which resulted
thus, however, had its good effect in checking the bitterness of their
rivalry; and they repeated her speech to their relatives and
acquaintance with a hilarious frequency and publicity that the lady
little divined, or she might have blushed and felt more
embarrassment still.
In the course of time the position resolved itself, and the
beauteous Lady Penelope (as she was called) made up her mind;
her choice being the eldest of the three knights, Sir George
Drenghard, owner of the mansion aforesaid, which thereupon
became her home; and her husband being a pleasant man, and his
family, though not so noble, of as good repute as her own, all things
seemed to show that she had reckoned wisely in honouring him with
her preference.
But what may lie behind the still and silent veil of the future none
can foretell. In the course of a few months the husband of her
choice died of his convivialities (as if, indeed, to bear out his name),
and the Lady Penelope was left alone as mistress of his house. By
this time she had apparently quite forgotten her careless declaration
to her lovers collectively; but the lovers themselves had not
forgotten it; and, as she would now be free to take a second one of
them, Sir John Gale appeared at her door as early in her widowhood
as it was proper and seemly to do so.
She gave him little encouragement; for, of the two remaining, her
best beloved was Sir William, of whom, if the truth must be told, she
had often thought during her short married life. But he had not yet
reappeared. Her heart began to be so much with him now that she
contrived to convey to him, by indirect hints through his friends, that
she would not be displeased by a renewal of his former attentions.
Sir William, however, misapprehended her gentle signalling, and
from excellent, though mistaken motives of delicacy, delayed to
intrude himself upon her for a long time. Meanwhile Sir John, now
created a baronet, was unremitting, and she began to grow
somewhat piqued at the backwardness of him she secretly desired to
be forward.
'Never mind,' her friends said jestingly to her (knowing of her
humorous remark, as everybody did, that she would marry them all
three if they would have patience)-'never mind; why hesitate upon
the order of them? Take 'em as they come.'
This vexed her still more, and regretting deeply, as she had often
done, that such a careless speech should ever have passed her lips,
she fairly broke down under Sir John's importunity, and accepted his
hand. They were married on a fine spring morning, about the very
time at which the unfortunate Sir William discovered her preference
for him, and was beginning to hasten home from a foreign court to
declare his unaltered devotion to her. On his arrival in England he
learnt the sad truth.
If Sir William suffered at her precipitancy under what she had
deemed his neglect, the Lady Penelope herself suffered more. She
had not long been the wife of Sir John Gale before he showed a
disposition to retaliate upon her for the trouble and delay she had
put him to in winning her. With increasing frequency he would tell
her that, as far as he could perceive, she was an article not worth
such labour as he had bestowed in obtaining it, and such snubbings
as he had taken from his rivals on the same account. These and
other cruel things he repeated till he made the lady weep sorely, and
wellnigh broke her spirit, though she had formerly been such a
mettlesome dame. By degrees it became perceptible to all her
friends that her life was a very unhappy one; and the fate of the fair
woman seemed yet the harder in that it was her own stately
mansion, left to her sole use by her first husband, which her second
had entered into and was enjoying, his being but a mean and
meagre erection.
But such is the flippancy of friends that when she met them, and
secretly confided her grief to their ears, they would say cheerily,
'Lord, never mind, my dear; there's a third to come yet!'-at which
maladroit remark she would show much indignation, and tell them
they should know better than to trifle on so solemn a theme. Yet
that the poor lady would have been only too happy to be the wife of
the third, instead of Sir John whom she had taken, was painfully
obvious, and much she was blamed for her foolish choice by some
people. Sir William, however, had returned to foreign cities on
learning the news of her marriage, and had never been heard of
since.
Two or three years of suffering were passed by Lady Penelope as
the despised and chidden wife of this man Sir John, amid regrets
that she had so greatly mistaken him, and sighs for one whom she
thought never to see again, till it chanced that her husband fell sick
of some slight ailment. One day after this, when she was sitting in
his room, looking from the window upon the expanse in front, she
beheld, approaching the house on foot, a form she seemed to know
well. Lady Penelope withdrew silently from the sickroom, and
descended to the hall, whence, through the doorway, she saw
entering between the two round towers, which at that time flanked
the gateway, Sir William Hervy, as she had surmised, but looking thin
and travel-worn. She advanced into the courtyard to meet him.
'I was passing through Casterbridge,' he said, with faltering
deference, 'and I walked out to ask after your ladyship's health. I felt
that I could do no less; and, of course, to pay my respects to your
good husband, my heretofore acquaintance . . . But oh, Penelope,
th'st look sick and sorry!'
'I am heartsick, that's all,' said she.
They could see in each other an emotion which neither wished to
express, and they stood thus a long time with tears in their eyes.
'He does not treat 'ee well, I hear,' said Sir William in a low voice.
'May God in Heaven forgive him; but it is asking a great deal!'
'Hush, hush!' said she hastily.
'Nay, but I will speak what I may honestly say,' he answered. 'I am
not under your roof, and my tongue is free. Why didst not wait for
me, Penelope, or send to me a more overt letter? I would have
travelled night and day to come!'
'Too late, William; you must not ask it,' said she, endeavouring to
quiet him as in old times. 'My husband just now is unwell. He will
grow better in a day or two, maybe. You must call again and see him
before you leave Casterbridge.'
As she said this their eyes met. Each was thinking of her lightsome
words about taking the three men in turn; each thought that two-
thirds of that promise had been fulfilled. But, as if it were unpleasant
to her that this recollection should have arisen, she spoke again
quickly: 'Come again in a day or two, when my husband will be well
enough to see you.'
Sir William departed without entering the house, and she returned
to Sir John's chamber. He, rising from his pillow, said, 'To whom hast
been talking, wife, in the courtyard? I heard voices there.'
She hesitated, and he repeated the question more impatiently.
'I do not wish to tell you now,' said she.
'But I wooll know!' said he.
Then she answered, 'Sir William Hervy.'
'By G—- I thought as much!' cried Sir John, drops of perspiration
standing on his white face. 'A skulking villain! A sick man's ears are
keen, my lady. I heard that they were lover-like tones, and he called
'ee by your Christian name. These be your intrigues, my lady, when I
am off my legs awhile!'
'On my honour,' cried she, 'you do me a wrong. I swear I did not
know of his coming!'
'Swear as you will,' said Sir John, 'I don't believe 'ee.' And with
this he taunted her, and worked himself into a greater passion,
which much increased his illness. His lady sat still, brooding. There
was that upon her face which had seldom been there since her
marriage; and she seemed to think anew of what she had so lightly
said in the days of her freedom, when her three lovers were one and
all coveting her hand. 'I began at the wrong end of them,' she
murmured. 'My God-that did I!'
'What?' said he.
'A trifle,' said she. 'I spoke to myself only.'
It was somewhat strange that after this day, while she went about
the house with even a sadder face than usual, her churlish husband
grew worse; and what was more, to the surprise of all, though to
the regret of few, he died a fortnight later. Sir William had not called
upon him as he had promised, having received a private
communication from Lady Penelope, frankly informing him that to do
so would be inadvisable, by reason of her husband's temper.
Now when Sir John was gone, and his remains carried to his
family burying-place in another part of England, the lady began in
due time to wonder whither Sir William had betaken himself. But she
had been cured of precipitancy (if ever woman were), and was
prepared to wait her whole lifetime a widow if the said Sir William
should not reappear. Her life was now passed mostly within the
walls, or in promenading between the pleasaunce and the bowling-
green; and she very seldom went even so far as the high road which
then skirted the grounds on the north, though it has now, and for
many years, been diverted to the south side. Her patience was
rewarded (if love be in any case a reward); for one day, many
months after her second husband's death, a messenger arrived at
her gate with the intelligence that Sir William Hervy was again in
Casterbridge, and would be glad to know if it were her pleasure that
he should wait upon her.
It need hardly be said that permission was joyfully granted, and
within two hours her lover stood before her, a more thoughtful man
than formerly, but in all essential respects the same man, generous,
modest to diffidence, and sincere. The reserve which womanly
decorum threw over her manner was but too obviously artificial, and
when he said 'the ways of Providence are strange,' and added after
a moment, 'and merciful likewise,' she could not conceal her
agitation, and burst into tears upon his neck.
'But this is too soon,' she said, starting back.
'But no,' said he. 'You are eleven months gone in widowhood, and
it is not as if Sir John had been a good husband to you.'
His visits grew pretty frequent now, as may well be guessed, and
in a month or two he began to urge her to an early union. But she
counselled a little longer delay.
'Why?' said he. 'Surely I have waited long! Life is short; we are
getting older every day, and I am the last of the three.'
'Yes,' said the lady frankly. 'And that is why I would not have you
hasten. Our marriage may seem so strange to everybody, after my
unlucky remark on that occasion we know so well, and which so
many others know likewise, thanks to talebearers.'
On this representation he conceded a little space, for the sake of
her good name. But the destined day of their marriage at last
arrived, and it was a gay time for the villagers and all concerned,
and the bells in the parish church rang from noon till night. Thus at
last she was united to the man who had loved her the most tenderly
of them all, who but for his reticence might perhaps have been the
first to win her. Often did he say to himself; 'How wondrous that her
words should have been fulfilled! Many a truth hath been spoken in
jest, but never a more remarkable one!' The noble lady herself
preferred not to dwell on the coincidence, a certain shyness, if not
shame, crossing her fair face at any allusion thereto.
But people will have their say, sensitive souls or none, and their
sayings on this third occasion took a singular shape. 'Surely,' they
whispered, 'there is something more than chance in this . . . The
death of the first was possibly natural; but what of the death of the
second, who ill-used her, and whom, loving the third so desperately,
she must have wished out of the way?'
Then they pieced together sundry trivial incidents of Sir John's
illness, and dwelt upon the indubitable truth that he had grown
worse after her lover's unexpected visit; till a very sinister theory
was built up as to the hand she may have had in Sir John's
premature demise. But nothing of this suspicion was said openly, for
she was a lady of noble birth-nobler, indeed, than either of her
husbands-and what people suspected they feared to express in
formal accusation.
The mansion that she occupied had been left to her for so long a
time as she should choose to reside in it, and, having a regard for
the spot, she had coaxed Sir William to remain there. But in the end
it was unfortunate; for one day, when in the full tide of his
happiness, he was walking among the willows near the gardens,
where he overheard a conversation between some basket-makers
who were cutting the osiers for their use. In this fatal dialogue the
suspicions of the neighbouring townsfolk were revealed to him for
the first time.
'A cupboard close to his bed, and the key in her pocket. Ah!' said
one.
'And a blue phial therein-h'm!' said another.
'And spurge-laurel leaves among the hearth-ashes. Oh-oh!' said a
third.
On his return home Sir William seemed to have aged years. But he
said nothing; indeed, it was a thing impossible. And from that hour a
ghastly estrangement began. She could not understand it, and
simply waited. One day he said, however, 'I must go abroad.'
'Why?' said she. 'William, have I offended you?'
'No,' said he; 'but I must go.'
She could coax little more out of him, and in itself there was
nothing unnatural in his departure, for he had been a wanderer from
his youth. In a few days he started off, apparently quite another
man than he who had rushed to her side so devotedly a few months
before.
It is not known when, or how, the rumours, which were so thick in
the atmosphere around her, actually reached the Lady Penelope's
ears, but that they did reach her there is no doubt. It was impossible
that they should not; the district teemed with them; they rustled in
the air like night-birds of evil omen. Then a reason for her husband's
departure occurred to her appalled mind, and a loss of health
became quickly apparent. She dwindled thin in the face, and the
veins in her temples could all be distinctly traced. An inner fire
seemed to be withering her away. Her rings fell off her fingers, and
her arms hung like the flails of the threshers, though they had till
lately been so round and so elastic. She wrote to her husband
repeatedly, begging him to return to her; but he, being in extreme
and wretched doubt, moreover, knowing nothing of her ill-health,
and never suspecting that the rumours had reached her also,
deemed absence best, and postponed his return awhile, giving
various good reasons for his delay.
At length, however, when the Lady Penelope had given birth to a
still- born child, her mother, the Countess, addressed a letter to Sir
William, requesting him to come back to her if he wished to see her
alive; since she was wasting away of some mysterious disease,
which seemed to be rather mental than physical. It was evident that
his mother-in-law knew nothing of the secret, for she lived at a
distance; but Sir William promptly hastened home, and stood beside
the bed of his now dying wife.
'Believe me, William,' she said when they were alone, 'I am
innocent-innocent!'
'Of what?' said he. 'Heaven forbid that I should accuse you of
anything!'
'But you do accuse me-silently!' she gasped. 'I could not write
thereon-and ask you to hear me. It was too much, too degrading.
But would that I had been less proud! They suspect me of poisoning
him, William! But, oh my dear husband, I am innocent of that
wicked crime! He died naturally. I loved you-too soon; but that was
all!'
Nothing availed to save her. The worm had gnawed too far into
her heart before Sir William's return for anything to be remedial
now; and in a few weeks she breathed her last. After her death the
people spoke louder, and her conduct became a subject of public
discussion. A little later on, the physician, who had attended the late
Sir John, heard the rumour, and came down from the place near
London to which he latterly had retired, with the express purpose of
calling upon Sir William Hervy, now staying in Casterbridge.
He stated that, at the request of a relative of Sir John's, who
wished to be assured on the matter by reason of its suddenness, he
had, with the assistance of a surgeon, made a private examination
of Sir John's body immediately after his decease, and found that it
had resulted from purely natural causes. Nobody at this time had
breathed a suspicion of foul play, and therefore nothing was said
which might afterwards have established her innocence.
It being thus placed beyond doubt that this beautiful and noble
lady had been done to death by a vile scandal that was wholly
unfounded, her husband was stung with a dreadful remorse at the
share he had taken in her misfortunes, and left the country anew,
this time never to return alive. He survived her but a few years, and
his body was brought home and buried beside his wife's under the
tomb which is still visible in the parish church. Until lately there was
a good portrait of her, in weeds for her first husband, with a cross in
her hand, at the ancestral seat of her family, where she was much
pitied, as she deserved to be. Yet there were some severe enough to
say-and these not unjust persons in other respects-that though
unquestionably innocent of the crime imputed to her, she had shown
an unseemly wantonness in contracting three marriages in such
rapid succession; that the untrue suspicion might have been ordered
by Providence (who often works indirectly) as a punishment for her
self-indulgence. Upon that point I have no opinion to offer.
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