📖 Scenario: You are writing a simple script to check the temperature and give advice on what to wear.
🎯 Goal: Create a bash script that uses if-elif-else statements to print clothing advice based on the temperature.
📋 What You'll Learn
Create a variable temperature with a specific value
Create a variable cold_threshold to set the cold temperature limit
Use if-elif-else to check if temperature is below cold_threshold, between cold_threshold and 25, or above 25
Print the correct advice message for each temperature range
💡 Why This Matters
🌍 Real World
Scripts like this help automate decisions based on changing data, such as weather or system status.
💼 Career
Understanding if-elif-else is essential for writing scripts that respond to different conditions in IT, DevOps, and automation roles.
Progress0 / 4 steps
1
Set the temperature variable
Create a variable called temperature and set it to 18.
Bash Scripting
Hint
Use variable=value syntax without spaces for bash variables.
2
Set the cold temperature threshold
Create a variable called cold_threshold and set it to 15.
Bash Scripting
Hint
Remember to use the same syntax as before for variables.
3
Write the if-elif-else decision structure
Write an if-elif-else statement that checks the value of temperature against cold_threshold and 25. Use -lt for less than and -le for less or equal in numeric comparisons. The conditions are: 1. If temperature is less than cold_threshold, print "Wear a jacket". 2. Else if temperature is less or equal to 25, print "Wear a sweater". 3. Else, print "Wear a t-shirt".
Bash Scripting
Hint
Use square brackets with spaces and double quotes around variables for safety.
4
Run the script and display the output
Print the output of the script by running it or echoing the result. The expected output is exactly Wear a sweater.
Bash Scripting
Hint
Just run the script or save and execute it to see the output.
Practice
(1/5)
1. What does the if-elif-else structure do in a Bash script?
easy
A. It repeats a set of commands multiple times.
B. It comments out lines of code.
C. It defines a function to reuse code.
D. It lets the script choose between multiple actions based on conditions.
Solution
Step 1: Understand the purpose of if-elif-else
The if-elif-else structure checks conditions one by one and runs the matching block.