DEV Community

Cover image for Continuation to Bash scripting: Introduction to Conditional statement and Loop
Kenneth Aigbuza
Kenneth Aigbuza

Posted on

Continuation to Bash scripting: Introduction to Conditional statement and Loop

surprise we are still on bash scripting? don't be surprise. bash scripting is wide and there are more to it. let's dive into conditional statement and loop

why is conditional statement and loop important?

Loops are useful in Bash scripts for automating repetitive tasks and streamlining workflows. For instance, a for loop can be used to rename multiple files at once, while a while loop can be used to monitor the status of a process and continue to monitor it until it has completed. An until loop can be used to repeatedly execute a task until a specific condition is met, such as establishing an Internet connection.

Conditional statements can also be used with loops to add more control to Bash scripts. For example, a for loop can be used to iterate over a list of files and check if each file contains a specific text string. If the string is found, the script can perform a task on the file. By automating tasks with loops, developers can save time, improve efficiency, and reduce the likelihood of errors.

i focused on condition statement for my Day 11, which i will be sharing right away.

Conditional statements provide a way to add more control to Bash scripts and loops, enabling them to perform different actions based on specific conditions. Here’s how to use conditional statements with loops in Bash scripts:

  1. If statements: If statements are used to execute a command or a set of commands when a condition is true. You can use an if statement inside a loop to test a condition for each iteration of the loop. For example, you can use an if statement to check if a file exists and perform an action based on the result.

  2. Case statements: Case statements are used to test a variable against a list of possible values and execute a command or a set of commands based on the matching value. You can use a case statement inside a loop to perform different actions based on the value of a variable. For example, you can use a case statement to perform different actions based on the file type.

  3. While statements: While statements provide a way to continue executing a loop while a condition is true. You can use a while statement inside a loop to test a condition and continue or break the loop based on the result. For example, you can use a while statement to continue reading a file until the end of the file is reached.

  4. For statements: For statements provide a way to execute a loop for a specific number of times or for each item in a list. You can use a for statement inside a loop to perform different actions based on the iteration of the loop. For example, you can use a for statement to iterate over a list of files and perform an action for each file.

Conditional statements (if/else)
Expressions that produce a boolean result, either true or false, are called conditions. There are several ways to evaluate conditions, including if, if-else, if-elif-else, and nested conditionals.

Syntax:

if [[ condition ]];
then
statement
elif [[ condition ]]; then
statement
else
do this by default
fi

Let's see an example of a Bash script that uses if, if-else, and if-elif-else statements to determine if a user-inputted number is positive, negative, or zero

from the question above, we will be writing a script that will indicate if a number inputted by a user is positive, negative or zero. LET'S GOOOOOO

Solution to the Question

REMEMBER:
-lt stands for less than
-gt stands for greater than

Output

very Easy right? let's solve some more questions

Q. write a script that checks the sum of 2 numbers and that number must be validated else throw a suggestion or error
Let's try this together

Solution to the Question

REMEMBER:
-eq stands for equal to
$# represent the number of command line argument passed to the script

Output

interesting!!!!!

We can use logical operators such as AND -a and OR -o to make comparisons that have more significance.
For Example:

echo "Enter a number"
read num
if [ $a -gt 30 -a $b -lt 60 ]; then
echo "The value is Valid"
else
echo "ERROR: Input a number in the range 30 to 59"
fi

run this and share your output in the comment section

Q. Write a Bash Script that checks the time of the day and print a corresponding greeting message

Solution to the Question

Output
Did you notice the errors in the first two output of the script? it shows error is normal. it opens our mind to thinking wide in other to get a solution.
Also did you Notice the changes in the last two output of the command, it shows the command was run in two different time and i added seconds to the script which made the last output show 9:19:16. do that yourself (winks)

that is all for day 11..Day 12 loading.....

Top comments (0)