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)

Verifying user input with lists

In this script, we will ensure that a value has been supplied to the first positional parameter. We can modify the hello2.sh script that we created in Chapter 1, The What and Why of Scripting with Bash, to check for user input before displaying the hello text.

You can copy the hello2.sh script to hello4.sh, or simply create a new script from scratch. There will not be a lot of typing and the script will be created as $HOME/bin/hello4.sh, as shown:

We can ensure that the script is executable by using the following command:

$ chmod +x $HOME/bin/hello4.sh  

We can then run the script with or without arguments. The test statement is looking for the $1 variable to be zero bytes. If it is, then we will not see the hello statement; otherwise, it will print the hello message. In simple terms, we will see the hello message if we supply a name.

The following...