我使用这个脚本在系统上生成一个带有手册页的可用命令列表。在我的电脑上,用time运行这个程序平均大约需要49秒。
#!/usr/local/bin/bash
for x in $(for f in $(compgen -c); do which $f; done | sort -u); do 
    dir=$(dirname $x)
    cmd=$(basename $x) 
    if [[ ! $(man --path "$cmd" 2>&1) =~ 'No manual entry' ]]; then 
        printf '%b\n' "${dir}:\n${cmd}"
    fi
done | awk '!x[$0]++'有没有办法优化它以获得更快的结果?
这是我当前输出的一个小样本。目标是按目录对命令进行分组。它稍后将被输入到一个数组中。
/bin:    # directories generated by $dir
[        # commands generated by $cmd (compgen output)
cat
chmod
cp
csh
date发布于 2017-10-16 02:40:09
完全忽略了这里的内置。不管怎么说,这就是which所做的。脚本未经过全面测试。
#!/bin/bash
shopt -s nullglob # need this for "empty" checks below
MANPATH=${MANPATH:-/usr/share/man:/usr/local/share/man}
IFS=: # chunk up PATH and MANPATH, both colon-deliminated
# just look at the directory!
has_man_p() {
    local needle=$1 manp manpp result=()
    for manp in $MANPATH; do
        # man? should match man0..man9 and a bunch of single-char things
        # do we need 'man?*' for longer suffixes here?
        for manpp in "$manp"/man?; do
            # assumption made for filename formats. section not checked.
            result=("$manpp/$needle".*)
            if (( ${#result[@]} > 0 )); then
                return 0
            fi
        done
    done
    return 1
}
unset seen
declare -A seen # for deduplication
for p in $PATH; do
    printf '%b:\n' "$p" # print the path first
    for exe in "$p"/*; do
        cmd=${exe##*/}  # the sloppy basename
        if [[ ! -x $exe || ${seen[$cmd]} == 1 ]]; then
            continue
        fi
        seen["$cmd"]=1
        if has_man_p "$cmd"; then
            printf '%b\n' "$cmd"
        fi
    done
done在Cygwin上使用截断路径的时间( Windows的完整路径对于原始版本来说有太多的遗漏):
$ export PATH=/usr/local/bin:/usr/bin
$ time (sh ./opti.sh &>/dev/null)
real    0m3.577s
user    0m0.843s
sys     0m2.671s
$ time (sh ./orig.sh &>/dev/null)
real    2m10.662s
user    0m20.138s
sys     1m5.728s(两个版本都需要注意:Cygwin的/usr/bin中的大多数东西都带有一个.exe扩展)
https://stackoverflow.com/questions/44879195
复制相似问题