Download as pdf or txt
Download as pdf or txt
You are on page 1of 54

Mastering Linux Shell Scripting a

practical guide to Linux command line


Bash scripting and Shell programming
Ebrahim
Visit to download the full and correct content document:
https://textbookfull.com/product/mastering-linux-shell-scripting-a-practical-guide-to-lin
ux-command-line-bash-scripting-and-shell-programming-ebrahim/
More products digital (pdf, epub, mobi) instant
download maybe you interests ...

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/mastering-linux-shell-scripting-
a-practical-guide-to-linux-command-line-bash-scripting-and-shell-
programming-second-edition-edition-mokhtar-ebrahim/

Linux Shell Scripting Cookbook - Third Edition Clif


Flynt

https://textbookfull.com/product/linux-shell-scripting-cookbook-
third-edition-clif-flynt/

BASH SCRIPT Linux Command Line Reference 1st Edition


Aditya Tiwari

https://textbookfull.com/product/bash-script-linux-command-line-
reference-1st-edition-aditya-tiwari/

Shell Programming in Unix Linux and OS X Stephen G.


Kochan

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/

Linux Basics for Hackers Getting Started with


Networking Scripting and Security in Kali Occupytheweb

https://textbookfull.com/product/linux-basics-for-hackers-
getting-started-with-networking-scripting-and-security-in-kali-
occupytheweb/

Wicked Cool Shell Scripts 101 Scripts for Linux OS X


and UNIX Systems Dave Taylor

https://textbookfull.com/product/wicked-cool-shell-
scripts-101-scripts-for-linux-os-x-and-unix-systems-dave-taylor/

Computing with the Raspberry Pi: Command Line and GUI


Linux 1st Edition Brian Schell

https://textbookfull.com/product/computing-with-the-raspberry-pi-
command-line-and-gui-linux-1st-edition-brian-schell/

Linux Basics for Hackers Getting Started with


Networking Scripting and Security in Kali 1st Edition
Occupytheweb

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

A practical guide to Linux command-line, Bash scripting, and Shell


programming

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.

Commissioning Editor: Vijin Boricha


Acquisition Editor: Rohit Rajkumar
Content Development Editor: Ron Mathew
Technical Editor: Prachi Sawant
Copy Editor: Safis Editing
Project Coordinator: Judie Jose
Proofreader: Safis Editing
Indexer: Mariammal Chettiyar
Graphics: Tom Scaria
Production Coordinator: Aparna Bhagat

First published: December 2015


Second edition: April 2018

Production reference: 1180418

Published by Packt Publishing Ltd.


Livery Place
35 Livery Street
Birmingham
B3 2PB, UK.

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

Get a free eBook or video every month

Mapt is fully searchable

Copy and paste, print, and bookmark content


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 www.PacktPub.com and as a print book customer, you are
entitled to a discount on the eBook copy. Get in touch with us at
service@packtpub.com for more details.

At www.PacktPub.com, 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
About the authors
Mokhtar Ebrahim started working as a Linux system administrator
in 2010. He is responsible for maintaining, securing, and
troubleshooting Linux servers for multiple clients around the world.
He loves writing shell and Python scripts to automate his work. He
writes technical articles on the Like Geeks website about Linux,
Python, web development, and server administration. He is a father
to a beautiful girl and a husband to a faithful wife.
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!
Packt is searching for authors
like you
If you're interested in becoming an author for Packt, please visit autho
rs.packtpub.com and apply today. We have worked with thousands of

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

Copyright and Credits

Mastering Linux Shell Scripting Second Edition

Packt Upsell

Why subscribe?

PacktPub.com

Contributors

About the authors

About the reviewer

Packt is searching for authors like you


Preface

Who this book is for

What this book covers

To get the most out of this book

Download the example code files

Download the color images

Conventions used

Get in touch

Reviews

1. The What and Why of Scripting with Bash

Technical requirements

Types of Linux shells

What is bash scripting?

The bash command hierarchy


Command type

Command PATH

Preparing text editors for scripting

Configuring vim

Configuring nano

Configuring gedit

Creating and executing scripts


Hello World!

Executing the script

Checking the exit status

Ensuring a unique name

Hello Dolly!

Running the script with arguments

The importance of correct quotes

Printing the script name

Declaring variables

User-defined variables

Environment variables

Variable scope

Command substitution

Debugging your scripts

Summary

Questions

Further reading

2. Creating Interactive Scripts

Technical requirements

Using echo with options

Basic script using read

Script comments

Enhancing scripts with read prompts


Limiting the number of entered characters

Controlling the visibility of the entered text


Passing options

Passing parameters with options


Read options values

Try to be standard
Enhancing learning with simple scripts

Backing-up with scripts


Connecting to a server

Version 1 – ping


Version 2 – SSH

Version 3 – MySQL/MariaDB


Reading files
Summary
Questions

Further reading
3. Conditions Attached

Technical requirements
Simple decision paths using command-line lists

Verifying user input with lists


Using the test shell built-in

Testing strings
Testing integers

Testing file types


Creating conditional statements using if

Extending if with else


Test command with the if command

Checking strings
Checking files and directories

Checking numbers
Combining tests

More conditions with elif


Creating the backup2.sh using elif

Using case statements


Recipe – building a frontend with grep

Summary
Questions

Further reading
4. Creating Code Snippets

Technical requirements
Abbreviations

Using code snippets


Bringing color to the Terminal

Creating snippets using VS Code


Summary

Questions
Further reading

5. Alternative Syntax
Technical requirement
Recapping the test command
Testing files

Adding logic
Square brackets as not seen before

Providing parameter defaults


Variables
Special parameters

Setting defaults
When in doubt – quote!
Advanced tests using [[
White space

Other advanced features


Pattern matching
Regular expressions
Regular expression script

Arithmetic operations using ((


Simple math
Parameter manipulation
Standard arithmetic tests

Summary
Questions
Further reading
6. Iterating with Loops

Technical requirement
for loops
Advanced for loops
The IFS

Counting directories and files


C-style for loops 
Nested loops
Redirecting loop output

Controlling the loop


while loops and until loops
Reading input from files
Creating operator menus

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

Using functions in menus


Summary
Questions
Further reading

8. Introducing the Stream Editor


Technical requirements
Using grep to display text
Displaying received data on an interface

Displaying user account data


Listing the number of CPUs in a system
Parsing CSV files
The CSV file

Isolating catalog entries


Understanding the basics of sed
The substitute command
Global replacement

Limiting substitution
Editing the file
Other sed commands
The delete command

The insert and append commands


The change command
The transform command
Multiple sed commands

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

11. Regular Expressions


Technical requirements
Regular expression engines
Defining BRE patterns

Anchor characters
The dot character
The character class
Ranges of characters

Special character classes


The asterisk
Defining ERE patterns
The question mark

The plus sign


Curly braces
The pipe character
Expression grouping

Using grep
Summary

Questions

Further reading
12. Summarizing Logs with AWK

Technical requirements

The HTTPD log file format


Displaying data from web logs

Selecting entries by date


Summarizing 404 errors

Summarizing HTTP access codes

Resources hits
Identify image hotlinking

Displaying the highest ranking IP address

Displaying the browser data


Working with email logs

Summary
Questions

Further reading

13. A Better lastlog with AWK


Technical requirements

Using AWK ranges to exclude data

The lastlog command


Horizontally filtering rows with AWK

Counting matched rows


Conditions based on the number of fields
Manipulating the AWK record separator to report on XML data

Apache Virtual Hosts


XML catalog

Summary

Questions
Further reading

14. Using Python as a Bash Scripting Alternative


Technical requirements

What is Python?

Saying Hello World the Python way


Pythonic arguments

Supplying arguments

Counting arguments
Significant whitespace

Reading user input


Using Python to write to files

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

Other Books You May Enjoy


Leave a review - let other readers know what you think
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.
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.

, Creating Interactive Scripts, covers how to read input from


Chapter 2

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.

, Conditions Attached, will introduce the


Chapter 3 if statement, the case

statement, and other testing command such as else and elif.

, Creating Code Snippets, covers creating and using code


Chapter 4

snippets using editors, such as vim and Visual Studio Code.

, Alternative Syntax, will discuss advanced testing using


Chapter 5 [[

and how to perform arithmetic operations.

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.

, Creating Building Blocks with Functions, will introduce


Chapter 7

functions and explains how to create a function, list builtin functions,


pass parameters to functions, and writing recursive functions.

, Introducing the Stream Editor, will introduce the basics of


Chapter 8

sed tool to manipulate files, such as adding, replacing deleting, and


transforming text.
, Automating Apache Virtual Hosts, contains a practical
Chapter 9

example of sed and explains how to create virtual hosts


automatically using sed.

, AWK Fundamentals, will discuss AWK and how to filter file


Chapter 10

content using it. Also, we will discuss some AWK programming


basics.

, Regular Expressions, covers regular expressions, their


Chapter 11

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

to output beautiful reports using the lastlog command by filtering


and processing the lastlog output.

, Using Python as a Bash Scripting Alternative, will discuss


Chapter 14

Python programming language basics and explains how to write


some Python scripts as a bash script alternative.
To get the most out of this
book
I assume that you have a little programming background. Even if
you don't have a programming background, the book will start from
the beginning.

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.

You can download the code files by following these steps:

1. Log in or register at www.packtpub.com.


2. Select the SUPPORT tab.
3. Click on Code Downloads & Errata.
4. Enter the name of the book in the Search box and follow the
onscreen instructions.

Once the file is downloaded, please make sure that you unzip or
extract the folder using the latest version of:

WinRAR/7-Zip for Windows


Zipeg/iZip/UnRarX for Mac
7-Zip/PeaZip for Linux

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

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 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.

CodeInText: 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 $HOME/bin/hello2.sh"

A block of code is set as follows:


if [ $file_compression = "L" ] ; then
tar_opt=$tar_l
elif [ $file_compression = "M" ]; then
tar_opt=$tar_m
else
tar_opt=$tar_h
fi

Any command-line input or output is written as follows:


$ type ls
ls is aliased to 'ls --color=auto'

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"
Warnings or important notes appear like this.

Tips and tricks appear like this.


Another random document with
no related content on Scribd:
The Project Gutenberg eBook of Engineering
reminiscences contributed to "Power" and
"American machinist"
This ebook is for the use of anyone anywhere in the United
States and most other parts of the world at no cost and with
almost no restrictions whatsoever. You may copy it, give it away
or re-use it under the terms of the Project Gutenberg License
included with this ebook or online at www.gutenberg.org. If you
are not located in the United States, you will have to check the
laws of the country where you are located before using this
eBook.

Title: Engineering reminiscences contributed to "Power" and


"American machinist"

Author: Charles T. Porter

Release date: November 5, 2023 [eBook #72043]

Language: English

Original publication: New York: John Wiley & Sons, 1908

Credits: deaurider, Harry Lamé and the Online Distributed


Proofreading Team at https://www.pgdp.net (This file
was produced from images generously made available
by The Internet Archive)

*** START OF THE PROJECT GUTENBERG EBOOK


ENGINEERING REMINISCENCES CONTRIBUTED TO "POWER"
AND "AMERICAN MACHINIST" ***
Please see the Transcriber’s Notes
at the end of this text.
New original cover art included with
this eBook is granted to the public
domain.
Faithfully yours
Charles T. Porter
Engineering Reminiscences
CONTRIBUTED TO

“Power” and “American

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

REVISED AND ENLARGED


FIRST EDITION

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

MY FATHER AND MOTHER


My Father
My Mother
PREFACE
A word of explanation seems due to both the reader and myself.
The idea of writing these reminiscences did not originate with me. I
was invited to write them by Mr. F. R. Low, the editor of Power. This
invitation I declined, saying that I felt averse to writing a story in
which I must be the central figure. Mr. Low replied that I should
regard it as a duty I owed to the profession. Engineers demanded to
know the origin and early development of the high speed system of
steam engineering. I was the only person who could meet this
demand; no one else possessed the necessary information.
I felt obliged to yield to this view, and can only ask the reader to
imagine that I am writing about somebody else.
C. T. P.
Montclair, N. J.,
December, 1907.
TABLE OF CONTENTS

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

You might also like