Professional Documents
Culture Documents
PDF Mastering Linux Shell Scripting A Practical Guide To Linux Command Line Bash Scripting and Shell Programming Ebrahim Ebook Full Chapter
PDF Mastering Linux Shell Scripting A Practical Guide To Linux Command Line Bash Scripting and Shell Programming Ebrahim Ebook Full Chapter
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.
Language: English
Machinist”
BY
CHARLES T. PORTER
Honorary Member of The American Society of Mechanical Engineers
A u t h o r o f “ A Tr e a t i s e o n t h e R i c h a r d s S t e a m - e n g i n e I n d i c a t o r
and the Development and Application of Force in the
Steam-engine,” 1874; “Mechanics and Faith,” 1885
FIRST THOUSAND
NEW YORK
JOHN WILEY & SONS
London: CHAPMAN & HALL, Limited
1908
Copyright 1908
BY
CHARLES T. PORTER
THIS BOOK IS DEDICATED
TO THE MEMORY OF
CHAPTER I
PAGE
Birth, Parentage and Education. Experience in the Practice
of Law. Introduction to Centrifugal Force. Invention and
Operation of a Stone-dressing Machine 1
CHAPTER II
The Evolution and Manufacture of the Central Counterpoise
Governor. Introduction of Mr. Richards 17
CHAPTER III
Invention and Application of my Marine Governor 34
CHAPTER IV
Engineering Conditions in 1860. I meet Mr. Allen. Mr. Allen’s
Inventions. Analysis of the Allen Link 42
CHAPTER V
Invention of the Richards Indicator. My Purchase of the
Patent. Plan my London Exhibition. Engine Design. Ship
Engine Bed to London, and sail myself 58
CHAPTER VI
Arrival in London. Conditions I found there. Preparations 65
and Start
CHAPTER VII
My London Exhibit, its Success, but what was the matter?
Remarkable Sale of the Engine 71
CHAPTER VIII
Sale of Governors. Visit from Mr. Allen. Operation of the
Engine Sold to Easton, Amos & Sons. Manufacture of the
Indicator. Application on Locomotives 80
CHAPTER IX
Designs of Horizontal Engine Beds. Engine Details.
Presentation of the Indicator at the Newcastle Meeting of
the British Association for the Advancement of Science 93
CHAPTER X
Contract with Ormerod, Grierson & Co. Engine for Evan
Leigh, Son & Co. Engine for the Oporto Exhibition.
Getting Home from Portugal 101
CHAPTER XI
Trouble with the Evan Leigh Engine. Gear Patterns from the
Whitworth Works. First Order for a Governor. Introduction
of the Governor into Cotton Mills. Invention of my
Condenser. Failure of Ormerod, Grierson & Co. 113
CHAPTER XII
Introduction to the Whitworth Works. Sketch of Mr.
Whitworth. Experience in the Whitworth Works. Our
Agreement Which was never Executed. First Engine in
England Transmitting Power by a Belt 122
CHAPTER XIII
The French Exposition of 1867. Final Break with Mr.
Whitworth 139
CHAPTER XIV
Study of the Action of Reciprocating Parts. Important Help
from Mr. Frederick J. Slade. Paper before Institution of
Mechanical Engineers. Appreciation of Zerah Colburn.
The Steam Fire Engine in England 153
CHAPTER XV
Preparations for Returning to America. Bright Prospects 165
CHAPTER XVI
Return to America. Disappointment. My Shop. The Colt
Armory Engine Designed by Mr. Richards. Appearance of
Mr. Goodfellow. My Surface Plate Work. Formation of a
Company 173
CHAPTER XVII
Mr. Allen’s Invention of his Boiler. Exhibition at the Fair of
the American Institute in 1870 190
CHAPTER XVIII
Demonstration to the Judges of Action of Reciprocating
Parts. Explanation of this Action. Mr. Williams’ Instrument
for Exhibiting this Action 198
CHAPTER XIX
Boiler Tests in Exhibition of 1871. We Lose Mr. Allen. 208
Importance of Having a Business Man as President.
Devotion of Mr. Hope
CHAPTER XX
Close of the Engine Manufacture in Harlem. My Occupation
During a Three Years’ Suspension 219
CHAPTER XXI
Production of an Original Surface Plate 233
CHAPTER XXII
Efforts to Resume the Manufacture. I Exhibit the Engine to
Mr. Holley. Contract with Mr. Phillips. Sale of Engine to
Mr. Peters 238
CHAPTER XXIII
Experience as Member of the Board of Judges at the
Philadelphia Centennial Exhibition 245
CHAPTER XXIV
Engine Building in Newark. Introduction of Harris Tabor 259
CHAPTER XXV
Engine for the Cambria Iron and Steel Company 271
CHAPTER XXVI
My Downward Progress 275
CHAPTER XXVII
My Last Connection with the Company 325
CHAPTER XXVIII
The Fall and Rise of the Southwark Foundry and Machine
Company. Popular Appreciation of the High-speed Engine 331