0
0
Bash Scriptingscripting~20 mins

Long option parsing in Bash Scripting - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Long Option Parsing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2: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
A
Name: John
Age: 30
B
Unknown option: --name=John
Unknown option: --age=30
C
Name: --name=John
Age: --age=30
D
Invalid option: -n
Invalid option: -a
Attempts:
2 left
💡 Hint
Look at how the script extracts values after the '=' sign in long options.
📝 Syntax
intermediate
2: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
AMissing quotes around variable expansions in echo statements
BUsing $OPTARG instead of $OPTIND inside the case statement
CIncorrect variable expansion syntax: ${OPTARG#*=} should be ${OPTARG:=*}
DMissing colon after '-' in getopts string: ":-" should be ":-:"
Attempts:
2 left
💡 Hint
Check the getopts option string carefully.
🔧 Debug
advanced
2: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
done
AMissing colon after 'file' in getopts string to accept an argument
BUsing $2 inside getopts loop is invalid; should use $OPTARG instead
CThe script expects the value as a separate argument, but the option is passed as --file=example.txt
DThe shift command inside the case breaks the getopts loop prematurely
Attempts:
2 left
💡 Hint
Think about how getopts treats options with '=' signs.
🚀 Application
advanced
3: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?
A
while getopts ":-:" opt; do
  case $opt in
    -)
      case $OPTARG in
        mode=*)
          mode=${OPTARG#*=}
          ;;
        verbose)
          verbose=1
          ;;
      esac
      ;;
  esac
done
B
while getopts ":m:v" opt; do
  case $opt in
    m)
      mode=$OPTARG
      ;;
    v)
      verbose=1
      ;;
  esac
done
C
while getopts ":-:" opt; do
  case $opt in
    -)
      case $OPTARG in
        mode)
          mode=$2
          shift 2
          ;;
        verbose)
          verbose=1
          ;;
      esac
      ;;
  esac
done
D
while getopts ":-:" opt; do
  case $opt in
    -)
      case $OPTARG in
        mode=*)
          mode=$OPTARG
          ;;
        verbose=*)
          verbose=$OPTARG
          ;;
      esac
      ;;
  esac
done
Attempts:
2 left
💡 Hint
Remember that options with '=' need to be parsed from $OPTARG.
🧠 Conceptual
expert
3: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?
Agetopts automatically converts all long options to short options internally, causing conflicts
Bgetopts does not natively support long options with optional or multiple arguments and requires complex workarounds
Cgetopts requires external libraries to parse any options longer than one character
Dgetopts cannot parse any options starting with a dash (-) and only works with positional arguments
Attempts:
2 left
💡 Hint
Think about the design and limitations of getopts.