diff --git a/tutorials/learnshell.org/en/Welcome.md b/tutorials/learnshell.org/en/Welcome.md index ab6a4fe0b..bfa206003 100644 --- a/tutorials/learnshell.org/en/Welcome.md +++ b/tutorials/learnshell.org/en/Welcome.md @@ -20,6 +20,7 @@ Just click on the chapter you wish to begin from, and follow the instructions. G - [[Loops]] - [[Array-Comparison]] - [[Shell Functions]] +- [[elif]] ### Advanced Tutorials diff --git a/tutorials/learnshell.org/en/elif.md b/tutorials/learnshell.org/en/elif.md new file mode 100644 index 000000000..1d53c15ac --- /dev/null +++ b/tutorials/learnshell.org/en/elif.md @@ -0,0 +1,60 @@ +# Tutorial +-------- +In Bash scripting, `elif` is used to handle multiple conditions in an `if` statement. It stands for "else if" and allows you to check additional conditions if the initial `if` condition is false. It's useful for branching beyond just `if` and `else`. + + Example: + +```bash +if [ $num -eq 1 ]; then + echo "One" +elif [ $num -eq 2 ]; then + echo "Two" +else + echo "Other" +fi +``` + +# Exercise +In this exercise, you will need to use an `if`, `elif`, and `else` statement to evaluate the value of a variable called `color`. + +Your task is to: +- Print `"You chose black!"` if the value of `color` is `"black"` +- Print `"You chose green!"` if the value is `"green"` + +- Print `"You chose purple!"` if the value is `"purple"` +- Print `"You chose orange!"` if the value is `"orange"` +- Print `"Unknown/uncataloged color, please try again!"` for any other value + +Set `color="black"` to begin, and write the conditional logic that produces the correct output. + +# Tutorial Code +```bash +#!/bin/bash +#enter your code here +``` +# Expected Output +You chose black! + +# Solution +```bash +#!/bin/bash + +color="black" + +if [ "$color" == "black" ]; then + echo "You chose black!" +elif [ "$color" == "green" ]; then + echo "You chose green!" + +elif [ "$color" == "purple" ]; then + echo "You chose purple!" + +elif [ "$color" == "orange" ]; then + echo "You chose orange!" + +else + echo "Unknown/uncataloged color, please try again!" +fi +``` + +[[Welcome]]