Courseiva

XK0-006 Automation, Orchestration, and Scripting Practice Question

A Linux administrator is automating user account creation. The script reads a list of usernames from a file and creates each account. The administrator wants the script to continue processing remaining users even if one useradd command fails due to a duplicate username. Which Bash construct should be used to run useradd and handle the failure without aborting the script?

⚠ Common exam trap

It's easy for candidates to confuse the short-circuit behavior of && and ||, since && fires on success while || fires on failure.

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

✓

useradd "$user" || echo "Failed to add $user" >&2

The || control operator provides a concise way to execute a fallback command only when the preceding command fails. By pairing useradd with a failure branch, the script can report the duplicate username and proceed to the next iteration, satisfying the requirement to continue processing the remaining users.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • ✗

    set -e; useradd "$user"

    Why it's wrong here

    The set -e option causes the script to exit immediately if a command returns a non-zero status. That is exactly the opposite of the requirement, because a duplicate username would terminate the script and prevent remaining users from being processed. It does not provide a way to continue after failure.

  • ✓

    useradd "$user" || echo "Failed to add $user" >&2

    Why this is correct

    The || operator runs the right-hand command only when the left-hand command returns a non-zero exit status. This allows the script to log the failure for a duplicate username and continue with the next iteration, which matches the requirement to keep processing remaining users without aborting the entire script.

  • ✗

    useradd "$user" && echo "Failed to add $user" >&2

    Why it's wrong here

    The && operator runs the right-hand command only when the left-hand command succeeds. Here it would print the failure message when useradd succeeds, which is the opposite of the intended behavior, and it would not handle the duplicate-username failure at all. It does not meet the requirement to log and continue on failure.

  • ✗

    useradd "$user" | echo "Failed to add $user" >&2

    Why it's wrong here

    The pipe connects the standard output of useradd to the standard input of echo, which is not how echo works; echo ignores its input and prints its arguments. This does not check the exit status of useradd and would not log a failure, so it fails to handle the duplicate username scenario.

About these practice questions

This XK0-006 question is part of Courseiva's 781-question bank — original exam-style content with full explanations and wrong-answer analysis, never real exam questions or exam dumps. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

JA

Written and reviewed by Johnson Ajibi, MSc IT Security

Senior Network & Security Engineer · founder of Courseiva

Last reviewed September 2026 · checked against the official CompTIA exam blueprint

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.