分页: 1 / 1

请教:如何修改select菜单提示信息(PS3)颜色(已解决)

发表于 : 2012-01-31 21:10
halfwolf
脚本如下:
cat test.sh
----------------------------------
#!/bin/bash

PS3="What does the book mean?"
select mean in 书 花 美女 帅哥
do
if [ $mean == "书" ]
then
echo "you are genius!"
exit
else
echo "How foolish you are!"
fi
done

运行结果
----------------------------------
1) 书
2) 花
3) 美女
4) 帅哥
What does the book mean?1
you are genius!

而我期望的提示信息为
----------------------------------
What does the book mean?
即用红色字体强调book一词,与下述echo效果相同
echo -e "Whats the\033[49;31m book \033[0mmean?"

echo尚好,select就不知道该怎么办了,望大牛指导,谢谢!
:em03

Re: 请教:如何修改select菜单提示信息(PS3)颜色

发表于 : 2012-02-01 7:43
aerofox
跟 echo 中的方法一样,只不过用 \e 代替 \033。

Re: 请教:如何修改select菜单提示信息(PS3)颜色

发表于 : 2012-02-01 13:27
halfwolf
楼上的兄弟,你好!能不能说详细一点,谢谢!

按照你的说法,我把PS3修改为
PS3="What does the\e[49;31m book \e[0mmean?"

执行后返回的提示信息为
What does the\e[49;31m book \e[0mmean?
颜色没加上,而正则的东西被输了出来,望进一步指导,谢谢!

Re: 请教:如何修改select菜单提示信息(PS3)颜色

发表于 : 2012-02-01 18:28
aerofox
刚才试了一下,确实在PS3中不认PS1中的特殊转义串,那么可以用 Ctrl-V Ctrl-[ 输入 ESC 字符。

代码: 全选

PS3="What does the^[[49;31m book ^[[0mmean?"
其中 ^[ 是一个字符,用 Ctrl-V Ctrl-[ 输入。

Re: 请教:如何修改select菜单提示信息(PS3)颜色

发表于 : 2012-02-01 18:39
tusooa
aerofox 写了:刚才试了一下,确实在PS3中不认PS1中的特殊转义串,那么可以用 Ctrl-V Ctrl-[ 输入 ESC 字符。

代码: 全选

PS3="What does the^[[49;31m book ^[[0mmean?"
其中 ^[ 是一个字符,用 Ctrl-V Ctrl-[ 输入。
简单点,可以PS3=$'内容'
这样的。

Re: 请教:如何修改select菜单提示信息(PS3)颜色

发表于 : 2012-02-01 21:45
halfwolf
谢谢aerofox兄弟苦口婆心的教导,
试过了,尽管不明白具体的机制,但结果完全可行!

tusooa兄讲的应该是
PS3=$(echo -e "Whats the\033[49;31m book \033[0mmean?")
试过了,完全可行!谢谢!

Re: 请教:如何修改select菜单提示信息(PS3)颜色

发表于 : 2012-02-02 8:19
aerofox
halfwolf 写了:谢谢aerofox兄弟苦口婆心的教导,
试过了,尽管不明白具体的机制,但结果完全可行!

tusooa兄讲的应该是
PS3=$(echo -e "Whats the\033[49;31m book \033[0mmean?")
试过了,完全可行!谢谢!
tusooa说的应该是:

代码: 全选

PS3=$'Whats the\033[49;31m book \033[0mmean?'

Re: 请教:如何修改select菜单提示信息(PS3)颜色(已解决)

发表于 : 2012-02-02 8:54
tusooa
另外,不推荐用\033。吾一直用\e,没出过问题。

Re: 请教:如何修改select菜单提示信息(PS3)颜色(已解决)

发表于 : 2012-02-02 14:20
halfwolf
谢谢两位,我又重新尝试了tusooa在5楼提供的方案,完全可行,在上次试验时‘’误用为“”,故导致结果不合预期,谢谢!

可行的方法汇总如下

1、直接派
--------------------
PS3="Whats the^[[49;31m book ^[[0mmean?"
PS3='Whats the^[[49;31m book ^[[0mmean?'

2、$派
--------------------
PS3=$'Whats the\e[49;31m book \e[0mmean?'
PS3=$'Whats the\033[49;31m book \033[0mmean?'
PS3=$'Whats the^[[49;31m book ^[[0mmean?'
PS3=$"Whats the^[[49;31m book ^[[0mmean?"

3、$(echo -e )派
--------------------
PS3=$(echo -e 'Whats the\033[49;31m book \033[0mmean?')
PS3=$(echo -e 'Whats the\e[49;31m book \e[0mmean?')
PS3=$(echo -e 'Whats the^[[49;31m book ^[[0mmean?')
PS3=$(echo -e "Whats the\033[49;31m book \033[0mmean?")
PS3=$(echo -e "Whats the\e[49;31m book \e[0mmean?")
PS3=$(echo -e "Whats the^[[49;31m book ^[[0mmean?")


以下是我的学习体验,请指导,谢谢!
--------------------
\033、\e、^[ 比较:^[通用性最强,\033与\e相同;
“”、‘’比较:‘’通用性比“”强

方法3:通过$()进行echo操作,将带颜色的信息赋予PS3;
方法1:标准的正则表达;
方法2:很酷,但没搞明白$''是什么意思,一种操作,还是其他?

Re: 请教:如何修改select菜单提示信息(PS3)颜色(已解决)

发表于 : 2012-02-06 17:58
tusooa
halfwolf 写了:谢谢两位,我又重新尝试了tusooa在5楼提供的方案,完全可行,在上次试验时‘’误用为“”,故导致结果不合预期,谢谢!

可行的方法汇总如下

1、直接派
--------------------
PS3="Whats the^[[49;31m book ^[[0mmean?"
PS3='Whats the^[[49;31m book ^[[0mmean?'

2、$派
--------------------
PS3=$'Whats the\e[49;31m book \e[0mmean?'
PS3=$'Whats the\033[49;31m book \033[0mmean?'
PS3=$'Whats the^[[49;31m book ^[[0mmean?'
PS3=$"Whats the^[[49;31m book ^[[0mmean?"

3、$(echo -e )派
--------------------
PS3=$(echo -e 'Whats the\033[49;31m book \033[0mmean?')
PS3=$(echo -e 'Whats the\e[49;31m book \e[0mmean?')
PS3=$(echo -e 'Whats the^[[49;31m book ^[[0mmean?')
PS3=$(echo -e "Whats the\033[49;31m book \033[0mmean?")
PS3=$(echo -e "Whats the\e[49;31m book \e[0mmean?")
PS3=$(echo -e "Whats the^[[49;31m book ^[[0mmean?")


以下是我的学习体验,请指导,谢谢!
--------------------
\033、\e、^[ 比较:^[通用性最强,\033与\e相同;
“”、‘’比较:‘’通用性比“”强

方法3:通过$()进行echo操作,将带颜色的信息赋予PS3;
方法1:标准的正则表达;
方法2:很酷,但没搞明白$''是什么意思,一种操作,还是其他?
[bash] Words of the form $'string' are treated specially. The word expands to string, with backslash-escaped characters replaced as
specified by the ANSI C standard. Backslash escape sequences, if present, are decoded as follows:
\a alert (bell)
\b backspace
\e
\E an escape character
\f form feed
\n new line
\r carriage return
\t horizontal tab
\v vertical tab
\\ backslash
\' single quote
\" double quote
\nnn the eight-bit character whose value is the octal value nnn (one to three digits)
\xHH the eight-bit character whose value is the hexadecimal value HH (one or two hex digits)
\cx a control-x character

The expanded result is single-quoted, as if the dollar sign had not been present.

A double-quoted string preceded by a dollar sign ($"string") will cause the string to be translated according to the current
locale. If the current locale is C or POSIX, the dollar sign is ignored. If the string is translated and replaced, the
replacement is double-quoted.
[/bash]
就是扩展了转义字符啊。