0
0
Bash Scriptingscripting~10 mins

if-elif-else in Bash Scripting - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to check if the variable 'num' is equal to 10.

Bash Scripting
if [ $num [1] 10 ]; then
  echo "Number is ten"
fi
Drag options to blanks, or click blank then click option'
A==
B=
C!=
D-eq
Attempts:
3 left
💡 Hint
Common Mistakes
Using '=' or '==' which are for strings, not numbers.
Forgetting spaces around brackets and operators.
2fill in blank
medium

Complete the code to add an elif branch that checks if 'num' is greater than 10.

Bash Scripting
if [ $num -eq 10 ]; then
  echo "Number is ten"
elif [ $num [1] 10 ]; then
  echo "Number is greater than ten"
fi
Drag options to blanks, or click blank then click option'
A-gt
B-lt
C-eq
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-lt' which means 'less than'.
Using '==' which is for strings, not numbers.
3fill in blank
hard

Fix the error in the else branch syntax.

Bash Scripting
if [ $num -eq 10 ]; then
  echo "Number is ten"
elif [ $num -gt 10 ]; then
  echo "Number is greater than ten"
[1]
  echo "Number is less than ten"
fi
Drag options to blanks, or click blank then click option'
Aelse
Belif
Cthen
Dfi
Attempts:
3 left
💡 Hint
Common Mistakes
Writing 'elif' without a condition.
Using 'then' alone without 'else'.
4fill in blank
hard

Fill both blanks to correctly compare a string variable 'color' with 'red' and 'blue'.

Bash Scripting
if [ "$color" [1] "red" ]; then
  echo "Color is red"
elif [ "$color" [2] "blue" ]; then
  echo "Color is blue"
else
  echo "Color is something else"
fi
Drag options to blanks, or click blank then click option'
A=
B!=
C==
D-eq
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-eq' which is for numbers.
Using '!=' when checking for equality.
5fill in blank
hard

Fill all three blanks to create a nested if-elif-else that checks if 'score' is above 90, above 75, or else.

Bash Scripting
if [ $score [1] 90 ]; then
  echo "Grade A"
elif [ $score [2] 75 ]; then
  echo "Grade B"
[3]
  echo "Grade C"
fi
Drag options to blanks, or click blank then click option'
A-gt
B-ge
Celse
D-lt
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'else' with a condition.
Mixing string and numeric operators.