XK0-006 Automation, Orchestration, and Scripting Practice Question
An administrator writes a bash script that uses a function to check if a file exists and is readable. The function returns 0 if the file meets the conditions, and 1 otherwise. Which of the following correctly implements this function?
Answer choices
Why each option matters
Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.
Correct answer & explanation
✓
check_file() { [[ -f "$1" ]] && [[ -r "$1" ]] && return 0; return 1; }
All four options are functionally correct. Option A chains two [[ ]] tests; Option B chains test commands with && and ||; Option C uses [ -a ]; Option D uses [[ && ]] inside an if statement. Each returns 0 only when the file exists and is readable, and returns 1 otherwise. If a single-answer question is required, the stem should be revised.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✓
check_file() { [[ -f "$1" ]] && [[ -r "$1" ]] && return 0; return 1; }
Why this is correct
Incorrect. This function uses separate test commands chained with &&, but the final 'return 1' is not part of an else clause. While it may work in practice, it is less readable and can be confusing. The correct approach is to use an if-else structure or a single compound condition.
- ✓
check_file() { test -f "$1" && test -r "$1" && return 0 || return 1; }
Why this is correct
Incorrect. The use of '&& return 0 || return 1' can fail if the return command itself fails (though it rarely does). Additionally, the logic is not as explicit as an if-else, and mixing test commands with return can lead to unexpected behavior in some edge cases.
- ✓
check_file() { if [ -f "$1" -a -r "$1" ]; then return 0; else return 1; fi; }
Why this is correct
Incorrect. While this uses an if-else, it relies on the deprecated '-a' operator inside single brackets. The '-a' operator is not guaranteed to work in all shells and can be error-prone. The modern approach is to use [[ ]] with &&.
- ✓
check_file() { if [[ -f "$1" && -r "$1" ]]; then return 0; else return 1; fi; }
Why this is correct
Correct. This function uses the modern [[ ]] syntax with a single compound condition (&&) inside the brackets, which is clear, reliable, and portable within bash. The if-else structure explicitly handles both outcomes.
Go deeper
Related to this question
Learn chapter
File Permissions and Ownership
Key term
Bash
Bash is a command-line interpreter that lets users interact with an operating system by typing text commands instead of clicking icons.
Key term
Bash script
A Bash script is a text file containing a sequence of commands for the Unix shell Bash, allowing users to automate repetitive tasks and streamline system administration on Linux and macOS.
About these practice questions
One of 979 original XK0-006 practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. Learn why practice questions differ from exam dumps →
JA
Written by Johnson Ajibi, MSc IT Security
Senior Network & Security Engineer · founder of Courseiva
This XK0-006 practice question is part of Courseiva's free CompTIA certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the XK0-006 exam.