2009-02-04 102 views

回答

30

您应该能够使用compgen命令,就像这样:

compgen -A builtin [YOUR STRING HERE] 

例如, “compgen -A内置L” 返回

let 
local 
logout 

您可以使用其他关键字“内建”的地方获得其他类型的完成。内建给你shell内建的命令。 “文件”给你的本地文件名等

这里的(针对使用compgen完整从BASH手册页)的操作的列表:

alias  Alias names. May also be specified as -a. 
    arrayvar Array variable names. 
    binding Readline key binding names. 
    builtin Names of shell builtin commands. May also be specified as -b. 
    command Command names. May also be specified as -c. 
    directory Directory names. May also be specified as -d. 
    disabled Names of disabled shell builtins. 
    enabled Names of enabled shell builtins. 
    export  Names of exported shell variables. May also be specified as -e. 
    file  File names. May also be specified as -f. 
    function Names of shell functions. 
    group  Group names. May also be specified as -g. 
    helptopic Help topics as accepted by the help builtin. 
    hostname Hostnames, as taken from the file specified by the HOSTFILE shell 
       variable. 
    job  Job names, if job control is active. May also be specified as 
       -j. 
    keyword Shell reserved words. May also be specified as -k. 
    running Names of running jobs, if job control is active. 
    service Service names. May also be specified as -s. 
    setopt  Valid arguments for the -o option to the set builtin. 
    shopt  Shell option names as accepted by the shopt builtin. 
    signal  Signal names. 
    stopped Names of stopped jobs, if job control is active. 
    user  User names. May also be specified as -u. 
    variable Names of all shell variables. May also be specified as -v. 
+0

不错。我为此而努力并完成,但一直未能弄清楚如何使用它。 +1 :) – 2009-02-04 15:31:25

+0

确实非常有用,并且非常好的答案。谢谢! (+1) – 2009-02-04 16:45:49

0

遍历$ PATH变量和路径中的每个目录做ls beginningofword*

为了得到它完全等同,你将需要过滤出可执行文件和姓名排序(应与LS标志和sort命令很容易)。

0

当你打在你的路径与字符串开始二进制文件什么是上市。因此,如果PATH变量包含: PATH =/usr/local/bin:/ usr/bin:/ bin:/ usr/games:/ usr/lib/java/bin:/ usr/lib/java/jre/bin :/ usr/lib中/ QT /斌:在/ usr /共享/ TEXMF /斌:。

Bash会在每个目录中查找,以便在您点击时向您显示建议。因此,要使开始 “LS” 到一个变量,你可以做的命令列表: MYVAR = $(LS在/ usr/local/bin目录/ LS *的/ usr/bin中/ LS * /斌/ LS *) 自然你可以添加所有其他我没有的目录。

+0

看到我的答案下面的快速解析$ PATH(不是我的想法,我偷了它)... – dmckee 2009-02-04 18:45:22

1

如果正是你想要的bash会怎样完成

COMPLETIONS=$(compgen -c "$WORD") 

compgen完成时使用相同的规则bash在选项卡上使用。

1

JacobM的回答非常好。为了做到这一点,我会用这样的东西:

echo $PATH | tr : '\n' | 
while read p; do 
    for i in $p/mod*; do 
    [[ -x "$i" && -f "$i" ]] && echo $i  
    done 
done 

输出前的测试确保只显示可执行文件,常规文件。以上显示了所有以mod开头的命令。

1

有趣的,我不知道compgen。在这里,我用来做什么的一个脚本,它不适用于非可执行文件检查:在您的$PATH(我把它命名为findcmd)某处

#!/bin/bash 
echo $PATH | tr ':' '\0' | xargs -0 ls | grep "[email protected]" | sort 

保存该脚本,chmod u+w它,然后使用它,像grep,通过你喜欢的选项和模式:

findcmd ^foo # finds all commands beginning with foo 
findcmd -i -E 'ba+r' # finds all commands matching the pattern 'ba+r', case insensitively 
0

只是为了好玩,另一个手动变体:

find -L $(echo $PATH | tr ":" " ") -name 'pattern' -type f -perm -001 -print 

其中pattern指定要使用的文件名称模式。这将会错过不是全局可执行的命令,但是您有权限执行命令。

find -L $(echo $PATH | tr ":" " ") -name 'pattern' -type f 
            \(\ 
             -perm -001 -or \ 
             \(-perm -100 -and -user $(whoami)\) \ 
            \) -print 

将拿起文件,您有权通过:


使用-or-and标志来建立这个命令的更全面的版本[在Mac OS X测试]拥有它们的美德。我没有看到一个通用的方式来获得所有你可以凭借团队联系而执行的,而没有更多的编码。

2

一个有趣的方式来做到这一点是打M-*(元通常是左Alt)。

作为一个例子,输入:

$ lo 

然后点击M-*

$ loadkeys loadunimap local locale localedef locale-gen locate 
    lockfile-create lockfile-remove lockfile-touch logd logger login 
    logname logout logprof logrotate logsave look lorder losetup 

您可以阅读man 3 readline更多关于这一点;这是readline库的一项功能。

相关问题