Book Image

Linux Shell Scripting Bootcamp

By : James K Lewis
Book Image

Linux Shell Scripting Bootcamp

By: James K Lewis

Overview of this book

Linux Shell Scripting Bootcamp is all about learning the essentials of script creation, validating parameters, and checking for the existence of files and other items needed by the script. We will use scripts to explore iterative operations using loops and learn different types of loop statements, with their differences. Along with this, we will also create a numbered backup script for backup files. Further, you will get well-versed with how variables work on a Linux system and how they relate to scripts. You’ll also learn how to create and call subroutines in a script and create interactive scripts. The most important archive commands, zip and tar, are also discussed for performing backups. Later, you will dive deeper by understanding the use of wget and curl scripts and the use of checksum and file encryption in further chapters. Finally, you will learn how to debug scripts and scripting best practices that will enable you to write a great code every time! By the end of the book, you will be able to write shell scripts that can dig data from the web and process it efficiently.
Table of Contents (19 chapters)
Linux Shell Scripting Bootcamp
Credits
About the Author
Acknowledgement
About the Reviewer
www.PacktPub.com
Customer Feedback
Preface
Index

Validating parameters using conditional statements


Variables can be tested and compared against other variables when using a variable as a number.

Here is a list of some of the operators that can be used:

Operator

Description

-eq

This stands for equal to

-ne

This stands for not equal to

-gt

This stands for greater than

-lt

This stands for less than

-ge

This stands for greater than or equal to

-le

This stands for less than or equal to

!

This stands for the negation operator

Let's take a look at this in our next example script:

Chapter 2 - Script 2

#!/bin/sh
#
# 6/13/2017
#
echo "script2"

# Numeric variables
a=100
b=100
c=200
d=300

echo a=$a b=$b c=$c d=$d     # display the values

# Conditional tests
if [ $a -eq $b ] ; then
 echo a equals b
fi

if [ $a -ne $b ] ; then
 echo a does not equal b
fi

if [ $a -gt $c ] ; then
 echo a is greater than c
fi

if [ $a -lt $c ] ; then
 echo a is less than c
fi

if [ $a -ge $d ] ; then
 echo a is greater than or equal to d...