0
0
Bash Scriptingscripting~10 mins

Long option parsing in Bash Scripting - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to check if the first argument is the long option '--help'.

Bash Scripting
if [ "$1" = [1] ]; then
  echo "Help option selected"
fi
Drag options to blanks, or click blank then click option'
A"-h"
B"--help"
C"help"
D"--Help"
Attempts:
3 left
💡 Hint
Common Mistakes
Using single dash '-h' instead of double dash '--help'.
Forgetting to quote the option string.
2fill in blank
medium

Complete the code to use a case statement to handle the '--version' option.

Bash Scripting
case "$1" in
  [1])
    echo "Version 1.0"
    ;;
esac
Drag options to blanks, or click blank then click option'
A"--help"
B"version"
C"-v"
D"--version"
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-v' instead of '--version'.
Missing quotes around the option.
3fill in blank
hard

Fix the error in the code to correctly parse the '--output' option with a value.

Bash Scripting
if [[ $1 == [1] ]]; then
  output_file=$2
fi
Drag options to blanks, or click blank then click option'
A"--output"
B"-o"
C"output"
D"--Output"
Attempts:
3 left
💡 Hint
Common Mistakes
Using single dash '-o' instead of '--output'.
Using single brackets [ ] which do not support pattern matching.
4fill in blank
hard

Fill both blanks to correctly parse '--file' option and assign its value.

Bash Scripting
if [[ $1 == [1] ]]; then
  file_name=[2]
fi
Drag options to blanks, or click blank then click option'
A"--file"
B$2
C$1
D"file"
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning $1 instead of $2 as the file name.
Using single dash '-file' instead of '--file'.
5fill in blank
hard

Fill all three blanks to create a case statement that handles '--help', '--version', and '--output' options.

Bash Scripting
case "$1" in
  [1])
    echo "Show help"
    ;;
  [2])
    echo "Show version"
    ;;
  [3])
    echo "Output file: $2"
    ;;
esac
Drag options to blanks, or click blank then click option'
A"--help"
B"--version"
C"--output"
D"-o"
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-o' instead of '--output'.
Mixing up option names or missing quotes.