You need to make a portable Bourne-style shell script terminate when the path stored in the variable LOGFILE is not both a regular file and writable. Complete the blank line in the excerpt so that the test is POSIX-compliant and protects the variable from word splitting:
# LOGFILE is already set
if __________; then
echo "Logging to $LOGFILE"
else
echo "Cannot write to $LOGFILE" >&2
exit 1
fi
The shell executes the command list that follows the if keyword and decides whether to enter the then branch based on its exit status. Using [ -f "$LOGFILE" ] && [ -w "$LOGFILE" ] invokes two separate test commands; each returns 0 only when its predicate is true, and the shell's logical-AND operator && ensures that the overall status is 0 only when both conditions are satisfied. Quoting the variable prevents word-splitting and wildcard expansion.
A variant that uses [[ -f "$LOGFILE" && -w "$LOGFILE" ]] relies on Bash's [[ reserved word, which is not defined by POSIX and therefore breaks the portability requirement. A form such as [ -f $LOGFILE && -w $LOGFILE ] places && inside single brackets, where it is treated as an ordinary argument; the test utility does not recognise it, leading to a syntax error. Finally, test -fw "$LOGFILE" is invalid because the test utility defines -f and -w as separate unary primaries; it has no combined -fw operator, so the expression never behaves as intended.
Ask Bash
Bash is our AI bot, trained to help you pass your exam. AI Generated Content may display inaccurate information, always double-check anything important.
What does the '-f' test in a shell script check for?
Open an interactive chat with Bash
Why is variable quoting important in shell scripts?
Open an interactive chat with Bash
What is the difference between '[ ]' and '[[ ]]' in shell scripting?
Open an interactive chat with Bash
CompTIA Linux+ XK0-006 (V8)
Automation, Orchestration, and Scripting
Your Score:
Report Issue
Bash, the Crucial Exams Chat Bot
AI Bot
Loading...
Loading...
Loading...
IT & Cybersecurity Package Join Premium for Full Access