在 bash 中,我怎样才能以任何随机顺序获得一组参数?像键值对?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7528646/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
In bash, how can I have a set of arguments in any random order? Like a key-value pair?
提问by stackoverflow
Example:
例子:
./myscript --ip 192.168.1.1 --port 1985
or another possible
或其他可能
./myscript --port 1985 --ip 192.168.1.1
I want to allow my script to take a set of arguments, in any order
我想允许我的脚本以任何顺序接受一组参数
./myscript a b c d
./myscript d c b a
./myscript b d a c
Etcetera
等等
回答by c00kiemon5ter
take a look at getopts
看一眼 getopts
getopts: getopts optstring name [arg]
Parse option arguments.Getopts is used by shell procedures to parse positional parameters as options.
OPTSTRINGcontains the option letters to be recognized; if a letter is followed by a colon, the option is expected to have an argument, which should be separated from it by white space.Each time it is invoked, getopts will place the next option in the shell variable $name, initializing name if it does not exist, and the index of the next argument to be processed into the shell variable OPTIND. OPTIND is initialized to 1 each time the shell or a shell script is invoked. When an option requires an argument, getopts places that argument into the shell variable
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, no error messages are printed. If an invalid option is seen, getopts places the option character found into OPTARG. If a required argument is not found, getopts places a ':' into NAME and sets OPTARG to the option character found. If getopts is not in silent mode, and an invalid option is seen, getopts places '?' into NAME and unsets OPTARG. If a required argument is not found, a '?' is placed in NAME, OPTARG is unset, and a diagnostic message is printed.
If the shell variable
OPTERRhas the value 0, getopts disables the printing of error messages, even if the first character of OPTSTRING is not a colon. OPTERR has the value 1 by default.Getopts normally parses the positional parameters ($0 - $9), but if more arguments are given, they are parsed instead.
Exit Status:
Returns success if an option is found; fails if the end of options is encountered or an error occurs.
getopts: getopts optstring name [arg]
解析选项参数。Shell 过程使用 Getopts 将位置参数解析为选项。
OPTSTRING包含要识别的选项字母;如果一个字母后跟一个冒号,则该选项应该有一个参数,该参数应与它用空格分隔。每次调用时,getopts 都会将下一个选项放在shell 变量$name 中,如果name 不存在则初始化,并将下一个要处理的参数的索引放入shell 变量OPTIND 中。每次调用 shell 或 shell 脚本时,OPTIND 都会被初始化为 1。当一个选项需要一个参数时,getopts 将该参数放入 shell 变量中
OPTARG。getopts 以两种方式之一报告错误。如果 OPTSTRING 的第一个字符是冒号,则 getopts 使用静默错误报告。在此模式下,不会打印任何错误消息。如果看到无效选项,getopts 会将找到的选项字符放入 OPTARG。如果未找到必需的参数,getopts 将一个 ' :' 放入 NAME 并将 OPTARG 设置为找到的选项字符。如果 getopts 未处于静默模式,并且看到无效选项,则 getopts 放置 '?' 进入 NAME 并取消设置 OPTARG。如果未找到必需的参数,则使用 ' ? ' 放在 NAME 中,OPTARG 未设置,并打印诊断消息。
如果 shell 变量
OPTERR的值为 0,即使 OPTSTRING 的第一个字符不是冒号,getopts 也会禁用错误消息的打印。默认情况下,OPTERR 的值为 1。Getopts 通常解析位置参数 ($0 - $9),但如果给出更多参数,则会改为解析它们。
退出状态:
如果找到选项则返回成功;如果遇到选项结束或发生错误,则失败。
回答by cmbuckley
You can use getoptsto parse command-line arguments. This tutorialis quite useful to get started with.
您可以使用getopts来解析命令行参数。本教程非常适合入门。
回答by Akif
This simple script takes the port number with pand ip address with iparameter.
这个简单的脚本使用p带有i参数的端口号和 ip 地址。
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
Can be executed in either ways:
可以通过以下任一方式执行:
./myscript -i 192.168.1.1 -p 1985
./myscript -i 192.168.1.1 -p 1985
or
或者
./myscript -p 1985 -i 192.168.1.1
./myscript -p 1985 -i 192.168.1.1
When executed it prints:
执行时打印:
ip address: 192.168.1.1 port number: 1985
Also as mentioned at http://wiki.bash-hackers.org/howto/getopts_tutorial
同样如http://wiki.bash-hackers.org/howto/getopts_tutorial 所述
Note that getopts is not able to parse GNU-style long options (--myoption) or XF86-style long options (-myoption)
请注意,getopts 无法解析 GNU 样式的长选项 (--myoption) 或 XF86 样式的长选项 (-myoption)
So you cannot use long strings as --portor --ipdirectly with getopts.
However additional workarounds available as described in this link:
http://www.bahmanm.com/blogs/command-line-options-how-to-parse-in-bash-using-getopt
所以你不能使用长字符串 as--port或--ip直接使用getopts. 但是,此链接中描述的其他解决方法可用:http:
//www.bahmanm.com/blogs/command-line-options-how-to-parse-in-bash-using-getopt

