调用“读取”时获取自动完成功能在 Bash 脚本中

发布于 2024-10-14 11:48:59 字数 209 浏览 2 评论 0原文

在我的 Bash 脚本中,我正在使用 read 读取用户输入的一些变量:

read -p "Glassfish Path:" GF_DIR

现在我希望用户在必须输入目录时获得自动完成功能,就像您在 Bash shell 中一样。因此,当他输入目录的第一个字母时,他可以通过按 TAB 键自动完成它。 这可能吗?

Inside my Bash script, I'm reading some variables entered by the user with read:

read -p "Glassfish Path:" GF_DIR

Now I want that the user gets a autocompletion when he has to enter a directory, like when you are on the Bash shell. So when he enters the first letters of a directory, he can autocomplete it by hitting TAB.
Is that possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

内心荒芜 2024-10-21 11:48:59

尝试:

read -e -p "Glassfish Path:" GF_DIR

-e 启用 readline

 -e 
    If the standard input is coming from a terminal, Readline is used
    to obtain the line.

Try:

read -e -p "Glassfish Path:" GF_DIR

-e enables readline:

 -e 
    If the standard input is coming from a terminal, Readline is used
    to obtain the line.
━╋う一瞬間旳綻放 2024-10-21 11:48:59

bash 脚本中的本机 readline 只能选择使用本机完成。

如果想要添加自定义完成,一种方法是将modebind结合使用。

这是一种“低级”方法,必须做更多的工作,例如通过拆分单词、查找当前单词等来解析输入。

人们仍然可以使用诸如compgen之类的东西> 和打印选项,修改输入行等。

简而言之(相当幼稚的示例):

# Custom complete function
list=(foo bar baz)
comp() {
    # Here we have two variables that can be used to manipulate the read-line
    # 1) $READLINE_LINE:  The current line
    # 2) $READLINE_POINT: Position of cursor
    #
    # If either is changed in this function, it is reflected on the read-line
    #

    printf '%s ' "${list[@]}" >&2
    printf '\n' >&2
    printf 'I am inserting "foo "\n' >&2

    READLINE_LINE+="foo "
    (( READLINE_POINT += 4 ))
}
# Make it available to bind
export -f comp
# Enable readline mode
set -o emacs
# Bind TAB to our custom completion function
bind -x '"\t"':comp

while :; do
    # TAB pressed on read will now call "comp"
    read -rep"> " inp
    # process command
done

Native readline in bash-scripts only have the option to use the native completion.

If one want to add custom completion, one way is to use mode with bind.

It is a "low level" approach and one have to do more work like parsing input by for example splitting into words, find current word etc.

One can still use things like compgen and print options, modify the input line etc.

In short (rather naive example):

# Custom complete function
list=(foo bar baz)
comp() {
    # Here we have two variables that can be used to manipulate the read-line
    # 1) $READLINE_LINE:  The current line
    # 2) $READLINE_POINT: Position of cursor
    #
    # If either is changed in this function, it is reflected on the read-line
    #

    printf '%s ' "${list[@]}" >&2
    printf '\n' >&2
    printf 'I am inserting "foo "\n' >&2

    READLINE_LINE+="foo "
    (( READLINE_POINT += 4 ))
}
# Make it available to bind
export -f comp
# Enable readline mode
set -o emacs
# Bind TAB to our custom completion function
bind -x '"\t"':comp

while :; do
    # TAB pressed on read will now call "comp"
    read -rep"> " inp
    # process command
done
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文