In bash, how can I have a set of arguments in any random order? How is a key-value pair?

Example:

./myscript --ip 192.168.1.1 --port 1985 

or other possible

 ./myscript --port 1985 --ip 192.168.1.1 

I want my script to accept a set of arguments in any order

 ./myscript abcd ./myscript dcba ./myscript bdac 

Etcetera

+8
bash
source share
3 answers

take a look at getopts

getopts : getopts optstring name [arg]
Parameters for the parse argument.

Getopts is used by shell routines to analyze positional parameters as options.

OPTSTRING contains the letters of the options to be recognized; if the letter is followed by a colon, it is expected that the parameter will have an argument, which should be separated by a space.

Each time it is called, getopts places the next option in the shell variable $ name, the initialization name if it does not exist, and the index of the next argument to be processed in the shell is the OPTIND variable. OPTIND is initialized to 1 each time a shell or shell script is called. If an argument is required for the parameter, getopts places this argument in the OPTARG shell OPTARG .

getopts reports errors in one of two ways. If the first character of OPTSTRING is a colon, getopts uses silent error reporting. In this mode, error messages are not printed. If an invalid option is seen, getopts places the option character in OPTARG. If the required argument is not found, getopts places in NAME ' : ' sets OPTARG to the option character. If getopts is not in silent mode and an invalid option, getopts places '?' in NAME and unsets OPTARG. If the required argument is not found, ' ? is placed in NAME, OPTARG does not work, and a diagnostic message is printed.

If the OPTERR shell variable is OPTERR to 0, getopts disables printing of error messages, even if the first character of OPTSTRING is not a colon. By default, OPTERR is set to 1.

Getopts usually parses positional parameters ($ 0 - $ 9), but if there are more arguments, they are parsed instead.

Exit Status:
Returns success if an option is found; if the end of the options occurs or an error occurs.

+10
source share

You can use getopts to parse command line arguments. This tutorial is a good place to start.

+7
source share

This simple script accepts a port number with p and an IP address with parameter i .

 while getopts "i: p:" option;  do
   case $ option in
     i) ip_address = $ OPTARG
     echo "ip address: $ ip_address"
     ;;
     p) port_number = $ OPTARG
     echo "port number: $ port_number"
     ;;
   esac
 done

It can be executed in any case:

./myscript -i 192.168.1.1 -p 1985

or

./myscript -p 1985 -i 192.168.1.1

When executed, it prints:

 ip address: 192.168.1.1
 port number: 1985

Also, as mentioned in http://wiki.bash-hackers.org/howto/getopts_tutorial

Please note that getopts cannot parse long options in the GNU style (--myoption) or long options in the XF86 style (-myoption)

Thus, you cannot use long lines like --port or --ip directly with getopts . However, additional workarounds described in this link are possible:
http://www.bahmanm.com/blogs/command-line-options-how-to-parse-in-bash-using-getopt

+3
source share

All Articles