本文共 1536 字,大约阅读时间需要 5 分钟。
据我所知,使用--的结束选项-标记开头sh,并getopt在系统III UNIX(1980)。
根据Bourne Shell家族的历史,Bourne Shell 首次出现在Unix 7版(1979年)中。但它没有一种方式set,以从分隔参数选项。因此原始的Bourne外壳可以做到:
set -e -打开错误退出模式
set arg1 arg2 ...-设置位置参数$1=arg1,$2=arg2等等。
但是:set arg1 -e arg2会给您$1=arg1,$2=arg2然后打开错误退出。哎呀
System III Unix(1980)修复了该错误,并进行了介绍getopt。根据getopt的手册页:
NAME
getopt - parse command options
SYNOPSIS
set -- `getopt optstring $∗`
DESCRIPTION
Getopt is used to break up options in command lines for easy parsing by
shell procedures, and to check for legal options. Optstring is a
string of recognized option letters (see getopt(3C)); if a letter is
followed by a colon, the option is expected to have an argument which
may or may not be separated from it by white space. The special option
-- is used to delimit the end of the options. Getopt will place -- in
the arguments at the end of the options, or recognize it if used
explicitly. The shell arguments ($1 $2 . . .) are reset so that each
option is preceded by a - and in its own shell argument; each option
argument is also in its own shell argument.
据我所知,这是它出现的第一位。
从那里开始,似乎其他命令都采用了--约定来解决参数解析歧义的问题(例如上面touch和rm您在上面引用的示例)在整个1980年代疯狂,无标准的日子。
其中一些零星采用的方法已在POSIX.1(1988)中进行了编纂,这是关于“ POSIX必需的合并”的变更日志注释的来源。
但是直到POSIX.2(1992)才采用实用程序语法准则,其中包含著名的准则10:
Guideline 10: The argument "--" should be accepted as a delimiter
indicating the end of options. Any following
arguments should be treated as operands, even if they
begin with the '-' character. The "--" argument
should not be used as an option or as an operand.
这就是从“ being脚”到普遍推荐的地步。
转载地址:https://blog.csdn.net/weixin_30496999/article/details/117130505 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!