0
0
Bash Scriptingscripting~10 mins

if-then-fi structure 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 5.

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

Complete the code to check if the file myfile.txt exists.

Bash Scripting
if [1] myfile.txt; then
  echo "File exists"
fi
Drag options to blanks, or click blank then click option'
Atest -s
Btest -d
Ctest -f
Dtest -e
Attempts:
3 left
💡 Hint
Common Mistakes
Using -d which checks only directories.
Using -s which checks if file is not empty.
3fill in blank
hard

Fix the error in the if statement to correctly check if count is greater than 10.

Bash Scripting
if [ $count [1] 10 ]; then
  echo "Count is large"
fi
Drag options to blanks, or click blank then click option'
A<
B-gt
C==
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' inside single brackets which is a string comparison and needs escaping.
Using '==' which is for string equality.
4fill in blank
hard

Fill both blanks to check if the variable word is not empty and equals "hello".

Bash Scripting
if [ -n [1] ] && [ [2] = "hello" ]; then
  echo "Greeting detected"
fi
Drag options to blanks, or click blank then click option'
A$word
B-z
D"hello"
Attempts:
3 left
💡 Hint
Common Mistakes
Using -z which checks for empty string.
Not using the variable with a $ sign.
5fill in blank
hard

Fill all three blanks to check if num is between 1 and 10 inclusive.

Bash Scripting
if [ [1] -ge 1 ] && [ [2] [3] 10 ]; then
  echo "Number is between 1 and 10"
fi
Drag options to blanks, or click blank then click option'
A$num
C-le
D-lt
Attempts:
3 left
💡 Hint
Common Mistakes
Using string comparison operators instead of numeric ones.
Using -lt instead of -le for the upper bound.