You are refactoring a provisioning script that reads a configuration file path from the environment variable CONFIG_FILE. The script must abort when either CONFIG_FILE is unset or empty, or when the path stored in the variable is not an existing regular file. Which Bash conditional expression, placed inside an if statement, meets this requirement?
The requirement is satisfied by combining two separate tests with a logical OR (||):
-z returns true when the length of CONFIG_FILE is zero, covering the cases where the variable is unset or empty.
-f returns true when the given pathname is an existing regular file, so ! -f is true when the file is missing or not a regular file.
Using these together as [[ -z "$CONFIG_FILE" || ! -f "$CONFIG_FILE" ]] causes the condition to be true whenever either prerequisite is violated, allowing the script to abort as intended. The other expressions fail for one or more reasons:
[[ -n "$CONFIG_FILE" && -f "$CONFIG_FILE" ]] is true only when the variable is non-empty and the file exists-the opposite of what is required.
[[ -z "$CONFIG_FILE" && -f "$CONFIG_FILE" ]] can never be true because a zero-length variable cannot simultaneously reference an existing file.
[[ -n "$CONFIG_FILE" || ! -d "$CONFIG_FILE" ]] uses the directory test (-d) instead of the regular-file test, so it would pass when the pathname is a directory and miss missing regular files.
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` flag in Bash conditional expressions check for?
Open an interactive chat with Bash
How does the `-z` operator work in Bash, and why is it used in this script?
Open an interactive chat with Bash
What is the purpose of the logical OR (`||`) operator in Bash conditional expressions?
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