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

Good Day everyone!! since yesterday have been a little busy/sad day but all the same we stay focus.
still on the if statement, i added a little flavor to the last question we treated in day 11. so glad to explore things as a beginning; we learn more when we explore. LETS GOOOOOO

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

Solution to the Question

Output

checking the output, you can see how many time i tried before getting the right script and writing it in a better way. don't be scared to try

Today we won't be doing much practical just some read and tomorrow might be off day or we might use it to get hands dirty on all the "loops" we will be looking into today

Case statements
In Bash, case statements are used to compare a given value against a list of patterns and execute a block of code based on the first pattern that matches. The syntax for a case statement in Bash is as follows:

Case statements syntax

case expression in
    pattern1)
        # code to execute if expression matches pattern1
        ;;
    pattern2)
        # code to execute if expression matches pattern2
        ;;
    pattern3)
        # code to execute if expression matches pattern3
        ;;
    *)
        # code to execute if none of the above patterns match expression
        ;;
esac
Enter fullscreen mode Exit fullscreen mode

Here, "expression" is the value that we want to compare, and "pattern1", "pattern2", "pattern3", and so on are the patterns that we want to compare it against.

The double semicolon ";;" separates each block of code to execute for each pattern. The asterisk "*" represents the default case, which executes if none of the specified patterns match the expression.
Let's experiment a script using case statement

Input

In this example, since the value of "fruit" is "apple", the first pattern matches, and the block of code that echoes "This is a Green fruit." is executed. If the value of "fruit" were instead "banana", the second pattern would match and the block of code that echoes "This is a yellow fruit." would execute. If the value of "fruit" were instead "Orange", the third pattern would match and the block of code that echoes "This is a Orange fruit . If the value of "fruit" does not match any of the specified patterns, the default case is executed, which echoes "Unknown fruit."

Output
That easy!!

For loop
A for loop is used when you want to repeat a set of commands for a predefined number of times. The syntax of a for loop is as follows:

for variable in list
do
   command1
   command2
   ...
   commandN
done
Enter fullscreen mode Exit fullscreen mode

In this loop, the variable takes on each value in the list, and the commands inside the loop are executed for each value of the variable.

While loop
A while loop, on the other hand, is used when you want to repeat a set of commands until a specific condition is met. The syntax of a while loop is as follows:

while [ condition ]
do
   command1
   command2
   ...
   commandN
done
Enter fullscreen mode Exit fullscreen mode

In this loop, the commands inside the loop are executed repeatedly as long as the condition is true.

Until loop
Finally, an until loop is similar to a while loop, but it executes commands until the condition becomes true, instead of executing them while the condition is true. The syntax of an until loop is as follows:

until [ condition ]
do
   command1
   command2
   ...
   commandN
done
Enter fullscreen mode Exit fullscreen mode

In this loop, the commands inside the loop are executed repeatedly until the condition becomes true.

Stopping here today. watch out for day 13

Top comments (1)

Collapse
 
devopsking profile image
UWABOR KING COLLINS

awesome post