优化使用手册页列出可用命令的脚本

时间:2017-07-03 06:39:15

标签: bash optimization

我正在使用此脚本生成系统上包含手册页的可用命令列表。使用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

1 个答案:

答案 0 :(得分:0)

在这里完全无视内置插件。无论如何,那是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

使用截断的PATH在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扩展名