Bash 优化列出带有手动页面的可用命令的脚本

Bash 优化列出带有手动页面的可用命令的脚本,bash,optimization,Bash,Optimization,我正在使用这个脚本生成一个可用命令列表,其中包含系统上的手动页面。用时间运行此程序时,我的计算机上显示的平均时间约为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) =~ 'N

我正在使用这个脚本生成一个可用命令列表,其中包含系统上的手动页面。用
时间运行此程序时,我的计算机上显示的平均时间约为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

完全无视这里的内置功能。不管怎么说,
就是这样做的。脚本没有经过彻底测试

#!/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的完整路径有太多未命中):

(两个版本的注意事项:Cygwin的
/usr/bin
中的大多数内容都带有
.exe
扩展名)

$ 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