Chapter 27. Flow Control: Branching with if

In the last chapter, we were presented with a problem. How can we make our report-generator script adapt to the privileges of the user running the script? The solution to this problem will require us to find a way to “change directions” within our script, based on the results of a test. In programming terms, we need the program to branch.

Let’s consider a simple example of logic expressed in pseudocode, a simulation of a computer language intended for human consumption:

X = 5

If X = 5, then:

Say “X equals 5.”

Otherwise:

Say “X is not equal to 5.”

This is an example of a branch. Based on the condition “Does X = 5?” do one thing: “Say ’X equals 5.’” Otherwise do another thing: “Say ’X is not equal to 5.’”

Using ...

Get The Linux Command Line now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.