【问题标题】:Underlining User Input下划线用户输入
【发布时间】:2012-02-22 08:45:35
【问题描述】:

我打算在用户输入下划线,但经过研究,我仍然找不到方法。

到目前为止,这是我从研究中得到的,但它仍然不起作用。 任何人都知道如何解决它?或者更确切地说是正确的方法?

如果我使用 read -p "What is your name:" name 而不是下面的那个会怎样?正如我对输出的期望一样。

你叫什么名字? (来自用户的输入并带有下划线)

function underlineInput()
{
PS1='\033[4;47;40m'
}

function UNunderlineInput()
{
PS1='\033[0;47;40m'
}

function hi()
{
echo "Please enter your name: "
underlineInput
read input
underlineInput
}

感谢那些提前帮助的人! 干杯!

【问题讨论】:

    标签: linux bash shell underline


    【解决方案1】:

    查看此链接here

    但它只是:

    $ echo -e "\033[4mThis is a underlined line.\033[0m"
    

    关键部分是\033[4m 下划线和\033[0m 将其改回。

    【讨论】:

    • 嗨 Grammin,如果我使用 read -p "What is your name?:" name
    • @Andres 我会这样做: echo -n "What is your name";读取变量; echo "\033[4mHi $var\033[0m"
    【解决方案2】:

    在提示符末尾加上转义序列开启下划线,然后再发送返回正常序列:

    read -p $'Please enter your name: \033[4m' name
    printf '\033[0m' # Use printf instead of echo to avoid newline, and it translates escape without $''
    

    顺便说一句,如果您希望此脚本也适用于其他类型的终端,您可以使用 terminfo 数据库来获取相关的转义(/whatever)序列。下划线开启和关闭的 terminfo 能力名称是“smul”和“rmul”:

    read -p "Please enter your name: $(tput smul)" name
    tput rmul
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-10
      • 1970-01-01
      • 2021-06-15
      • 2014-08-08
      • 2018-06-24
      • 1970-01-01
      • 1970-01-01
      • 2018-11-10
      相关资源
      最近更新 更多