gpt4 book ai didi

linux - 调用脚本文件时如何提示建议

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:59:41 25 4
gpt4 key购买 nike

我必须为 linux-box 编写一个脚本,它从命令行获取输入 sudo build_all.sh -vmware yes|no nic bridged|host_only -ipaddress xx.xx.xx.xx|dhcp -arch=ARM|x86 ,我想知道输入输入时是否有办法获得建议。对于前。

如果我输入 sudo build_all.sh然后按 Tab我得到了类似 -vmware 的建议输入-vmware后我又树了Tab我提示了 Yes No .

有什么办法吗?

最佳答案

这称为 bash 补全。您可以在/etc/bash_completion.d/中找到许多示例。 bash_completion 函数需要加载到您的 shell 才能工作。这是一个让你开始的例子

# Bash completion must to be loaded when shell starts
# Recommended location is /etc/bash_completion.d/build_all.sh
# or added to ~/.bashrc
# Then load with . /etc/bash_completion.d/build_all.sh
# or with . ~/.bashrc
# New instances of bash will have already sourced it.
# The file name build_all.sh may be too common, and result in unwanted tab
# completion for build_all.sh from other projects.

_build_all.sh(){
local cur
COMPREPLY=()
_get_comp_words_by_ref cur

case $COMP_CWORD in
1) COMPREPLY=( $( compgen -W '-vmware' -- "$cur" ) ) ;;
2) COMPREPLY=( $( compgen -W 'no yes' -- "${COMP_WORDS[COMP_CWORD]}" ) ) ;;

3) COMPREPLY=( $( compgen -W '-nic' -- "$cur" ) ) ;;
4) COMPREPLY=( $( compgen -W 'host_only bridged' -- "${COMP_WORDS[COMP_CWORD]}" ) ) ;;

5) COMPREPLY=( $( compgen -W '-ipaddress' -- "$cur" ) ) ;;
6) COMPREPLY=( $( compgen -W 'dhcp xx.xx.xx.xx' -- "${COMP_WORDS[COMP_CWORD]}" ) ) ;;

7) COMPREPLY=( $( compgen -W '-arch' -- "$cur" ) ) ;;
8) COMPREPLY=( $( compgen -W 'x86 ARM' -- "${COMP_WORDS[COMP_CWORD]}" ) ) ;;
esac
return 0

} &&
complete -F _build_all.sh build_all.sh

关于linux - 调用脚本文件时如何提示建议,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21601769/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com