Book Image

Mastering Linux Shell Scripting

By : Andrew Mallett
Book Image

Mastering Linux Shell Scripting

By: Andrew Mallett

Overview of this book

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.
Table of Contents (21 chapters)
Mastering Linux Shell Scripting
About the Author
About the Reviewer


One of the most important and time consuming tasks in scripting is building all of the conditional statements that we need to make the script usable and robust. There is an 80-20 rule that is often spoken of. This is where 20 percent of your time is spent in writing the main script and 80 percent of the time is spent to ensure all of the possible eventualities that are correctly handled in the script. This is what I refer to as the procedural integrity of the script, where we try to cover each scenario carefully and accurately.

We started by looking at a simple test with command-line lists. If the actions needed are simple, then these provide great functionality and are easily added. Where more complexity is required, we will add if statements.

Using the if statements, we can extend them as required using the else and elif keywords. Don't forget that elif keywords need their own conditions to evaluate.

Finally, we saw how we can use case where a single expression needs to be evaluated...