InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

Menu
  • Home
  • Sitemap

Python Programming Language Best Tutorials and Code Examples

Learn Python Right Now!
Home
Bash
Parsing Arguments with Your Own Error Messages in bash
Bash

Parsing Arguments with Your Own Error Messages in bash

InfinityCoder February 21, 2017

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?

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.)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/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" "$*"

See that discussion for more background. One difference here is that getopts may now return a colon.

It does so when an option is missing (e.g., you invoke the script with -b but without an argument for it).

In that case, it puts the option letter into $OPTARG so that you know what option it was that was missing itsargument.
Similarly, if an unsupported option is given (e.g., if you tried -d when invoking our example) getopts returns a question mark as the value for $FOUND, and puts the letter (the d in this case) into $OPTARG so that it can be used in your error messages.

We put a backslash in front of both the colon and the question mark to indicate that these are literals and not any special patterns or shell syntax.

While not necessary for the colon, it looks better to have the parallel construction with the two punctuations both being escaped.
We added an I/O redirection on the esac (the end of the case statement), so that all output from the various printf statements will be redirected to standard error.

This is in keeping with the purpose of standard error and is just easier to put it here than remembering to put it on each printf individually.

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Testing for String Characteristics in bash
You want your script to check the value of some …

Testing for String Characteristics in bash

Counting Lines, Words, or Characters in a File in bash
You need to know how many lines, words, or characters …

Counting Lines, Words, or Characters in a File in bash

About The Author

InfinityCoder
InfinityCoder

Leave a Reply

Cancel reply

Recent Tutorials InfinityQuest

  • Adding New Features to bash Using Loadable Built-ins in bash
    Adding New Features to bash Using Loadable …
    June 27, 2017 0
  • Getting to the Bottom of Things in bash
    Getting to the Bottom of Things in …
    June 27, 2017 0

Recent Comments

  • fer on Turning a Dictionary into XML in Python
  • mahesh on Turning a Dictionary into XML in Python

Categories

  • Bash
  • PHP
  • Python
  • Uncategorized

InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

About Us

Start learning your desired programming language with InfinityQuest.com.

On our website you can access any tutorial that you want with video and code examples.

We are very happy and honored that InfinityQuest.com has been listed as a recommended learning website for students.

Popular Tags

binary data python CIDR convert string into datetime python create xml from dict python dictionary into xml python how to create xml with dict in Python how to write binary data in Python IP Address read binary data python tutorial string as date object python string to datetime python

Archives

  • June 2017
  • April 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
Copyright © 2021 InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more
Programming Tutorials | Sitemap