Shell Scripts for Beginners

Flow Control

Conditional Logic

In this article, you'll learn how to implement conditional logic in shell scripts. Using a practical example, we simulate a scenario where a script checks a rocket’s status. The script handles three possible responses from a command:

  • "launching" when the launch is in progress
  • "success" when the launch completes successfully
  • "failed" when the rocket crashes

If the rocket launch fails, the script automatically runs the debugging command. This tutorial will help you understand how to structure your conditional statements to control the script's flow based on various conditions.

How Conditional Statements Work

Shell scripting utilizes the if statement similarly to natural language. The condition is placed inside square brackets, and if it evaluates to true, the subsequent commands are executed. The structure includes the if keyword, followed by a condition, the then statement, and finally the closing fi to mark the end of the conditional block.

Before entering the conditional block, the script captures the output of the rocket status command in the variable rocket_status. Based on its value, the script decides whether to trigger additional commands, such as running a debug command.

Note

Ensure there is at least one space between the square brackets [ ] and the condition when using conditional statements.

Example: Using if, elif, and else Blocks

Below is an example demonstrating how to implement conditional logic in a shell script:

# Command examples
$ rocket-status lunar-mission
launching    success     failed


$ rocket-debug lunar-mission
overheating


# Script snippet
mission_name=$1
rocket_status=$(rocket-status $mission_name)


if [ "$rocket_status" = "failed" ]; then
    rocket-debug $mission_name
elif [ "$rocket_status" = "success" ]; then
    echo "This is successful"
else
    echo "The state is not failed or success"
fi

In this snippet, the status is checked against predefined conditions. If rocket_status equals "failed," it triggers the debugging command. For a "success" status, it displays a confirmation message. Any other response results in a default message.

String Comparisons and Conditional Operators

In shell scripts, string comparison uses the equals operator (=) for equality and the not-equals operator (!=) for inequality. Make sure the condition is placed inside square brackets with proper spacing.

The image shows a table explaining conditional operators with examples and descriptions, including string and number comparisons for equality and inequality.

Numeric Comparisons

For numeric values, different operators are employed:

  • Use -eq to check if two numbers are equal.
  • Use -ne to verify that numbers are not equal.
  • Use -gt for "greater than."
  • Use -lt for "less than."

An extended version using double square brackets [[ ]] provides additional functionality such as pattern matching, which is a Bash extension and might not be available in all shells.

Pattern Matching Example

To check if the string "ABC" contains the substring "BC", you can use pattern matching with asterisks (*) outside double quotes:

[[ "ABC" == *BC* ]]

The image explains conditional operators in Bash, showing examples and descriptions of string comparisons and pattern matching.

Sorting and Logical Operators

Alphabetical comparisons use sort order operators. For example, "ABC" comes before "BCD" when sorted alphabetically, and operators reflect that order during comparisons.

You can combine multiple conditions using logical operators:

  • The AND operator (&&) ensures both conditions are true.

    [ COND1 ] && [ COND2 ]
    
  • The OR operator (||) checks if at least one condition is true.

    [ COND1 ] || [ COND2 ]
    

When using double square brackets, you can include the entire condition with logical operators in one pair:

[[ A -gt 4 && A -lt 10 ]]
[[ A -gt 4 || A -lt 10 ]]

File-Level Conditional Operators

Shell scripts also allow file-level checks. Some common file operators are:

OperatorPurposeExample
-eCheck if a file exists[ -e filename ]
-dCheck if a path is a directory[ -d /path/to/directory ]
-sCheck if a file is not empty[ -s filename ]
-xCheck if a file is executable[ -x filename ]
-wCheck if a file is writable[ -w filename ]

The image shows a table of conditional operators for files, detailing checks for existence, directory status, size, executability, and writability.

Warning

Be cautious with file operators and always verify that the file or directory you are checking has the correct permissions to avoid unexpected behavior.

Conclusion

By mastering conditional logic in shell scripts, you can effectively control the execution flow of your scripts based on dynamic conditions. Practice using these statements to further deepen your understanding of shell scripting and improve automation in your projects.

For additional learning, consider exploring resources like Shell Scripting Fundamentals and Advanced Bash-Scripting Guide.

Watch Video

Watch video content

Practice Lab

Practice lab

Previous
Arithmetic Operations