Condition Implementation

Tutorial 1 of 4

Introduction

In this tutorial, we aim to understand how to implement conditions in shell scripting. Conditions are a crucial part of any programming language as they allow us to control the flow of our programs and make decisions based on certain criteria.

By the end of this tutorial, you will:
- Understand the concept of conditions in shell scripting.
- Learn how to use condition statements.
- Be able to write simple shell scripts utilizing conditions.

Prerequisites: Basic understanding of shell scripting.

Step-by-Step Guide

Conditions in shell scripting are implemented using various conditional statements. These include 'if', 'if..else', 'if..elif..else', 'case' statements.

An 'if' statement is the most basic type of conditional statement. It checks if a condition is true. If it's true, it executes some code. If not, it skips the code and moves on.

The syntax for an 'if' statement in shell scripting is as follows:

if [ condition ]
then
   command1
   command2
   ...
fi

The 'else' statement can be used along with 'if' statement to execute a block of code when the condition is false.

Here's the syntax for 'if..else' statement:

if [ condition ]
then
   command1
   command2
   ...
else
   command3
   command4
   ...
fi

The 'elif' (else if) statement can be used for multiple conditions. It checks for the first true condition, and once it finds it, it executes that block of code and skips the rest.

Code Examples

Let's look at some examples:

Example 1: Basic 'if' statement

#!/bin/bash
num=10
if [ $num -eq 10 ]
then
    echo "The number is 10"
fi

In the above example, we are checking if the variable 'num' is equal to 10. If true, it prints "The number is 10".

Example 2: 'if..else' statement

#!/bin/bash
num=15
if [ $num -eq 10 ]
then
    echo "The number is 10"
else
    echo "The number is not 10"
fi

In this example, if the 'num' is not equal to 10, it prints "The number is not 10".

Summary

We've covered the basics of condition implementation in shell scripting, including 'if', 'if..else' and 'elif' statements. The next step in your learning journey could be to explore loops and how to use them with conditional statements in shell scripting.

Practice Exercises

  1. Write a shell script that checks if a given number is positive or negative.
  2. Write a shell script that checks if a file exists in the current directory and prints an appropriate message.

Try to solve these exercises on your own before checking the solutions below.

Solution 1:

#!/bin/bash
num=$1
if [ $num -lt 0 ]
then
    echo "The number is negative"
else
    echo "The number is positive"
fi

In this script, we accept an argument ($1) while running the script and check if it's less than 0. If true, it prints "The number is negative". Otherwise, it prints "The number is positive".

Solution 2:

#!/bin/bash
file="test.txt"
if [ -e $file ]
then
    echo "The file exists"
else
    echo "The file does not exist"
fi

This script checks if the file 'test.txt' exists in the current directory. If it does, it prints "The file exists". Otherwise, it prints "The file does not exist".

Keep practicing and exploring more about shell scripting. Happy coding!