0
0
Bash Scriptingscripting~15 mins

if-elif-else in Bash Scripting - Mini Project: Build & Apply

Choose your learning style9 modes available
Basic Decision Making with if-elif-else in Bash
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Just run the script or save and execute it to see the output.