【问题标题】:Bash script select option based on value基于值的 Bash 脚本选择选项
【发布时间】:2019-05-17 15:09:19
【问题描述】:

我想在终端中运行一个命令。

例如。

示例命令 -s

稍后提供一个选项:

  1. 选项 1
  2. 选项 2

输入问题 请输入索引:

所以我需要在终端中输入 2。

但是,选项 1 和选项 2 的顺序发生了变化,因此很难对特定索引进行硬编码。

例如可能是:

  1. 选项 2
  2. 选项 1

有没有办法使用提供的选项的上下文并使脚本只选择“选项 2”。

【问题讨论】:

标签: bash ssh terminal


【解决方案1】:

你可以使用se;ect:

> select: select NAME [in WORDS ... ;] do COMMANDS; done
>     The WORDS are expanded, generating a list of words.  The
>     set of expanded words is printed on the standard error, each
>     preceded by a number.  If `in WORDS' is not present, `in "$@"'
>     is assumed.  The PS3 prompt is then displayed and a line read
>     from the standard input.  If the line consists of the number
>     corresponding to one of the displayed words, then NAME is set
>     to that word.  If the line is empty, WORDS and the prompt are
>     redisplayed.  If EOF is read, the command completes.  Any other
>     value read causes NAME to be set to null.  The line read is saved
>     in the variable REPLY.  COMMANDS are executed after each selection
>     until a break command is executed.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-30
    • 1970-01-01
    • 2011-11-06
    • 2018-12-04
    相关资源
    最近更新 更多