简体   繁体   English

bash中的空格和后缀complete()

[英]Spaces and suffixes in bash complete()

I have two difficulties with compgen shell builtin. 我对compgen shell内置有两个困难。 I try show it in simple bash _filedir -like (using ls ) code: 我尝试以简单的bash _filedir (使用ls )代码显示它:

_myfiledir(){
  path="$cur"
  prefix=`echo /$path | grep -o "[0-9a-Z\-+_/]*/"`
  sufix=`echo /$path | grep "/[0-9a-Z\-+_]*$" | grep -o "[0-9a-Z\-+_]*$"`
  res=`ls -p $prefix`
  COMPREPLY=($( compgen -o nospace -W "$res" -- $sufix ))
}

In this case when cur=="usr/li" then prefix=="/usr/" and sufix=="li" 在这种情况下,当cur=="usr/li"prefix=="/usr/"sufix=="li"

I have two difficulties. 我有两个困难。 With space and replacement. 随着空间和更换。 For example: 例如:

$ script usr/li[TAB]

I get: 我得到:

$ script lib/  <- additional space here

I need: 我需要:

$ script usr/lib/ <- no space here

This code is only for example. 此代码仅作为示例。

-o nospace -o空格

This option don't work in compgen context. 此选项在compgen上下文中compgen Use compopt to change options. 使用compopt更改选项。 In your case compopt -o nospace usefull for whitespace issue, and compopt -o filenames for filenames. 在您的情况下, compopt -o nospace对于空格问题有用,而compopt -o filenames对于文件名。 Be careful with file names, you need add $prefix to $res variable. 注意文件名,您需要在$res变量中添加$prefix Option filenames only cut part of full path when displayed to user for autocompletition. 当显示给用户进行自动填充时,选项filenames仅剪切完整路径的一部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM