Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 在调用脚本文件时,如何提出建议_Linux_Bash_Shell_Scripting - Fatal编程技术网

Linux 在调用脚本文件时,如何提出建议

Linux 在调用脚本文件时,如何提出建议,linux,bash,shell,scripting,Linux,Bash,Shell,Scripting,我必须为linux编写一个脚本,它从命令行获取输入 sudo build_all.sh-vmware yes | no nic bridged | host | only-ipaddress xx.xx.xx | dhcp-arch=ARM | x86,我想知道在输入信息时是否有办法获得建议。 例如 如果我输入sudo build_all.sh并按Tab我会在输入-vmware后得到类似-vmware的建议,我会再次在树Tab中提示是否 有什么办法吗?这叫做bash完成。您可以在/etc/bas

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

如果我输入
sudo build_all.sh
并按
Tab
我会在输入
-vmware
后得到类似
-vmware
的建议,我会再次在树
Tab
中提示


有什么办法吗?

这叫做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

有一个名为
whiptail
的程序,可以在文本模式下进行“GUI”对话框


这里已经提出了这个问题,难道我们不能在我的bash脚本中添加自动完成功能吗?或者我必须始终对
/etc/bash\u completion.d/
进行更改。实际上,这个脚本将在不同的Linux中运行,谁下载这个包?有什么建议“我们不能在我的bash脚本中加入自动完成吗?”没有!您可以在/etc/bash_completion.d/中调整bash completions脚本的速度,或者从~/.bashrc加载它。如果您觉得这有帮助,请标记。