Courseiva
Automation, Orchestration, and ScriptingmediumMultiple ChoiceObjective-mapped

XK0-006 Automation, Orchestration, and Scripting Practice Question

A Linux administrator is writing a bash script that accepts command-line options: -a for all, -f for file, and -o for output. Which of the following correctly uses getopts to parse these options?

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

while getopts "afo" opt; do case $opt in a) ...;; f) ...;; o) ...;; esac; done

getopts uses a string of option characters; a colon after an option indicates it requires an argument. The correct usage is 'a:f:o' where a and f require arguments, but the stem doesn't specify arguments. Assuming -a and -f don't require arguments, the string should be 'afo'.

Answer analysis

Option-by-option breakdown

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

  • while getopts "-a -f -o" opt; do ... done

    Why it's wrong here

    The option string should not include dashes; getopts expects a string of option letters.

  • while getopts "a:f:o" opt; do case $opt in a) ...;; f) ...;; o) ...;; esac; done

    Why it's wrong here

    This indicates that -a and -f require arguments, which is not stated in the stem.

  • getopts "afo" opt; case $opt in a) ...;; f) ...;; o) ...;; esac

    Why it's wrong here

    getopts should be used in a while loop to process all options; this only processes one option.

  • while getopts "afo" opt; do case $opt in a) ...;; f) ...;; o) ...;; esac; done

    Why this is correct

    Correct. The option string 'afo' defines three boolean options without arguments.

About these practice questions

This XK0-006 question is part of Courseiva's 979-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 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.