Parse Flags with getopts

medium 20 XP ~10 minHands-on lab

Write the executable script /home/student/bin/deploy.sh with this interface:

deploy.sh [-e ENV] [-n] [-v] SERVICE

Task

  • -e ENV picks the environment: staging or production. The default is staging.
  • Without -n it prints deploying SERVICE to ENV. With -n (dry run) it prints DRY RUN: deploying SERVICE to ENV instead. Both go to stdout and the script exits 0.
  • -v additionally prints verbose: on to stderr.
  • Options can come in any order and can be combined (-nv).
  • Exit 2 with the line usage: deploy.sh [-e ENV] [-n] [-v] SERVICE on stderr when: SERVICE is missing, an unknown option is given, -e has no value, or ENV is not staging or production.

For example, deploy.sh -n -e production pulse prints DRY RUN: deploying pulse to production.

About this machine

Ubuntu 24.04 with no internet access. You are student with passwordless sudo. The grader checks the final state of the machine, not the commands you typed. Values like names and ports are generated for you.

TerminalNot started

Your machine is ready to start

Create a free account to open a machine and get graded.