Linux 中 10 个有用的命令行补全例子

在 Linux 系统中,当你输入一个命令,再按两次 TAB 键,就会列出所有以你输入字符开头的可用命令。这并不新鲜,可能你已经知道了。这个功能被称作命令行补全bash completion。默认情况下,bash 命令行可以自动补全文件或目录名称。不过,我们可以增强 bash 命令补全功能,通过 complete 命令让它达到新的高度。

Linux 中 10 个有用的命令行补全例子

这个教程说明了我们是怎样使用可编程的命令行补全功能(programmable completion)把自动补全功能应用于选项或者命令行参数。

例如:在输入 write 命令之后,如果你按两次 TAB 按键,自动补全功能会提供可供执行 write 操作的用户列表。

  1. <span class="pln">$ </span><span class="kwd">write</span><span class="pun">[</span><span class="pln">TAB</span><span class="pun">][</span><span class="pln">TAB</span><span class="pun">]</span>
  2. <span class="pln">bala raj</span>
  3. <span class="pln">jason randy</span>
  4. <span class="pln">john ritu</span>
  5. <span class="pln">mayla thomas</span>
  6. <span class="pln">nisha www</span><span class="pun">-</span><span class="pln">data</span>

在下面的例子中,可以为 telnet 命令显示可用的主机名:

  1. <span class="pln">$ telnet </span><span class="pun">[</span><span class="pln">TAB</span><span class="pun">][</span><span class="pln">TAB</span><span class="pun">]</span>
  2. <span class="pln">localhost dev</span><span class="pun">-</span><span class="pln">db fileserver</span>

要让可编程命令补全功能在你的终端起作用 ,你只需要如下执行/etc/bash_completion即可:

  1. <span class="com">#</span><span class="pun">.</span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">bash_completion</span>

你也可以取消/etc/bash.bashrc(来自 Ubuntu Linux 13.04 系统)中如下的注释,这样,你就可以不需要执行上面的命令了:

  1. <span class="com">### enable bash completion in interactive shells</span>
  2. <span class="kwd">if</span><span class="pun">!</span><span class="pln"> shopt </span><span class="pun">-</span><span class="pln">oq posix</span><span class="pun">;</span><span class="kwd">then</span>
  3. <span class="kwd">if</span><span class="pun">[</span><span class="pun">-</span><span class="pln">f </span><span class="pun">/</span><span class="pln">usr</span><span class="pun">/</span><span class="pln">share</span><span class="pun">/</span><span class="kwd">bash</span><span class="pun">-</span><span class="pln">completion</span><span class="pun">/</span><span class="pln">bash_completion </span><span class="pun">];</span><span class="kwd">then</span>
  4. <span class="pun">.</span><span class="pun">/</span><span class="pln">usr</span><span class="pun">/</span><span class="pln">share</span><span class="pun">/</span><span class="kwd">bash</span><span class="pun">-</span><span class="pln">completion</span><span class="pun">/</span><span class="pln">bash_completion</span>
  5. <span class="kwd">elif</span><span class="pun">[</span><span class="pun">-</span><span class="pln">f </span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">bash_completion </span><span class="pun">];</span><span class="kwd">then</span>
  6. <span class="pun">.</span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">bash_completion</span>
  7. <span class="kwd">fi</span>
  8. <span class="kwd">fi</span>

如果你没有发现这些代码,也没有找到/etc/bash_completion文件,那么你只需要通过使用apt-get命令来安装bash_completion 包即可。

1. 查看已有的命令行补全

在启用可编程的命令行补全功能后,就已经有了一些定义好的命令补全功能。complete 命令用于定义命令行补全。

要查看已有的命令行补全,如下使用 complete 命令:

  1. <span class="pln">complete </span><span class="pun">-</span><span class="pln">p </span><span class="pun">|</span><span class="kwd">less</span>

上面例子中的 -p 选项是可选的。

2. 列出 bash 中标准补全功能

默认情况下,Bash 为 Linux 用户提供了下列标准补全功能。

  1. 变量补全
  2. 用户名补全
  3. 主机名补全
  4. 路径补全
  5. 文件名补全

我们在之前的  bash 标准补全中讨论过这些。

3. 定义一个命令名补全

通过 -c 选项可以将所有的可用命令作为一个命令的补全参数。在下面的例子里面,为 which 命令定义了一个补全(LCTT译注:在按两下 TAB 时,可以列出所有命令名作为可补全的参数)。

  1. <span class="pln">$ complete </span><span class="pun">-</span><span class="pln">c which</span>
  2. <span class="pln">$ which </span><span class="pun">[</span><span class="pln">TAB</span><span class="pun">][</span><span class="pln">TAB</span><span class="pun">]</span>
  3. <span class="typ">Display</span><span class="pln"> all </span><span class="lit">2116</span><span class="pln"> possibilities</span><span class="pun">?</span><span class="pun">(</span><span class="pln">y </span><span class="kwd">or</span><span class="pln"> n</span><span class="pun">)</span>

如上,如果按下 ‘y’,就会列出所有的命令名。

4. 定义一个目录补全

通过选项 -d,可以定义一个仅包含目录名的补全参数。在下面的例子中,为 ls 命令定义了补全。

  1. <span class="pln">$ </span><span class="kwd">ls</span>
  2. <span class="pln">countfiles</span><span class="pun">.</span><span class="pln">sh dir1</span><span class="pun">/</span><span class="pln"> dir2</span><span class="pun">/</span><span class="pln"> dir3</span><span class="pun">/</span>
  3. <span class="pln">$ complete </span><span class="pun">-</span><span class="pln">d </span><span class="kwd">ls</span>
  4. <span class="pln">$ </span><span class="kwd">ls</span><span class="pun">[</span><span class="pln">TAB</span><span class="pun">][</span><span class="pln">TAB</span><span class="pun">]</span>
  5. <span class="pln">dir1</span><span class="pun">/</span><span class="pln"> dir2</span><span class="pun">/</span><span class="pln"> dir3</span><span class="pun">/</span>

如上,连按下 TAB 仅会显示目录名。

5. 定义一个后台任务名补全

补全功能也能够以任务名作为补全参数。选项 -j 可以定义任务名作为传递给命令的参数,如下:

  1. <span class="pln">$ jobs</span>
  2. <span class="pun">[</span><span class="lit">1</span><span class="pun">]-</span><span class="typ">Stopped</span><span class="kwd">cat</span>
  3. <span class="pun">[</span><span class="lit">2</span><span class="pun">]+</span><span class="typ">Stopped</span><span class="kwd">sed</span><span class="str">'p'</span>
  4. <span class="pln">$ complete </span><span class="pun">-</span><span class="pln">j </span><span class="pun">./</span><span class="pln">list_job_attrib</span><span class="pun">.</span><span class="pln">sh</span>
  5. <span class="pln">$ </span><span class="pun">./</span><span class="pln">list_job_attrib</span><span class="pun">.</span><span class="pln">sh </span><span class="pun">[</span><span class="pln">TAB</span><span class="pun">][</span><span class="pln">TAB</span><span class="pun">]</span>
  6. <span class="kwd">cat</span><span class="kwd">sed</span>

关于后台任务,你可以参考 Linux 后台任务中的例子了解如何管理后台任务。

6. 带有前缀和后缀的补全

补全功能可以为实际的补全内容定义前缀和后缀。在下面的例子中,为 list_job_attrib.sh 定义了补全内容的前缀和后缀。

  1. <span class="pln">$ jobs </span>
  2. <span class="pun">[</span><span class="lit">1</span><span class="pun">]+</span><span class="typ">Stopped</span><span class="kwd">cat</span>
  3. <span class="pln">$ complete </span><span class="pun">-</span><span class="pln">P </span><span class="str">'">'</span><span class="pun">-</span><span class="pln">S </span><span class="str">'<"'</span><span class="pun">./</span><span class="pln">list_job_attrib</span><span class="pun">.</span><span class="pln">sh</span>
  4. <span class="pln">$ </span><span class="pun">./</span><span class="pln">list_job_attrib</span><span class="pun">.</span><span class="pln">sh </span><span class="pun">[</span><span class="pln">TAB</span><span class="pun">][</span><span class="pln">TAB</span><span class="pun">]</span>
  5. <span class="pln">$ </span><span class="pun">./</span><span class="pln">list_job_attrib</span><span class="pun">.</span><span class="pln">sh </span><span class="str">">cat<"</span>

7. 带有排除的文件名和目录名补全

假如脚本运行完成后,输出目录如下:

  1. <span class="pln">$ </span><span class="kwd">cd</span><span class="pln"> output</span><span class="pun">/</span>
  2. <span class="pln">$ </span><span class="kwd">ls</span>
  3. <span class="pln">all_calls</span><span class="pun">.</span><span class="pln">txt incoming_calls</span><span class="pun">.</span><span class="pln">txt outgoing_calls</span><span class="pun">.</span><span class="pln">txt missed_calls</span><span class="pun">.</span><span class="pln">txt</span>
  4. <span class="pln">parser_mod</span><span class="pun">.</span><span class="pln">tmp extract</span><span class="pun">.</span><span class="pln">o</span>

如上,如果你想要 ls 命令的补全忽略 .tmp 和 .o 文件:

  1. <span class="pln">$ </span><span class="kwd">export</span><span class="pln"> FIGNORE</span><span class="pun">=</span><span class="str">'.tmp:.o'</span>
  2. <span class="pln">$ complete </span><span class="pun">-</span><span class="pln">f </span><span class="pun">-</span><span class="pln">d </span><span class="kwd">ls</span>
  3. <span class="pln">$ </span><span class="kwd">cd</span><span class="pln"> output</span>
  4. <span class="pln">$ </span><span class="kwd">ls</span><span class="pun">[</span><span class="pln">TAB</span><span class="pun">][</span><span class="pln">TAB</span><span class="pun">]</span>
  5. <span class="pln">all_calls</span><span class="pun">.</span><span class="pln">txt incoming_calls</span><span class="pun">.</span><span class="pln">txt outgoing_calls</span><span class="pun">.</span><span class="pln">txt missed_calls</span><span class="pun">.</span><span class="pln">txt</span>

FIGNORE 是一个环境变量,它包含了自动补全所需要排除的文件名后缀。

8. 通过 IFS 变量分割字符串得到补全值

可以通过 -W 选项定义补全值列表,然后通过 IFS 环境变量进行切分。切分结果会展开变量并作为补全显示。

  1. <span class="pln">$ </span><span class="kwd">export</span><span class="pln"> IFS</span><span class="pun">=</span><span class="str">" "</span>
  2. <span class="pln">$ complete </span><span class="pun">-</span><span class="pln">W </span><span class="str">"bubble quick"</span><span class="pun">./</span><span class="pln">sort_numbers</span><span class="pun">.</span><span class="pln">sh</span>
  3. <span class="pln">$ </span><span class="pun">./</span><span class="pln">sort_numbers</span><span class="pun">.</span><span class="pln">sh </span><span class="pun">[</span><span class="pln">TAB</span><span class="pun">][</span><span class="pln">TAB</span><span class="pun">]</span>
  4. <span class="pln">bubble quick</span>

如上所述,字符串通过 IFS 分隔符进行切分后,内嵌的变量会被展开为变量值,所以可以如下使用变量:

  1. <span class="pln">$ </span><span class="kwd">echo</span><span class="pln"> $SORT_TYPE1</span>
  2. <span class="pln">bubble</span>
  3. <span class="pln">$ </span><span class="kwd">echo</span><span class="pln"> $SORT_TYPE2</span>
  4. <span class="pln">quick</span>
  5. <span class="pln">$ complete </span><span class="pun">-</span><span class="pln">W </span><span class="str">"$SORT_TYPE1 $SORT_TYPE2"</span><span class="pun">./</span><span class="pln">sort_numbers</span><span class="pun">.</span><span class="pln">sh</span>
  6. <span class="pln">$ </span><span class="pun">./</span><span class="pln">sort_numbers</span><span class="pun">.</span><span class="pln">sh </span><span class="pun">[</span><span class="pln">TAB</span><span class="pun">][</span><span class="pln">TAB</span><span class="pun">]</span>
  7. <span class="pln">bubble quick</span>

9. 写个函数来生成补全

你可以引入一个函数来定义补全。使用 -F 选项将函数名传给 complete 命令,执行函数生成补全内容。例如,函数如下:

  1. <span class="pln">_parser_options</span><span class="pun">()</span>
  2. <span class="pun">{</span>
  3. <span class="kwd">local</span><span class="pln"> curr_arg</span><span class="pun">;</span>
  4. <span class="pln">curr_arg</span><span class="pun">=</span><span class="pln">$</span><span class="pun">{</span><span class="pln">COMP_WORDS</span><span class="pun">[</span><span class="pln">COMP_CWORD</span><span class="pun">]}</span>
  5. <span class="pln">COMPREPLY</span><span class="pun">=(</span><span class="pln"> $</span><span class="pun">(</span><span class="pln">compgen </span><span class="pun">-</span><span class="pln">W </span><span class="str">'-i --incoming -o --outgoing -m --missed'</span><span class="pun">--</span><span class="pln"> $curr_arg </span><span class="pun">)</span><span class="pun">);</span>
  6. <span class="pun">}</span>

在上述函数中:

  1. COMPREPLY : 该数组控制连按下 TAB 后显示的结果
  2. COMP_WORDS : 该数组包含命令行输入的单词
  3. COMP_CWORD : COMP_WORDS 数组的索引,使用它来区分命令行可以访问的单词位置
  4. compgen : -W 基于 $current_arg 提供可能的补全及其参数

该函数放在 parser_option 文件中,并通过 source 命令引入:

  1. <span class="pln">$ source parser_option</span>

将该函数和你的 parser.pl 脚本关联起来:

  1. <span class="pln">$ complete </span><span class="pun">-</span><span class="pln">F _parser_options </span><span class="pun">./</span><span class="pln">parser</span><span class="pun">.</span><span class="pln">pl</span>
  2. <span class="pln">$ </span><span class="pun">./</span><span class="pln">parser</span><span class="pun">.</span><span class="pln">pl </span><span class="pun">[</span><span class="pln">TAB</span><span class="pun">][</span><span class="pln">TAB</span><span class="pun">]</span>
  3. <span class="pun">-</span><span class="pln">i </span><span class="pun">--</span><span class="pln">incoming </span><span class="pun">-</span><span class="pln">o </span><span class="pun">--</span><span class="pln">outgoing </span><span class="pun">-</span><span class="pln">m </span><span class="pun">--</span><span class="pln">missed</span>

如上,parser.pl 的选项是由函数 _parser_options() 生成的。

提示: 查看/etc/bash_completion 来了解更多的可编程补全函数。

10. 当第一个规则没有生成结果时,就使用第二个

如果定义的补全规则没有生成匹配时,可以使用 -o 选项生成补全。

  1. <span class="pln">$ complete </span><span class="pun">-</span><span class="pln">F _count_files </span><span class="pun">-</span><span class="pln">o dirnames </span><span class="pun">./</span><span class="pln">countfiles</span><span class="pun">.</span><span class="pln">sh</span>

如上,为 ./countfiles.sh 定义了 _count_files 补全函数。 如果 the _count_files() 函数没有生成任何匹配的话,就会触发目录补全。

  1. <span class="pln">$ </span><span class="kwd">ls</span>
  2. <span class="pln">countfiles</span><span class="pun">.</span><span class="pln">sh dir1</span><span class="pun">/</span><span class="pln"> dir2</span><span class="pun">/</span><span class="pln"> dir3</span><span class="pun">/</span>
  3. <span class="pln">$</span><span class="pun">./</span><span class="pln">countfiles</span><span class="pun">.</span><span class="pln">sh </span><span class="pun">[</span><span class="pln">TAB</span><span class="pun">][</span><span class="pln">TAB</span><span class="pun">]</span>
  4. <span class="pln">dir1 dir2 dir3</span>

相关推荐