Parse flags with `getopts`, then validate operands
Implement a small Bash CLI using `getopts`, `usage()`, `shift`, and operand validation.
A backup script needs environment, customer, optional dry-run, and one source directory. The current positional order is easy to swap. POSIX-style CLI: parse options with getopts, shift parsed flags, then validate operands. Do not read $1, $2, and $3 directly once options become optional. Optional flags change positions and make the same command mean different things. Usage Create usage() with backup.sh -e ENV -c CUSTOMER [-n] -- SOURCE_DIR. The synopsis shows options first, -- before operands, and a single source directory. Every bad-argument path can now print the same help. Parse Use while getopts ':e:c:nh' opt; do case "$opt" in…
Sign up free — one personalized lesson every day, matched to your role and goals.
Already have an account? Sign in