【问题标题】:BASH: auto-completing $keyword for printf "%s" [duplicate]BASH:为 printf“%s”自动完成 $keyword [重复]
【发布时间】:2017-10-29 10:36:55
【问题描述】:

我必须为平凡的 printf "%s" 找到一种不平凡的方法。 在我的情况下,所有关键字都是不同位置的物理文件。 我希望能够像使用 CLI 一样使用选项卡按钮来完成位置/文件,并显示所有以相同字母开头的文件。 是否可以将系统的索引添加到 bash 脚本中? 例如:

#!/bin/bash

printf "%s" "The file is: "
read keyword

~~~~~~~~~

./script.sh
The file is: /home/user/file
(Hit tab)
file.txt file2 file5
/home/user/file.
(Hit tab)
/home/user/file.txt

文件可以位于不同的位置。一次只能提供 1 个参数。

【问题讨论】:

    标签: linux bash shell


    【解决方案1】:

    printf 在这里不相关;这是您想要完成的read。在bash 中,使用-e 选项启用Readline 支持。

    read -e keyword
    

    顺便说一句,您可以完全取消printf 命令,使用-p 选项打印提示:

    read -p "The file is: " -e keyword
    

    【讨论】:

      猜你喜欢
      • 2014-05-23
      • 1970-01-01
      • 2012-09-20
      • 1970-01-01
      • 2016-04-27
      • 1970-01-01
      • 1970-01-01
      • 2015-05-23
      • 2021-09-11
      相关资源
      最近更新 更多