Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bash getopts with multiple and mandatory options

Tags:

bash

getopts

Is it possible to use getopts to process multiple options together? For example, myscript -iR or myscript -irv.

Also, I have a situation where based on a condition script would need mandatory option. For example, if argument to script is a directory, I will need to specify -R or -r option along with any other options (myscript -iR mydir or myscript -ir mydir or myscript -i -r mydir or myscript -i -R mydir), in case of file only -i is sufficient (myscript -i myfile).

I tried to search but didn't get any answers.

like image 556
Ramesh Samane Avatar asked Jul 01 '12 03:07

Ramesh Samane


People also ask

How to use getopts command with multiple arguments in Bash?

Run the script with the option -p and the argument value ‘bash’, with only option -p and with the option -t. This example shows the uses of getopts command with multiple arguments. Create a bash script named grtopts3.sh with the following code to test the script.

How to handle multiple options in a getopts loop?

You can concatenate the options you provide and getopts will separate them. In your case statement you will handle each option individually. You can set a flag when options are seen and check to make sure mandatory "options" (!) are present after the getopts loop has completed.

How to enforce mandatory options in getopts?

The getopts utility does not know about mandatory options, only about what options are allowed (and what options out of these should take an option argument). If you want to enforce mandatory options, you would have to do so with your own tests in or after the option parsing loop.

How to test the getopts command in Linux?

Create a bash file named ‘getopts1.sh’ with the following code to test the code. Here, while loop will continue for the option of getopts command. Case statement will check the option.


1 Answers

You can concatenate the options you provide and getopts will separate them. In your case statement you will handle each option individually.

You can set a flag when options are seen and check to make sure mandatory "options" (!) are present after the getopts loop has completed.

Here is an example:

#!/bin/bash rflag=false small_r=false big_r=false  usage () { echo "How to use"; }  options=':ij:rRvh' while getopts $options option do     case "$option" in         i  ) i_func;;         j  ) j_arg=$OPTARG;;         r  ) rflag=true; small_r=true;;         R  ) rflag=true; big_r=true;;         v  ) v_func; other_func;;         h  ) usage; exit;;         \? ) echo "Unknown option: -$OPTARG" >&2; exit 1;;         :  ) echo "Missing option argument for -$OPTARG" >&2; exit 1;;         *  ) echo "Unimplemented option: -$OPTARG" >&2; exit 1;;     esac done  if ((OPTIND == 1)) then     echo "No options specified" fi  shift $((OPTIND - 1))  if (($# == 0)) then     echo "No positional arguments specified" fi  if ! $rflag && [[ -d $1 ]] then     echo "-r or -R must be included when a directory is specified" >&2     exit 1 fi 

This represents a complete reference implementation of a getopts function, but is only a sketch of a larger script.

like image 117
Dennis Williamson Avatar answered Oct 05 '22 05:10

Dennis Williamson