Challenge - 5 Problems
File Test Operator Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate2:00remaining
What is the output of this script?
Consider a file named
example.txt that exists and is readable but not executable. What will this script print?Bash Scripting
if [ -r example.txt ] && [ ! -x example.txt ]; then echo "Readable but not executable" else echo "Other" fi
Attempts:
2 left
💡 Hint
Think about what -r and -x check for in a file.
✗ Incorrect
The -r operator checks if the file is readable, and -x checks if it is executable. Since the file is readable but not executable, the condition is true and prints "Readable but not executable".
💻 Command Output
intermediate2:00remaining
What does this script output if
mydir is a directory?Given
mydir is a directory, what will this script print?Bash Scripting
if [ -d mydir ]; then echo "Directory exists" else echo "Not a directory" fi
Attempts:
2 left
💡 Hint
The -d operator checks if the path is a directory.
✗ Incorrect
The -d operator returns true if the file exists and is a directory. Since mydir is a directory, it prints "Directory exists".
💻 Command Output
advanced2:00remaining
What error does this script produce?
What error will this script produce when run?
Bash Scripting
if [ -f ]; then echo "File exists" fi
Attempts:
2 left
💡 Hint
Check if the -f operator requires an argument.
✗ Incorrect
The -f operator needs a filename to check. Without it, the test command reports 'unary operator expected'.
💻 Command Output
advanced2:00remaining
What is the output of this script if
script.sh is executable and writable?Given
script.sh is executable and writable, what will this script print?Bash Scripting
if [ -x script.sh ] && [ -w script.sh ]; then echo "Executable and writable" else echo "Other" fi
Attempts:
2 left
💡 Hint
Remember what -x and -w check for.
✗ Incorrect
The -x operator checks if the file is executable, and -w checks if it is writable. Both conditions are true, so it prints "Executable and writable".
💻 Command Output
expert2:00remaining
What is the value of variable
result after running this script?Assuming
file.txt exists and is readable but not writable, what is the value of result after this script runs?Bash Scripting
if [ -e file.txt ]; then if [ -r file.txt ] && [ ! -w file.txt ]; then result="read-only" else result="other" fi else result="missing" fi
Attempts:
2 left
💡 Hint
Check the file existence and permissions carefully.
✗ Incorrect
The file exists (-e true), is readable (-r true), and not writable (! -w true), so result is set to "read-only".