有点上下文,这个问题是这个问题的后续:Bash remote autocompletion : change the 'starting' directory
无论如何,我正在编写我的自定义自动完成 bash 脚本:我希望自动完成能够像cd
那样工作,除了我想从特定目录中获取名称,不一定是当前目录。它工作得很好,除非文件名中有空格。
一个简单的例子。假设我从中获取名称的目录中有两个文件:a_file
和another file
(注意空格)。有时候是这样的:
my_command
TABTAB
a_file
file
another
演示文稿并不完美,但我的想法是提示我有 3 个选择,another file
分为another
和file
。所需的输出将是:
file_1
another file
。我还希望自动转义空格:
my_command ano
TAB
my_command another\ file
这是我的脚本的样子:
#!/bin/bash _get_file_list() { dir="/some/path/" cd $目录 查找 * -maxdepth 0 } _GetOptMyCommand() { 本地货币 完全=() cur=${COMP_WORDS[COMP_CWORD]} 案例“$cur”在 -*) COMPREPLY=( $( compgen -W "-h -l --help --list --" -- "$cur" ) );; *) COMPREPLY=( $( compgen -W "$(_get_file_list)" -- "$cur" ) );; esac 返回 0 } 完成 -F _GetOptMyCommand my_command
如何处理文件名中的空格并使我的自动完成脚本像cd
?
尝试find
通过管道输出sed 's/ /\\ /'
。
但请注意,其他字符(引号、$、&..所有常见的字符)也可能会让您陷入困境。
sed 's/\([ $&!#*()<>|{}[?`"'"'"']\)/\\\1/'
Run Code Online (Sandbox Code Playgroud)
可能会更好,因为它会转义大多数“特殊”字符。
(不过,我还没弄清楚如何正确转义 \ )
归档时间: |
|
查看次数: |
3093 次 |
最近记录: |