加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

在bash中,我如何以任意随机顺序拥有一组参数?像一个键值对?

发布时间:2020-12-16 01:11:02 所属栏目:安全 来源:网络整理
导读:例: ./myscript --ip 192.168.1.1 --port 1985 或另一种可能的 ./myscript --port 1985 --ip 192.168.1.1 我想允许我的脚本以任何顺序获取一组参数 ./myscript a b c d./myscript d c b a./myscript b d a c 诸如此类 看看 getopts getopts : getopts optst
例:
./myscript --ip 192.168.1.1 --port 1985

或另一种可能的

./myscript --port 1985 --ip 192.168.1.1

我想允许我的脚本以任何顺序获取一组参数

./myscript a b c d
./myscript d c b a
./myscript b d a c

诸如此类

看看 getopts

getopts: getopts optstring name [arg]
Parse option arguments.

Getopts is used by shell procedures to parse positional parameters
as options.

OPTSTRING contains 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 OPTERR has 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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读