Book Image

Mastering Linux Shell Scripting - Second Edition

By : Mokhtar Ebrahim, Andrew Mallett
5 (1)
Book Image

Mastering Linux Shell Scripting - Second Edition

5 (1)
By: Mokhtar Ebrahim, Andrew Mallett

Overview of this book

In this book, you’ll discover everything you need to know to master shell scripting and make informed choices about the elements you employ. Grab your favorite editor and start writing your best Bash scripts step by step. 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. You will also learn to write complex shell scripts. This book will also deep dive into file system administration, directories, and system administration like networking, process management, user authentications, and package installation and regular expressions. Towards the end of the book, you will learn how to use Python as a BASH Scripting alternative. By the end of this book, you will know shell scripts at the snap of your fingers and will be able to automate and communicate with your system with keyboard expressions.
Table of Contents (17 chapters)

Advanced for loops

In the previous examples, we used the for loop to iterate over simple values where each value has no space.

As you know, if your values contain a space, you should use double quotes:

#!/bin/bash 
for var in one "This is two" "Now three" "We'll check four" 
do 
echo "Value: $var" 
done 

As you can see, each value is printed as expected thanks to double quotes.

This example contains values in one line and we quote the values because they have spaces and commas. What if the values were on multiple lines, as in a file?

What if the separator between the values we want to iterate over is something other than a space such as a comma or a semicolon?

Here comes the IFS.