๐Ÿ“ข Too many exams? Donโ€™t know which one suits you best? Book Your Free Expert ๐Ÿ‘‰ call Now!

  • google app store apple app store
  • โœ–

      Question

      To check if a file /tmp/data.txt is both readable and

      writable by the current user in a shell conditional, which test is correct?
      A if [ -r /tmp/data.txt && -w /tmp/data.txt ]; then Correct Answer Incorrect Answer
      B if [ -r /tmp/data.txt ] && [ -w /tmp/data.txt ]; then Correct Answer Incorrect Answer
      C if test -rw /tmp/data.txt; then Correct Answer Incorrect Answer
      D if [ -read /tmp/data.txt -write ]; then Correct Answer Incorrect Answer
      E if [[ -r /tmp/data.txt -a -w /tmp/data.txt ]]; then Correct Answer Incorrect Answer

      Solution

      POSIX-compliant way uses two test commands joined by &&. Option E uses -a inside single [ which is not portable; [[ is a Bash extension but the safest explicit form is B.

      Practice Next
      ask-question