Pages

Sunday, April 10, 2016

getopts one liners

Home

The base-syntax for getopts is:
getopts OPTSTRING VARNAME [ARGS...]
where:
OPTSTRING - tells getopts which options to expect and where to expect arguments (see below)
VARNAME - tells getopts which shell-variable to use for option reporting
ARGS - tells getopts to parse these optional words instead of the positional parameters

The option-string tells getopts which options to expect and which of them must have an argument. The syntax is very simple — every option character is simply named as is, this example-string would tell getopts to look for -f, -A and –x

getopts fAx VARNAME

When you want getopts to expect an argument for an option, just place a : (colon) after the proper option flag. If you want -A to expect an argument (i.e. to become -A SOMETHING) just do:

getopts fA:x VARNAME

If the very first character of the option-string is a : (colon), which indicate getopts to switch to "silent error reporting mode". In productive scripts, this is helpful to handle errors yourself without being disturbed by annoying messages.

getopts :fA:x VARNAME


Example script getopts with only single mandatory argument

#!/bin/bash
while getopts ":a:" opt; do
  case $opt in
    a)
      echo "-a was triggered, Parameter: $OPTARG" >&2
      ;;
    \?)
      echo "Invalid option: -$OPTARG" >&2
      exit 1
      ;;
    :)
      echo "Option -$OPTARG requires an argument." >&2
      exit 1
      ;;
  esac
done

Executing script with below style will not give you any output as below,

# sh getops-01.sh
#

# sh getops-01.sh /etc/passwd
#

Executing script with option will show you various outputs as below,

# sh getops-01.sh -a
Option -a requires an argument.

# sh getops-01.sh -b
Invalid option: -b

# sh getops-01.sh -c
Invalid option: -c

# sh getops-01.sh -a 123
-a was triggered, Parameter: 123

# sh getops-01.sh -a 123 -a -a -c
-a was triggered, Parameter: 123
-a was triggered, Parameter: -a
Invalid option: -c

Example script getopts with two mandatory arguments

#!/bin/bash
while getopts ":a:b:" opt; do
  case $opt in
    a )
      echo "-a was triggered, Parameter: $OPTARG"

      ;;
    b )
      echo "-b was triggered, Parameter: $OPTARG"
      ;;
    \? )
      echo "Invalid option: -$OPTARG"
      exit 1
      ;;
    : )
      echo "Option -$OPTARG requires an argument."
      exit 1
      ;;
  esac
done

Executing script with below style will not give you any output as below,

# sh getops-02.sh
#

# sh getops-02.sh /etc/passwd
#

Executing script with option will show you various outputs as below,

# sh getops-02.sh -a
Option -a requires an argument.

# sh getops-02.sh -a -b
-a was triggered, Parameter: -b

# sh getops-02.sh -a 123
-a was triggered, Parameter: 123

# sh getops-02.sh -a 123 -b
-a was triggered, Parameter: 123
Option -b requires an argument.

# sh getops-02.sh -a 123 -b 234
-a was triggered, Parameter: 123
-b was triggered, Parameter: 234

# sh getops-02.sh -a 123 -b 234 -c
-a was triggered, Parameter: 123
-b was triggered, Parameter: 234

Invalid option: -c

References:-
http://wiki.bash-hackers.org/howto/getopts_tutorial

Back To Top
Home

No comments:

Post a Comment