Linux Bash中的自定义路径完成
我想为我自己的文件系统编写一个bash_完成脚本。我有一个客户端程序,它向一些数据库发送查询 例如:Linux Bash中的自定义路径完成,linux,bash,tab-completion,Linux,Bash,Tab Completion,我想为我自己的文件系统编写一个bash_完成脚本。我有一个客户端程序,它向一些数据库发送查询 例如: my_prog --ls db_name:/foo/bar/ 此命令写入db\u name:/foo/bar文件夹中文件的标准输出列表 我想为此启用自动完成。所以当我按tab键时,它会显示选项列表 my_prog --ls db_name:/foo/bar/<tab> 我在尝试类似的东西时发现了这条线。 stackexchange post帮助我将下面的自动完成功能组合在一起。它
my_prog --ls db_name:/foo/bar/
此命令写入db\u name:/foo/bar
文件夹中文件的标准输出列表
我想为此启用自动完成。所以当我按tab键时,它会显示选项列表
my_prog --ls db_name:/foo/bar/<tab>
我在尝试类似的东西时发现了这条线。 stackexchange post帮助我将下面的自动完成功能组合在一起。它不像完整路径显示的那样是“正常的”自动完成,但在其他方面,您可能会发现它很有用
_complete_func()
local cur prev opts
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
if [ $COMP_CWORD -eq 1 ]; then
opts="some options for the program"
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
elif [ $COMP_CWORD -ge 2 ]; then
local files=("${cur}"*)
COMPREPLY=( "${files[@]}")
fi
}
complete -o nospace -F complete_func command_to_autocomplete
什么是
${db}
??
__complete_path()
{
COMPREPLY=()
if [[ ${1} == "" ]]
then
COMPREPLY=( "/" )
compopt -o nospace
return
fi
base=${1##*/}
dir=${1%/*}
options="my_prog --ls ${db}:${dir}"
COMPREPLY=( $(compgen -W "${options}" -- ${base} ) )
compopt -o nospace
}
_complete_func()
local cur prev opts
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
if [ $COMP_CWORD -eq 1 ]; then
opts="some options for the program"
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
elif [ $COMP_CWORD -ge 2 ]; then
local files=("${cur}"*)
COMPREPLY=( "${files[@]}")
fi
}
complete -o nospace -F complete_func command_to_autocomplete