0
0
Bash Scriptingscripting~15 mins

if-then-fi structure in Bash Scripting - Mini Project: Build & Apply

Choose your learning style9 modes available
Basic Decision Making with if-then-fi Structure in Bash
📖 Scenario: You are writing a simple bash script to check if a number is positive. This is like checking if the temperature outside is above zero before deciding to wear a jacket.
🎯 Goal: Build a bash script that uses the if-then-fi structure to check if a variable number is greater than zero and print a message accordingly.
📋 What You'll Learn
Create a variable number with the value 5
Create a variable threshold with the value 0
Use an if statement with then and fi to check if number is greater than threshold
Print "Number is positive" if the condition is true
Print "Number is not positive" if the condition is false
💡 Why This Matters
🌍 Real World
Scripts often need to make decisions, like checking if a file exists or if a user input meets certain criteria. The if-then-fi structure is the basic way to do this in bash.
💼 Career
Understanding conditional statements in bash is essential for system administrators, DevOps engineers, and anyone automating tasks on Linux or Unix systems.
Progress0 / 4 steps
1
Create the number variable
Create a variable called number and set it to 5.
Bash Scripting
Need a hint?

Use number=5 without spaces around the equals sign.

2
Create the threshold variable
Create a variable called threshold and set it to 0.
Bash Scripting
Need a hint?

Use threshold=0 without spaces around the equals sign.

3
Write the if-then-fi condition
Write an if statement to check if number is greater than threshold. Use then and fi to mark the block. Inside the if, print "Number is positive". Use else to print "Number is not positive" if the condition is false.
Bash Scripting
Need a hint?

Use if [ "$number" -gt "$threshold" ] to compare numbers. Remember to close the block with fi.

4
Run the script and display the output
Run the script and print the output to show the message based on the if condition.
Bash Scripting
Need a hint?

Just run the script. The output should be Number is positive.