0
0
Bash Scriptingscripting~10 mins

Logical operators (-a, -o, !) 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 file 'data.txt' exists and is readable.

Bash Scripting
if [ -e data.txt [1] ]; then echo "File exists and is readable"; fi
Drag options to blanks, or click blank then click option'
A-a -r data.txt
B-a -e data.txt
C-a -f data.txt
D-o -r data.txt
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-o' instead of '-a' for AND operation.
Forgetting to include the file test after '-a'.
Using '-f' instead of '-r' when checking readability.
2fill in blank
medium

Complete the code to check if the variable 'count' is either 0 or 10.

Bash Scripting
if [ "$count" -eq 0 [1] "$count" -eq 10 ]; then echo "Count is 0 or 10"; fi
Drag options to blanks, or click blank then click option'
A==
B-a
C-o
D!
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-a' which means AND instead of OR.
Using '==' which is not valid in this context.
Using '!' which negates a condition.
3fill in blank
hard

Fix the error in the code to check if the directory 'logs' does NOT exist.

Bash Scripting
if [ [1] -e logs ]; then echo "Logs directory missing"; fi
Drag options to blanks, or click blank then click option'
A-z
B-o
C-a
D!
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-o' or '-a' which are logical AND/OR, not negation.
Using '-z' which checks for empty strings, not existence.
4fill in blank
hard

Complete the code to check if the file 'config.cfg' exists AND is NOT empty.

Bash Scripting
if [ -e config.cfg [1] -s config.cfg ]; then echo "Config file exists and is not empty"; fi
Drag options to blanks, or click blank then click option'
A-a
B-o
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-o' which means OR instead of AND.
Confusing '-s' (non-empty) with empty check; '! -s' would be for empty or non-existent.
5fill in blank
hard

Fill all three blanks to create a condition that checks if variable 'user' is NOT 'admin' AND file 'access.log' exists OR variable 'mode' equals 'debug'.

Bash Scripting
if [ ! "$user" = [1] [2] -e access.log [3] "$mode" = "debug" ]; then echo "Condition met"; fi
Drag options to blanks, or click blank then click option'
A"admin"
B-a
C-o
D"root"
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong strings for user comparison.
Mixing up '-a' and '-o' operators.
Not quoting variables properly.