Chapter 7. Conditional statements

Table of Contents

1. Introduction to if
1.1. General
1.2. Simple applications of if
2. More advanced if usage
2.1. if/then/else constructs
2.2. if/then/elif/else constructs
2.3. Nested if statements
2.4. Boolean operations
2.5. Using the exit statement and if
3. Using case statements
3.1. Simplified conditions
3.2. Initscript example
4. Summary
5. Exercises

Abstract

In this chapter we will discuss the use of conditionals in Bash scripts. This includes the following topics:

  • The if statement

  • Using the exit status of a command

  • Comparing and testing input and files

  • if/then/else constructs

  • if/then/elif/else constructs

  • Using and testing the positional parameters

  • Nested if statements

  • Boolean expressions

  • Using case statements