这个bash脚本摘录中的双破折号是做什么的 [英] What is the double dash in this bash script excerpt doing

查看:40
本文介绍了这个bash脚本摘录中的双破折号是做什么的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在查看bash的摘录,我试图了解这里到底发生了什么,特别是在 COMPREPLY 作业中:

I'm looking at an excerpt of bash and I'm trying to understand what exactly is happening here, particularly in the COMPREPLY assignment:

 case "$cur" in
    -*)
    COMPREPLY=( $( compgen -W '-a -d -f -l -t -h --aoption --debug \
                               --file --log --test --help --' -- $cur ) );;
#   Generate the completion matches and load them into $COMPREPLY array.
#   xx) May add more cases here.
#   yy)
#   zz)
  esac

我了解我们在这里根据 compgen 的输出为 COMPREPLY 分配一个值,但是-到底在做什么这项作业?另外,为什么要使用双分号?

I understand we're assigning a value to COMPREPLY here based on the output of compgen, but what exactly is -- doing in this assignment? Also, why the double semi-colon?

推荐答案

compgen bash 内置命令,手册页中说:

compgen is a bash builtin command, and the man pages say:

除非另有说明,否则本节中记录为接受选项的每个内置命令均以-接受-开头,以表示这些选项的结尾.

Unless otherwise noted, each builtin command documented in this section as accepting options preceded by - accepts -- to signify the end of the options.

由于 $ cur 是一个包含选项的变量(以-开头),所以需要-(如man中所述)页)以区分 compgen 选项和要处理的输入.

Since $cur is a variable that contains options (starting with -), the -- is required (as mentioned in the man pages) to make a distinction between compgen options and input to be processed.

以下命令打开了-debug 中的-d 选项:

The following command is turning the option --d in --debug:

compgen -W '-a -d -f -l -t -h --aoption --debug --file --log --test --help --' -- --d
--debug

如果删除分隔符-,该命令将引发错误,因为 compgen 没有任何以-d :

If you remove the separator --, the command throws an error, because compgen doesn't have any option starting with --d:

compgen -W '-a -d -f -l -t -h --aoption --debug --file --log --test --help --' --d
-bash: compgen: --: invalid option
compgen: usage: compgen [-abcdefgjksuv] [-o option]  [-A action] [-G globpat] [-W wordlist]  [-F function] [-C command] [-X filterpat] [-P prefix] [-S suffix] [word]


;; 是在 case esac 语句中使用的分隔符,以在您的示例中终止以-开头的部分*).在 bash 手册页中找到 Compound Commands 以获得更多信息.


The ;; is a separator used in the case, esac statement to terminate in your example the section started with -*). Look at bash man pages for Compound Commands to get more info.

这篇关于这个bash脚本摘录中的双破折号是做什么的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆