0
0
Bash Scriptingscripting~20 mins

File test operators (-f, -d, -e, -r, -w, -x) in Bash Scripting - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
File Test Operator Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2: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
AReadable but not executable
BOther
CSyntax error
DNo output
Attempts:
2 left
💡 Hint
Think about what -r and -x check for in a file.
💻 Command Output
intermediate
2: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
ANot a directory
BNo output
CSyntax error
DDirectory exists
Attempts:
2 left
💡 Hint
The -d operator checks if the path is a directory.
💻 Command Output
advanced
2:00remaining
What error does this script produce?
What error will this script produce when run?
Bash Scripting
if [ -f ]; then
  echo "File exists"
fi
AFile exists
Bbash: [: -f: unary operator expected
CNo output
DSyntax error: unexpected token
Attempts:
2 left
💡 Hint
Check if the -f operator requires an argument.
💻 Command Output
advanced
2: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
AExecutable and writable
BOther
CNo output
DSyntax error
Attempts:
2 left
💡 Hint
Remember what -x and -w check for.
💻 Command Output
expert
2: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
Amissing
Bother
Cread-only
DSyntax error
Attempts:
2 left
💡 Hint
Check the file existence and permissions carefully.