Parsing Arguments with Your Own Error Messages
Problem
You are using getopts to parse your options for your shell script. But you don’t like the error messages that it writes when it encounters bad input. Can you still use getopts but write your own error handling?
Solution
If you just want getopts to be quiet and not
report any errors at all, just assign $OPTERR=0
before you begin parsing. But if you
want getopts to give you more information without
the error messages, then just begin the option list with a colon. (The
v---
in the comments below is meant
to be an arrow pointing to a particular place in the line below it, to
show that special colon.)
#!/usr/bin/env bash # cookbook filename: getopts_custom # # using getopts - with custom error messages # aflag= bflag= # since we don't want getopts to generate error # messages, but want this script to issue its # own messages, we will put, in the option list, a # leading ':' v---here to silence getopts. while getopts :ab: FOUND do case $FOUND in a) aflag=1 ;; b) bflag=1 bval="$OPTARG" ;; \:) printf "argument missing from -%s option\n" $OPTARG printf "Usage: %s: [-a] [-b value] args\n" $(basename $0) exit 2 ;; \?) printf "unknown option: -%s\n" $OPTARG printf "Usage: %s: [-a] [-b value] args\n" $(basename $0) exit 2 ;; esac >&2 done shift $(($OPTIND - 1)) if [ "$aflag" ] then printf "Option -a specified\n" fi if [ "$bflag" ] then printf 'Option -b "%s" specified\n' "$bval" fi printf "Remaining arguments are: %s\n" "$*"
Discussion
The script ...
Get bash Cookbook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.