Mastering Linux Shell Scripting
eBook - ePub

Mastering Linux Shell Scripting

  1. 198 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Mastering Linux Shell Scripting

Book details
Book preview
Table of contents
Citations

About This Book

Master the complexities of Bash shell scripting and unlock the power of shell for your enterprise

About This Book

  • Identify the high level steps such as verifying user input, using command lines and conditional statements in creating and executing simple shell scripts
  • Create and edit dynamic shell scripts to manage complex and repetitive tasks
  • Learn about scripting in Perl and programming in Python as a BASH scripting alternative with this practical, step-by-step guide

Who This Book Is For

Mastering Linux Shell Scripting has been written for Linux administrators who want to automate tasks in their daily lives, saving time and effort. You'll need to have command-line experience and be familiar with the tasks that you need to automate.

What You Will Learn

  • Use the type command to identify the order of command evaluation
  • Create interactive scripts that prompt for user input
  • Foster menu structures for operators with little command-line experience
  • Develop scripts that dynamically edit web configuration files to produce a new virtual host
  • Write scripts that use AWK to search and reports on log files
  • Draft effective scripts using functions as building blocks, reducing maintenance and build time
  • Make informed choices by comparing different script languages such as Perl and Python with BASH

In Detail

Shell scripting is a quick method to prototype a complex application or a problem by automating tasks when working on Linux-based systems. Using both simple one-line commands and command sequences complex problems can be solved with ease, from text processing to backing up sysadmin tools.

In this book, you'll discover everything you need to know to master shell scripting and make informed choices about the elements you employ. Get to grips with the fundamentals of creating and running a script in normal mode, and in debug mode. Learn about various conditional statements' code snippets, and realize the power of repetition and loops in your shell script. Implement functions and edit files using the Stream Editor, script in Perl, program in Python – as well as complete coverage of other scripting languages to ensure you can choose the best tool for your project.

Style and approach

The book will capture your attention and keep you engaged with the simplicity and clarity of each explanation. Every step is accompanied with screen captures so you can cross-check the results before moving on.

Frequently asked questions

Simply head over to the account section in settings and click on “Cancel Subscription” - it’s as simple as that. After you cancel, your membership will stay active for the remainder of the time you’ve paid for. Learn more here.
At the moment all of our mobile-responsive ePub books are available to download via the app. Most of our PDFs are also available to download and we're working on making the final remaining ones downloadable now. Learn more here.
Both plans give you full access to the library and all of Perlego’s features. The only differences are the price and subscription period: With the annual plan you’ll save around 30% compared to 12 months on the monthly plan.
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 1000+ topics, we’ve got you covered! Learn more here.
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more here.
Yes, you can access Mastering Linux Shell Scripting by Andrew Mallett in PDF and/or ePUB format, as well as other popular books in Computer Science & Operating Systems. We have over one million books available in our catalogue for you to explore.

Information

Year
2015
ISBN
9781784396978
Edition
1

Mastering Linux Shell Scripting


Table of Contents

Mastering Linux Shell Scripting
Credits
About the Author
About the Reviewer
www.PacktPub.com
Support files, eBooks, discount offers, and more
Why subscribe?
Free access for Packt account holders
Preface
What this book covers
What you need for this book
Who this book is for
Conventions
Reader feedback
Customer support
Downloading the example code
Downloading the color images of this book
Errata
Piracy
Questions
1. What and Why of Scripting with Bash
Bash vulnerabilities
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
Debugging your scripts
Summary
2. Creating Interactive Scripts
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 entered text
Enhancing learning with simple scripts
Backing-up with scripts
Connecting to a server
Version 1 – ping
Version 2 – SSH
Version 3 – MySQL/MariaDB
Summary
3. Conditions Attached
Simple decision paths using command-line lists
Verifying user input with lists
Using the test shell builtin
Testing strings
Testing integers
Testing file types
Creating conditional statements using if
Extending if with else
More conditions with elif
Creating the backup2.sh using elif
Using case statements
Script – building a front-end with grep
Summary
4. Creating Code Snippets
Abbreviations
Using code snippets
Bringing color to the terminal
Summary
5. Alternative Syntax
Recapping test
Testing files
Adding logic
Square brackets as not seen before
Providing parameter defaults
Variables
Special parameters
Setting defaults
When in doubt – Quote!
Advanced test using [[
Whitespace
Other advanced features
Pattern matching
Regular expressions
Regular expression script
Arithmetic operations using ((
Simple math
Parameter manipulation
Standard arithmetic tests
Summary
6. Iterating with Loops
For loops
Controlling the loop
While loops and until loops
Reading input from files
Creating operator menus
Summary
7. Creating Building Blocks with Functions
Introducing functions
Passing parameters to functions
Returning values from functions
Using functions in menus
Summary
8. Introducing sed
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
Using regular expressions
Working with alternate spellings
How many words have four consecutive vowels?
RE anchors
Understanding the basics of sed
Substituting command
Editing the file
Summary
9. Automating Apache Virtual Hosts
Apache name-based Virtual Hosts
Creating the Virtual Hosts template
First steps
Isolating lines
sed script files
Automating Virtual Host creation
Prompting for data during site creation
Summary
10. Awk Fundamentals
The history behind awk
Displaying and filtering content from files
Formatting output
Further filtering to display users by UID
Awk control files
Summary
11. Summarizing Logs with Awk
The HTTPD log file format
Displaying data from web logs
Selecting entries by date
Summarizing 404 errors
Summarizing HTTP access codes
Displaying the highest ranking IP address
Displaying the browser data
Working with e-mail logs
Summary
12. A Better lastlog with Awk
Using awk ranges to exclude data
The lastlog command
Horizontal 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
13. Using Perl as a Bash Scripting Alternative
What is Perl?
Hello World
Perl arrays
Program name?
Argument arrays
Counting elements in an array
Looping through an array
Creating arrays
Conditional statements in Perl
Replacing command line lists
If and unless
Using functions within Perl
Prompt for user input
Creating the function
Summary
14. Using Python as a Bash Scripting Alternative
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
Summary
Index

Mastering Linux Shell Scripting

Copyright © 2015 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 author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirec...

Table of contents

  1. Mastering Linux Shell Scripting