shell 从命令帮助中查找某些选项

查看命令帮助时,满屏的选项,而有时,我们只想查看某几个选项的意思。我们可以通过管道查过滤形如: cmmand --help | grep -- '-d' ,如果是几个选项,那就不太方便了。

看我的实现效果

 

function see(){

    local cmd=""
    local end=0
    echo "------------------------------------------------------------------------------------------"
    for arg in "$@"; do
    
        if [ "$end" == 0 ];then
            cmd="$cmd $arg"
            shift 1
            [ "${arg:0:1}" == "-" ] && { [ -z "$1" ] && eval $cmd  || { end=1; cmd="$cmd | grep --"; } }
        else
            if [ "${arg:0:1}" == "-" -a "${arg:0:2}" != "--" ];then
                arg="${arg:1}"
                for (( i = 0; i < ${#arg}; i = i + 1 )) do
                    eval $cmd "-${arg:$i:1}"
                    echo "------------------------------------------------------------------------------------------"
                done
            else
                eval $cmd $arg
                echo "------------------------------------------------------------------------------------------"
            fi

        fi

    done

}

 

posted @ 2020-12-20 16:05  心随所遇  阅读(167)  评论(0编辑  收藏  举报