51工具盒子

依楼听风雨
笑看云卷云舒,淡观潮起潮落

通过bash-completion设置kubectl命令补全

默认bash包含了自动补全,只要我们输入几个命令的首字母,通过TAB键,就可以补全命令,而bash-completion相当于bash的扩展包,这个包提供了一些额外的补全功能,本文通过bash-completion来设置kubectl参数命令的补全。

本文基于CentOS7版本测试,理论适用于其他版本!

1、安装bash-completion
yum install bash-completion

|---|-----------------------------| | 1 | yum install bash-completion |

2、使其立即生效
source /usr/share/bash-completion/bash_completion source <(kubectl completion bash)

|-----|--------------------------------------------------------------------------------------| | 1 2 | source /usr/share/bash-completion/bash_completion source <(kubectl completion bash) |

3、添加环境变量
echo "source <(kubectl completion bash)" >> ~/.bashrc

|---|-----------------------------------------------------------| | 1 | echo "source <(kubectl completion bash)" >> ~/.bashrc |

4、通过命令验证设置是否正常
type _init_completion

|---|-----------------------| | 1 | type _init_completion |

如果正常,则看到以下内容
_init_completion is a function _init_completion () { local exclude= flag outx errx inx OPTIND=1; while getopts "n:e:o:i:s" flag "$@"; do case $flag in n) exclude+=$OPTARG ;; e) errx=$OPTARG ;; o) outx=$OPTARG ;; i) inx=$OPTARG ;; s) split=false; exclude+== ;; esac; done; COMPREPLY=(); local redir="@(?([0-9])<|?([0-9&])>?(>)|>&)"; _get_comp_words_by_ref -n "$exclude<>&" cur prev words cword; _variables && return 1; if [[ $cur == $redir* || $prev == $redir ]]; then local xspec; case $cur in 2'>'*) xspec=$errx ;; *'>'*) xspec=$outx ;; *'<'*) xspec=$inx ;; *) case $prev in 2'>'*) xspec=$errx ;; *'>'*) xspec=$outx ;; *'<'*) xspec=$inx ;; esac ;; esac; cur="${cur##$redir}"; _filedir $xspec; return 1; fi; local i skip; for ((i=1; i < ${#words[@]}; 1)) do if [[ ${words[i]} == $redir* ]]; then [[ ${words[i]} == $redir ]] && skip=2 || skip=1; words=("${words[@]:0:i}" "${words[@]:i+skip}"); [[ $i -le $cword ]] && cword=$(( cword - skip )); else i=$(( ++i )); fi; done; [[ $cword -eq 0 ]] && return 1; prev=${words[cword-1]}; [[ -n ${split-} ]] && _split_longopt && split=true; return 0 }

|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | _init_completion is a function _init_completion () { local exclude= flag outx errx inx OPTIND=1; while getopts "n:e:o:i:s" flag "$@"; do case $flag in n) exclude+=$OPTARG ;; e) errx=$OPTARG ;; o) outx=$OPTARG ;; i) inx=$OPTARG ;; s) split=false; exclude+== ;; esac; done; COMPREPLY=(); local redir="@(?([0-9])<|?([0-9&])>?(>)|>&)"; _get_comp_words_by_ref -n "$exclude<>&" cur prev words cword; _variables && return 1; if [[ $cur == $redir* || $prev == $redir ]]; then local xspec; case $cur in 2'>'*) xspec=$errx ;; *'>'*) xspec=$outx ;; *'<'*) xspec=$inx ;; *) case $prev in 2'>'*) xspec=$errx ;; *'>'*) xspec=$outx ;; *'<'*) xspec=$inx ;; esac ;; esac; cur="${cur##$redir}"; _filedir $xspec; return 1; fi; local i skip; for ((i=1; i < ${#words[@]}; 1)) do if [[ ${words[i]} == $redir* ]]; then [[ ${words[i]} == $redir ]] && skip=2 || skip=1; words=("${words[@]:0:i}" "${words[@]:i+skip}"); [[ $i -le $cword ]] && cword=$(( cword - skip )); else i=$(( ++i )); fi; done; [[ $cword -eq 0 ]] && return 1; prev=${words[cword-1]}; [[ -n ${split-} ]] && _split_longopt && split=true; return 0 } |

此时使用kubectl命令时,通过TAB键即可补全参数

赞(2)
未经允许不得转载:工具盒子 » 通过bash-completion设置kubectl命令补全