0
0
Bash Scriptingscripting

Logical operators (-a, -o, !) in Bash Scripting - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the '-a' operator do in bash scripting?
The '-a' operator means AND. It checks if both conditions on its sides are true.
Click to reveal answer
beginner
What is the purpose of the '-o' operator in bash?
The '-o' operator means OR. It checks if at least one of the conditions is true.
Click to reveal answer
beginner
What does the '!' operator do in bash conditional expressions?
The '!' operator means NOT. It reverses the result of the condition that follows it.
Click to reveal answer
intermediate
How would you write a condition to check if a file exists AND is readable using '-a'?
You write: [ -e filename -a -r filename ] which means the file exists AND is readable.
Click to reveal answer
intermediate
Why should you be careful when mixing '-a' and '-o' in the same test expression?
Because '-a' and '-o' have different precedence and can cause unexpected results. It's safer to use separate tests or parentheses.
Click to reveal answer
In bash, what does '[ -f file -a -w file ]' check?
AFile exists and is writable
BFile exists or is writable
CFile does not exist and is writable
DFile is not writable
What does the '!' operator do in '[ ! -e folder ]'?
AChecks if folder is readable
BChecks if folder exists
CChecks if folder does NOT exist
DChecks if folder is writable
Which operator means OR in bash test expressions?
A-o
B!
C-a
D&&
Why might mixing '-a' and '-o' in one test be risky?
AThey always return false
BThey only work in scripts, not command line
CThey are deprecated
DThey have different precedence which can cause logic errors
How do you write a condition to check if a file is NOT empty using '!'?
A[ -s ! file ]
B[ ! -s file ]
C[ ! -e file ]
D[ -s file ]
Explain how to use '-a', '-o', and '!' in bash conditional tests with simple examples.
Think about checking file properties with AND, OR, and negation.
You got /5 concepts.
    Describe a real-life scenario where you might use logical operators '-a', '-o', and '!' in a bash script.
    Imagine checking if a file is ready to be processed.
    You got /5 concepts.