📢 Too many exams? Don’t know which one suits you best? Book Your Free Expert 👉 call Now!


    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