sh Bash选项卡完成代码段

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sh Bash选项卡完成代码段相关的知识,希望对你有一定的参考价值。

# ssh(1) completion                                        -*- shell-script -*-

_ssh_queries()
{
    COMPREPLY+=( $( compgen -W \
        "cipher cipher-auth mac kex key protocol-version" -- "$cur" ) )
}

_ssh_query()
{
    ${1:-ssh} -Q $2 2>/dev/null
}

_ssh_ciphers()
{
    local ciphers='$( _ssh_query "$1" cipher )'
    [[ $ciphers ]] || ciphers="3des-cbc aes128-cbc aes192-cbc aes256-cbc
        aes128-ctr aes192-ctr aes256-ctr arcfour128 arcfour256 arcfour
        blowfish-cbc cast128-cbc"
    COMPREPLY+=( $( compgen -W "$ciphers" -- "$cur" ) )
}

_ssh_macs()
{
    local macs='$( _ssh_query "$1" mac )'
    [[ $macs ]] || macs="hmac-md5 hmac-sha1 umac-64@openssh.com hmac-ripemd160
        hmac-sha1-96 hmac-md5-96"
    COMPREPLY+=( $( compgen -W "$macs" -- "$cur" ) )
}

_ssh_options()
{
    compopt -o nospace
    COMPREPLY=( $( compgen -S = -W 'AddressFamily BatchMode BindAddress
        ChallengeResponseAuthentication CheckHostIP Cipher Ciphers
        ClearAllForwardings Compression CompressionLevel ConnectionAttempts
        ConnectTimeout ControlMaster ControlPath ControlPersist DynamicForward
        EnableSSHKeysign EscapeChar ExitOnForwardFailure ForwardAgent
        ForwardX11 ForwardX11Timeout ForwardX11Trusted GatewayPorts
        GlobalKnownHostsFile GSSAPIAuthentication GSSAPIClientIdentity
        GSSAPIDelegateCredentials GSSAPIKeyExchange GSSAPIRenewalForcesRekey
        GSSAPIServerIdentity GSSAPITrustDns HashKnownHosts Host
        HostbasedAuthentication HostKeyAlgorithms HostKeyAlias HostName
        IdentityFile IdentitiesOnly IPQoS KbdInteractiveDevices KexAlgorithms
        LocalCommand LocalForward LogLevel MACs
        NoHostAuthenticationForLocalhost NumberOfPasswordPrompts ProxyJump
        PasswordAuthentication PermitLocalCommand PKCS11Provider Port
        PreferredAuthentications Protocol ProxyCommand PubkeyAuthentication
        RekeyLimit RemoteForward RequestTTY RhostsRSAAuthentication
        RSAAuthentication SendEnv ServerAliveCountMax ServerAliveInterval
        SmartcardDevice StrictHostKeyChecking TCPKeepAlive Tunnel TunnelDevice
        UsePrivilegedPort User UserKnownHostsFile VerifyHostKeyDNS
        VisualHostKey XAuthLocation' -- "$cur" ) )
}

# Complete a ssh suboption (like ForwardAgent=y<tab>)
# Two parameters: the string to complete including the equal sign, and
# the ssh executable to invoke (optional).
# Not all suboptions are completed.
# Doesn't handle comma-separated lists.
_ssh_suboption()
{
    # Split into subopt and subval
    local prev=${1%%=*} cur=${1#*=}

    case $prev in
        BatchMode|ChallengeResponseAuthentication|CheckHostIP|\
        ClearAllForwardings|ControlPersist|Compression|EnableSSHKeysign|\
        ExitOnForwardFailure|ForwardAgent|ForwardX11|ForwardX11Trusted|\
        GatewayPorts|GSSAPIAuthentication|GSSAPIKeyExchange|\
        GSSAPIDelegateCredentials|GSSAPIRenewalForcesRekey|GSSAPITrustDns|\
        HashKnownHosts|HostbasedAuthentication|IdentitiesOnly|\
        KbdInteractiveAuthentication|KbdInteractiveDevices|\
        NoHostAuthenticationForLocalhost|PasswordAuthentication|\
        PubkeyAuthentication|RhostsRSAAuthentication|RSAAuthentication|\
        StrictHostKeyChecking|TCPKeepAlive|UsePrivilegedPort|\
        VerifyHostKeyDNS|VisualHostKey)
            COMPREPLY=( $( compgen -W 'yes no' -- "$cur" ) )
            ;;
        AddressFamily)
            COMPREPLY=( $( compgen -W 'any inet inet6' -- "$cur" ) )
            ;;
        BindAddress)
            _ip_addresses
            ;;
        Cipher)
            COMPREPLY=( $( compgen -W 'blowfish des 3des' -- "$cur" ) )
            ;;
        ProxyJump)
            _known_hosts_real -a -F "$configfile" -- "$cur"
            ;;
        IPQoS)
            COMPREPLY=( $( compgen -W 'af1{1..4} af2{2..3} af3{1..3} af4{1..3}
                cs{0..7} ef lowdelay throughput reliability' -- "$cur" ) )
            ;;
        HostbasedKeyTypes|HostKeyAlgorithms)
            COMPREPLY=( $( compgen -W '$( _ssh_query "$2" key )' -- "$cur" ) )
            ;;
        KexAlgorithms)
            COMPREPLY=( $( compgen -W '$( _ssh_query "$2" kex )' -- "$cur" ) )
            ;;
        Protocol)
            COMPREPLY=( $( compgen -W '1 2 1,2 2,1' -- "$cur" ) )
            ;;
        RequestTTY)
            COMPREPLY=( $( compgen -W 'no yes force auto' -- "$cur" ) )
            ;;
        Tunnel)
            COMPREPLY=( $( compgen -W 'yes no point-to-point ethernet' \
                    -- "$cur" ) )
            ;;
        PreferredAuthentications)
            COMPREPLY=( $( compgen -W 'gssapi-with-mic host-based publickey
                keyboard-interactive password' -- "$cur" ) )
            ;;
        MACs)
            _ssh_macs "$2"
            ;;
        Ciphers)
            _ssh_ciphers "$2"
            ;;
    esac
    return 0
}

# Try to complete -o SubOptions=
#
# Returns 0 if the completion was handled or non-zero otherwise.
_ssh_suboption_check()
{
    # Get prev and cur words without splitting on =
    local cureq=`_get_cword :=` preveq=`_get_pword :=`
    if [[ $cureq == *=* && $preveq == -o ]]; then
        _ssh_suboption $cureq "$1"
        return $?
    fi
    return 1
}

# Search COMP_WORDS for '-F configfile' or '-Fconfigfile' argument
_ssh_configfile()
{
    set -- "${words[@]}"
    while [[ $# -gt 0 ]]; do
        if [[ $1 == -F* ]]; then
            if [[ ${#1} -gt 2 ]]; then
                configfile="$(dequote "${1:2}")"
            else
                shift
                [[ $1 ]] && configfile="$(dequote "$1")"
            fi
            break
        fi
        shift
    done
}

_ssh()
{
    local cur prev words cword
    _init_completion -n : || return

    local -a config

    local configfile
    _ssh_configfile

    _ssh_suboption_check "$1" && return

    local ipvx

    case $prev in
        -F|-i|-S)
            _filedir
            return
            ;;
        -I)
            _filedir so
            return
            ;;
        -c)
            _ssh_ciphers "$1"
            return
            ;;
        -J)
            _known_hosts_real -a -F "$configfile" -- "$cur"
            return
            ;;
        -m)
            _ssh_macs "$1"
            return
            ;;
        -l)
            COMPREPLY=( $( compgen -u -- "$cur" ) )
            return
            ;;
        -O)
            COMPREPLY=( $( compgen -W 'check forward exit stop' -- "$cur" ) )
            return
            ;;
        -o)
            _ssh_options
            return
            ;;
        -Q)
            _ssh_queries "$1"
            return
            ;;
        -w)
            _available_interfaces
            return
            ;;
        -b)
            _ip_addresses
            return
            ;;
        -D|-e|-L|-p|-R|-W)
            return
            ;;
        -4|-6)
            ipvx=$prev
            ;;
    esac

    if [[ "$cur" == -F* ]]; then
        cur=${cur#-F}
        _filedir
        # Prefix completions with '-F'
        COMPREPLY=( "${COMPREPLY[@]/#/-F}" )
        cur=-F$cur  # Restore cur
    elif [[ "$cur" == -* ]]; then
        COMPREPLY=( $( compgen -W '$( _parse_usage "$1" )' -- "$cur" ) )
    else
        _known_hosts_real $ipvx -a -F "$configfile" -- "$cur"

        local args
        _count_args
        if [[ $args -gt 1 ]]; then
            compopt -o filenames
            COMPREPLY+=( $( compgen -c -- "$cur" ) )
        fi
    fi
} &&
shopt -u hostcomplete && complete -F _ssh ssh slogin autossh sidedoor

# sftp(1) completion
#
_sftp()
{
    local cur prev words cword
    _init_completion || return

    local configfile
    _ssh_configfile

    _ssh_suboption_check && return

    local ipvx

    case $prev in
        -b|-F|-i)
            _filedir
            return
            ;;
        -o)
            _ssh_options
            return
            ;;
        -c)
            _ssh_ciphers
            return
            ;;
        -S)
            _command
            return
            ;;
        -B|-D|-l|-P|-R|-s)
            return
            ;;
        -4|-6)
            ipvx=$prev
            ;;
    esac

    if [[ "$cur" == -F* ]]; then
        cur=${cur#-F}
        _filedir
        # Prefix completions with '-F'
        COMPREPLY=( "${COMPREPLY[@]/#/-F}" )
        cur=-F$cur  # Restore cur
    elif [[ "$cur" == -* ]]; then
        COMPREPLY=( $( compgen -W '$( _parse_usage "$1" )' -- "$cur" ) )
    else
        _known_hosts_real $ipvx -a -F "$configfile" -- "$cur"
    fi
} &&
shopt -u hostcomplete && complete -F _sftp sftp

# things we want to backslash escape in scp paths
_scp_path_esc='[][(){}<>",:;^&!$=?`|\\'"'"'[:space:]]'

# Complete remote files with ssh.  If the first arg is -d, complete on dirs
# only.  Returns paths escaped with three backslashes.
_scp_remote_files()
{
    local IFS=$'\n'

    # remove backslash escape from the first colon
    cur=${cur/\\:/:}

    local userhost=${cur%%?(\\):*}
    local path=${cur#*:}

    # unescape (3 backslashes to 1 for chars we escaped)
    path=$( command sed -e 's/\\\\\\\('$_scp_path_esc'\)/\\\1/g' <<<"$path" )

    # default to home dir of specified user on remote host
    if [[ -z $path ]]; then
        path=$(ssh -o 'Batchmode yes' $userhost pwd 2>/dev/null)
    fi

    local files
    if [[ $1 == -d ]]; then
        # escape problematic characters; remove non-dirs
        files=$( ssh -o 'Batchmode yes' $userhost \
            command ls -aF1dL "$path*" 2>/dev/null | \
            command sed -e 's/'$_scp_path_esc'/\\\\\\&/g' -e '/[^\/]$/d' )
    else
        # escape problematic characters; remove executables, aliases, pipes
        # and sockets; add space at end of file names
        files=$( ssh -o 'Batchmode yes' $userhost \
            command ls -aF1dL "$path*" 2>/dev/null | \
            command sed -e 's/'$_scp_path_esc'/\\\\\\&/g' -e 's/[*@|=]$//g' \
            -e 's/[^\/]$/& /g' )
    fi
    COMPREPLY+=( $files )
}

# This approach is used instead of _filedir to get a space appended
# after local file/dir completions, and -o nospace retained for others.
# If first arg is -d, complete on directory names only.  The next arg is
# an optional prefix to add to returned completions.
_scp_local_files()
{
    local IFS=$'\n'

    local dirsonly=false
    if [[ $1 == -d ]]; then
        dirsonly=true
        shift
    fi

    if $dirsonly ; then
        COMPREPLY+=( $( command ls -aF1dL $cur* 2>/dev/null | \
            command sed -e "s/$_scp_path_esc/\\\\&/g" -e '/[^\/]$/d' -e "s/^/$1/") )
    else
        COMPREPLY+=( $( command ls -aF1dL $cur* 2>/dev/null | \
            command sed -e "s/$_scp_path_esc/\\\\&/g" -e 's/[*@|=]$//g' \
            -e 's/[^\/]$/& /g' -e "s/^/$1/") )
    fi
}

# scp(1) completion
#
_scp()
{
    local cur prev words cword
    _init_completion -n : || return

    local configfile
    _ssh_configfile

    _ssh_suboption_check && {
        COMPREPLY=( "${COMPREPLY[@]/%/ }" )
        return
    }

    local ipvx

    case $prev in
        -l|-P)
            return
            ;;
        -F|-i)
            _filedir
            compopt +o nospace
            return
            ;;
        -c)
            _ssh_ciphers
            COMPREPLY=( "${COMPREPLY[@]/%/ }" )
            return
            ;;
        -o)
            _ssh_options
            return
            ;;
        -S)
            _command
            compopt +o nospace
            return
            ;;
        -4|-6)
            ipvx=$prev
            ;;
    esac

    _expand || return

    case $cur in
        !(*:*)/*|[.~]*) ;; # looks like a path
        *:*) _scp_remote_files ; return ;;
    esac

    local prefix

    if [[ "$cur" == -F* ]]; then
        cur=${cur#-F}
        prefix=-F
    else
        case $cur in
            -*)
                COMPREPLY=( $( compgen -W '$( _parse_usage "${words[0]}" )' \
                    -- "$cur" ) )
                COMPREPLY=( "${COMPREPLY[@]/%/ }" )
                return
                ;;
            */*|[.~]*)
                # not a known host, pass through
                ;;
            *)
                _known_hosts_real $ipvx -c -a -F "$configfile" -- "$cur"
                ;;
        esac
    fi

    _scp_local_files "$prefix"
} &&
complete -F _scp -o nospace scp

# ex: filetype=sh

以上是关于sh Bash选项卡完成代码段的主要内容,如果未能解决你的问题,请参考以下文章

bash 选项卡完成如何工作?

markdown Bash-only Laravel Artisan选项卡自动完成

设置 git bash 以使用 Windows 上的选项卡

sbt控制台 - 选项卡完成

jquery实现的选项卡的嵌套代码实例

Git选项卡完成在mac上的zsh中不起作用