Conditional Statements in Shell

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to use conditional statements in shell programming to make logical decisions. We will cover the basic syntax of if-else statements, as well as how to use numeric and string comparisons to evaluate conditions. By the end of this lab, you will be able to write shell scripts that can make decisions based on various conditions.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL shell(("`Shell`")) -.-> shell/ControlFlowGroup(["`Control Flow`"]) linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux(("`Linux`")) -.-> linux/FileandDirectoryManagementGroup(["`File and Directory Management`"]) linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) shell(("`Shell`")) -.-> shell/BasicSyntaxandStructureGroup(["`Basic Syntax and Structure`"]) shell(("`Shell`")) -.-> shell/VariableHandlingGroup(["`Variable Handling`"]) shell/ControlFlowGroup -.-> shell/if_else("`If-Else Statements`") linux/BasicSystemCommandsGroup -.-> linux/echo("`Text Display`") linux/BasicSystemCommandsGroup -.-> linux/logical("`Logic Operations`") linux/FileandDirectoryManagementGroup -.-> linux/cd("`Directory Changing`") linux/BasicFileOperationsGroup -.-> linux/chmod("`Permission Modifying`") shell/BasicSyntaxandStructureGroup -.-> shell/shebang("`Shebang`") shell/BasicSyntaxandStructureGroup -.-> shell/comments("`Comments`") shell/VariableHandlingGroup -.-> shell/variables_decl("`Variable Declaration`") shell/VariableHandlingGroup -.-> shell/variables_usage("`Variable Usage`") subgraph Lab Skills shell/if_else -.-> lab-388815{{"`Conditional Statements in Shell`"}} linux/echo -.-> lab-388815{{"`Conditional Statements in Shell`"}} linux/logical -.-> lab-388815{{"`Conditional Statements in Shell`"}} linux/cd -.-> lab-388815{{"`Conditional Statements in Shell`"}} linux/chmod -.-> lab-388815{{"`Conditional Statements in Shell`"}} shell/shebang -.-> lab-388815{{"`Conditional Statements in Shell`"}} shell/comments -.-> lab-388815{{"`Conditional Statements in Shell`"}} shell/variables_decl -.-> lab-388815{{"`Conditional Statements in Shell`"}} shell/variables_usage -.-> lab-388815{{"`Conditional Statements in Shell`"}} end

Create Your First If Statement

Let's start by creating a simple if statement that checks if a variable called NAME is equal to "John".

First, open a terminal in the WebIDE. You should be in the /home/labex/project directory by default. If you're not sure, you can always check your current directory with the pwd command.

Create a new file called if.sh using the following command:

touch if.sh

This command creates an empty file named if.sh in your current directory.

Now, open the if.sh file in the WebIDE. You can do this by clicking on the file in the file explorer on the left side of the WebIDE.

Add the following content to the file:

#!/bin/bash

NAME="John"
if [ "$NAME" = "John" ]; then
  echo "The name is John"
fi

Let's break down this script:

  1. #!/bin/bash: This is called a "shebang" line. It tells the system which interpreter to use to run the script. In this case, we're using Bash.
  2. NAME="John": This line creates a variable called NAME and assigns it the value "John".
  3. if [ "$NAME" = "John" ]; then: This is the start of our if statement. It checks if the value of NAME is equal to "John".
    • The square brackets [ ] are actually a command in Bash, equivalent to the test command.
    • We put "$NAME" in quotes to handle cases where NAME might be empty or contain spaces.
    • The semicolon and then are part of the if statement syntax in Bash.
  4. echo "The name is John": This line will be executed if the condition is true.
  5. fi: This marks the end of the if statement. It's "if" spelled backwards!

Save the file after adding this content.

Now, we need to make the script executable. In Unix-like systems, files aren't executable by default for security reasons. We can change this using the chmod command:

chmod +x if.sh

This command adds the execute permission to the file. The +x means "add execute permission".

Now, run the script:

./if.sh

The ./ tells the shell to look for the script in the current directory.

You should see the output: The name is John

If you don't see this output, double-check that you've saved the file with the correct content and that you've made it executable.

Add an Else Clause

Now, let's expand our if statement to include an else clause. This allows us to specify what should happen when the condition is false.

Open the if.sh file in the WebIDE again and modify it as follows:

#!/bin/bash

NAME="Alice"
if [ "$NAME" = "John" ]; then
  echo "The name is John"
else
  echo "The name is not John"
fi

Let's go through the changes:

  1. We've changed the NAME variable to "Alice". This is to demonstrate what happens when the condition is false.
  2. We've added an else clause. This clause specifies what should happen if the condition in the if statement is false.
  3. After the else, we've added another echo command that will run if NAME is not "John".

The else clause is optional in if statements, but it's very useful when you want to do something specific when the condition is false, rather than just doing nothing.

Save the file with these changes.

Now, run the script again:

./if.sh

This time, you should see the output: The name is not John

This is because NAME is now "Alice", so the condition [ "$NAME" = "John" ] is false, and the code in the else block is executed.

Try changing the NAME variable back to "John" and run the script again. What output do you get? This is a good way to test that your if-else statement is working correctly for both cases.

Introducing Elif

Sometimes, we want to check multiple conditions. This is where the elif (else if) clause comes in handy. Let's modify our script to handle multiple names.

Update the if.sh file with the following content:

#!/bin/bash

NAME="George"
if [ "$NAME" = "John" ]; then
  echo "John Lennon"
elif [ "$NAME" = "Paul" ]; then
  echo "Paul McCartney"
elif [ "$NAME" = "George" ]; then
  echo "George Harrison"
elif [ "$NAME" = "Ringo" ]; then
  echo "Ringo Starr"
else
  echo "Unknown member"
fi

Let's break down this script:

  1. We start with NAME="George". This will be the name we're checking.
  2. The first if statement checks if the name is "John".
  3. If it's not "John", we move to the first elif (else if) statement, which checks if the name is "Paul".
  4. If it's not "Paul", we move to the next elif, checking for "George".
  5. If it's not "George", we check for "Ringo".
  6. If none of these conditions are true, we fall through to the else clause, which will echo "Unknown member".

The elif clause allows you to check multiple conditions in sequence. You can have as many elif clauses as you need. The conditions are checked in order, and the first one that's true will have its corresponding code block executed.

Save the file with these changes.

Now, run the script:

./if.sh

You should see the output: George Harrison

Try changing the NAME variable to different values ("John", "Paul", "Ringo", or something else entirely) and run the script each time. Observe how the output changes based on the value of NAME.

Numeric Comparisons

Shell scripts can also compare numbers. Let's create a new script to demonstrate numeric comparisons.

Create a new file called numeric.sh:

touch numeric.sh

Open numeric.sh in the WebIDE and add the following content:

#!/bin/bash

NUMBER=10

if [ $NUMBER -lt 5 ]; then
  echo "The number is less than 5"
elif [ $NUMBER -eq 10 ]; then
  echo "The number is exactly 10"
elif [ $NUMBER -gt 15 ]; then
  echo "The number is greater than 15"
else
  echo "The number is between 5 and 15, but not 10"
fi

This script introduces numeric comparison operators:

  • -lt: less than
  • -eq: equal to
  • -gt: greater than

There are also others you can use:

  • -le: less than or equal to
  • -ge: greater than or equal to
  • -ne: not equal to

Notice that we use these special operators instead of symbols like < or >. This is because in Bash, < and > are used for input/output redirection, not for numeric comparison.

Now, let's make the script executable and run it:

chmod +x numeric.sh
./numeric.sh

You should see the output: The number is exactly 10

Try changing the NUMBER variable to different values and run the script again. See how the output changes based on the value you set.

For example, if you change NUMBER to 20, you should get "The number is greater than 15". If you change it to 7, you should get "The number is between 5 and 15, but not 10".

String Comparisons and Logical Operators

Lastly, let's explore string comparisons and logical operators. Create a new file called string_logic.sh:

touch string_logic.sh

Open string_logic.sh in the WebIDE and add the following content:

#!/bin/bash

STRING1="hello"
STRING2="world"
NUMBER1=5
NUMBER2=10

if [ "$STRING1" = "hello" ] && [ "$STRING2" = "world" ]; then
  echo "Both strings match"
fi

if [ $NUMBER1 -lt 10 ] || [ $NUMBER2 -gt 5 ]; then
  echo "At least one of the number conditions is true"
fi

if [[ "$STRING1" != "$STRING2" ]]; then
  echo "The strings are different"
fi

if [[ -z "$STRING3" ]]; then
  echo "STRING3 is empty or not set"
fi

This script demonstrates several new concepts:

  1. String equality comparison (=): This checks if two strings are exactly the same.
  2. Logical AND (&&): This operator allows you to combine two conditions. Both conditions must be true for the overall expression to be true.
  3. Logical OR (||): This operator also combines two conditions, but only one needs to be true for the overall expression to be true.
  4. String inequality comparison (!=): This checks if two strings are different.
  5. Checking if a string is empty (-z): This is true if the string is empty (has zero length).

Also, notice the use of double square brackets [[ ]] in some of the if statements. These are an enhanced version of the single square brackets and are preferred in Bash scripts when possible. They allow for more complex expressions and have fewer surprises with regard to word splitting and pathname expansion.

Make the script executable and run it:

chmod +x string_logic.sh
./string_logic.sh

You should see all four echo statements printed, because all the conditions in the script are true.

Both strings match
At least one of the number conditions is true
The strings are different
STRING3 is empty or not se

Try changing some of the values (like setting STRING1 to something other than "hello") and see how it affects the output.

Summary

In this lab, you have learned how to use conditional statements in shell programming. You have practiced using if-else statements, elif clauses, numeric comparisons, string comparisons, and logical operators. These tools allow you to create more complex and decision-based shell scripts.

Key takeaways:

  • The basic structure of if-else statements in shell scripts
  • How to use elif for multiple conditions
  • Numeric comparison operators (-lt, -gt, -eq, etc.)
  • String comparison and logical operators
  • The importance of making scripts executable with chmod

Remember, practice is key to becoming proficient in shell scripting. Try creating your own scripts that use these concepts in different ways. As you advance, you'll find these conditional statements essential for creating more sophisticated and useful scripts.

Other Linux Tutorials you may like