Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to check if a file exists.
Bash Scripting
if [ -e [1] ]; then echo "File exists" fi
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Putting the test option like -f instead of the file name.
Leaving the blank empty.
✗ Incorrect
The test command [ -e myfile.txt ] checks if the file named 'myfile.txt' exists.
2fill in blank
mediumComplete the code to print "Yes" if variable x equals 10.
Bash Scripting
x=10 if [ [1] -eq 10 ]; then echo "Yes" fi
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using x without $ which compares string 'x' not the variable value.
Using == which is not for numeric comparison in test.
✗ Incorrect
Use $x to get the value of variable x inside the test brackets.
3fill in blank
hardFix the error in the conditional to check if a directory exists.
Bash Scripting
DIR="/home/user" if [ [1] "$DIR" ]; then echo "Directory exists" fi
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using -f which checks for regular files, not directories.
Using -e which checks for any file type but not specifically directory.
✗ Incorrect
Use -d to check if the path is a directory.
4fill in blank
hardFill both blanks to check if variable num is greater than 5 and print a message.
Bash Scripting
num=7 if [ [1] [2] 5 ]; then echo "Greater than five" fi
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using variable name without $.
Using -lt which means less than.
✗ Incorrect
Use $num to get the variable value and -gt to check if greater than 5.
5fill in blank
hardFill both blanks to create a conditional that checks if a string is empty.
Bash Scripting
str="" if [ [1] [2] ]; then echo "String is empty" fi
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using -n which checks for non-empty string.
Using str without the leading $.
✗ Incorrect
Use -z $str to test if the string length is zero.