Challenge - 5 Problems
Long Option Parsing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate2:00remaining
Output of a script parsing long options with getopts
What is the output of this Bash script when run with arguments
--name=John --age=30?Bash Scripting
#!/bin/bash while getopts ":-:" opt; do case $opt in -) case $OPTARG in name=*) name=${OPTARG#*=} echo "Name: $name" ;; age=*) age=${OPTARG#*=} echo "Age: $age" ;; *) echo "Unknown option: --$OPTARG" ;; esac ;; *) echo "Invalid option: -$OPTARG" ;; esac done
Attempts:
2 left
💡 Hint
Look at how the script extracts values after the '=' sign in long options.
✗ Incorrect
The script uses getopts with a special case for '-' to parse long options. It extracts the value after '=' and prints it. So, it correctly prints 'Name: John' and 'Age: 30'.
📝 Syntax
intermediate2:00remaining
Identify the syntax error in long option parsing
Which option contains a syntax error that prevents parsing long options correctly?
Bash Scripting
while getopts ":-" opt; do case $opt in -) case $OPTARG in name=*) name=${OPTARG#*=} echo "Name: $name" ;; age=*) age=${OPTARG#*=} echo "Age: $age" ;; *) echo "Unknown option: --$OPTARG" ;; esac ;; *) echo "Invalid option: -$OPTARG" ;; esac done
Attempts:
2 left
💡 Hint
Check the getopts option string carefully.
✗ Incorrect
The getopts string must include a colon after '-' to enable long option parsing. Without it, getopts won't recognize long options properly.
🔧 Debug
advanced2:00remaining
Why does this long option parsing script fail to capture option values?
Given this script snippet, why does it fail to print the expected values when run with
--file=example.txt?
while getopts ":-:" opt; do
case $opt in
-)
case $OPTARG in
file)
file=$2
echo "File: $file"
shift 2
;;
*)
echo "Unknown option: --$OPTARG"
;;
esac
;;
esac
doneAttempts:
2 left
💡 Hint
Think about how getopts treats options with '=' signs.
✗ Incorrect
When using long options with '=' (like --file=example.txt), getopts treats the entire string as one argument. The script incorrectly expects the value in $2, but it is part of $OPTARG.
🚀 Application
advanced3:00remaining
Write a snippet to parse --mode= and --verbose long options
Which code snippet correctly parses
--mode=fast and --verbose options using getopts in Bash?Attempts:
2 left
💡 Hint
Remember that options with '=' need to be parsed from $OPTARG.
✗ Incorrect
Option A correctly parses --mode=fast by extracting the value after '=' and sets verbose flag when --verbose is passed without value.
🧠 Conceptual
expert3:00remaining
Why is getopts limited for complex long option parsing?
Which statement best explains why getopts is not ideal for parsing complex long options in Bash scripts?
Attempts:
2 left
💡 Hint
Think about the design and limitations of getopts.
✗ Incorrect
getopts was designed for short options and does not handle long options with optional or multiple arguments well, so scripts must use complex parsing logic or other tools.