Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to correctly initialize the option parsing loop using getopts.
Bash Scripting
while getopts [1] opt; do case $opt in h) echo "Help message"; exit 0;; *) echo "Invalid option"; exit 1;; esac done
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Including a dash '-' in the option string.
Using multiple letters when only one is needed.
Adding a colon when the option does not take an argument.
✗ Incorrect
The getopts string should contain the option letters to parse. Here, only 'h' is expected, so "h" is correct.
2fill in blank
mediumComplete the code to correctly check if the option requires an argument.
Bash Scripting
while getopts "f[1]" opt; do case $opt in f) filename=$OPTARG;; *) echo "Unknown option"; exit 1;; esac done
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting the colon after the option letter that requires an argument.
Adding letters without colons for options that need arguments.
✗ Incorrect
A colon ':' after an option letter means that option requires an argument. Here, 'f:' means option 'f' needs an argument.
3fill in blank
hardFix the error in the option parsing loop to correctly handle unknown options.
Bash Scripting
while getopts "ab" [1]; do case $opt in a) echo "Option a";; b) echo "Option b";; *) echo "Unknown option: $opt"; exit 1;; esac done
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different variable name after getopts than in the case statement.
Using $OPTARG instead of the option variable for case matching.
✗ Incorrect
The variable after getopts is the option variable name. It must match the variable used in the case statement. Here, 'opt' is correct.
4fill in blank
hardFill both blanks to correctly parse options 'u' with argument and 'v' without argument.
Bash Scripting
while getopts [1] [2]; do case $opt in u) user=$OPTARG;; v) verbose=1;; *) echo "Invalid option"; exit 1;; esac done
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Not adding a colon after 'u' if it requires an argument.
Using a variable name different from 'opt' in the loop.
✗ Incorrect
The option string "u:v" means 'u' and 'v' are options; 'u' requires an argument due to the colon after u, while 'v' does not. The variable after getopts is 'opt' as used in the case.
5fill in blank
hardFill all three blanks to create a dictionary of options and their arguments, skipping options without arguments.
Bash Scripting
declare -A opts while getopts [1] [2]; do case $opt in u) opts[[3]]=$OPTARG;; v) opts[verbose]=1;; *) echo "Unknown option"; exit 1;; esac done
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Not using quotes around the dictionary key.
Using the wrong variable name after getopts.
Missing colon after option letter that requires argument.
✗ Incorrect
The option string "u:v" means 'u' requires an argument, 'v' does not. The variable after getopts is 'opt'. The key for the dictionary is 'user' as a string.