【问题标题】:Bash Read Input - Tab the PromptBash 读取输入 - 选项卡提示
【发布时间】:2016-07-01 16:26:55
【问题描述】:

我有一个脚本,我正在读取用户的输入。这是我的代码:

if [ -z $volreadexists ]; then
        echo -e "\tThis will overwrite the entire volume (/dev/vg01/$myhost)...are you sure?"
        read REPLY
        echo
        if [[ $REPLY =~ ^[Yy]$ ]]; then
            echo -e "\t\tContinuing"
            syncvolume
        else
            echo "Fine...skipping"
        fi
    fi

我不得不使用read REPLY,因为read 本身不插入标签。我正在寻找的是类似于:

read -p "\tDoes this look OK? (n for No)" -n 1 -r

\t 将在读取提示上切换。

如何在阅读提示中添加标签?

更新:感谢@gniourf 的精彩回答!:

read -p $'\tDoes this look OK? (n for No)' -n 1 -r

但是,我发现了一个问题。当我尝试在那里使用变量时,它不会翻译它:

read -p $'\tThis will overwrite the entire volume (/dev/vg01/$myhost)...are you sure? ' -n 1 -r

变成

        This will overwrite the entire volume (/dev/vg01/$myhost)...are you sure?

我想要的地方:

        This will overwrite the entire volume (/dev/vg01/server1)...are you sure?

使用双引号也不起作用:(

有什么想法吗?

【问题讨论】:

  • 就用ANSI-C quoting:read -p $'\tDoes this look OK? (n for No)' -n 1 -r
  • 看起来不错!!亲爱的,谢谢!
  • @gniourf_gniourf 您真的应该将其添加为答案,以便它可以被投票和(更重要的是)接受。
  • 是的,添加一个答案 - 得到业力:)
  • 那是因为$"..." 完全做了别的事情。但您并不局限于一组引号。

标签: bash


【解决方案1】:

只需使用ANSI-C quoting:

read -p $'\tDoes this look OK? (n for No)' -n 1 -r

现在,如果你也想使用变量扩展,你可以像这样混合不同的引号:

read -p $'\t'"This will overwrite the entire volume (/dev/vg01/$myhost)...are you sure? " -n 1 -r

这里我只对制表符使用了 ANSI-C 引用。确保在 $'\t'"This will...." 之间不要留任何空格。

【讨论】:

    【解决方案2】:

    我最终参考了这个答案:

    Read a variable in bash with a default value

    并创建了一个解决方法。它并不完美,但它确实有效:

    myhost="server1"
    if [ -z $volreadexists ]; then
        read -e -i "$myhost" -p $'\tJust checking if it\'s OK to overwrite volume at /dev/vg01/'
        echo
        if [[ $REPLY =~ ^$myhost[Yy]$ ]]; then
            echo -e "\t\tContinuing"
        else
            echo "Fine...skipping"
        fi
    fi
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-23
      • 2013-01-01
      • 1970-01-01
      • 2017-02-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-10
      相关资源
      最近更新 更多