bash 如何在shell脚本中使文本闪烁

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17439482/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 23:55:24  来源:igfitidea点击:

How to make a text blink in shell script

bashblink

提问by iconoclast

I have this piece of code below:

我在下面有这段代码:

echo " \033[33mTitle of the Program\033[0m"

echo " \033[33mTitle of the Program\033[0m"

which changes the colour to yellow.

这将颜色更改为黄色。

How can I make the text "Title of the Program" blink?

如何使文本“程序标题”闪烁?

回答by iconoclast

Try adding -eif it's not working without it

-e如果没有它就无法工作,请尝试添加

You may need to add the -eoption to echo(at least that's required on all or most systems I use). The following will tell your system to blink the text:

您可能需要将-e选项添加到echo(至少在我使用的所有或大多数系统上都需要)。以下将告诉您的系统闪烁文本:

echo  -e "3[5mTitle of the Program3[0m"

You can have blinking andcolor

你可以有闪烁颜色

And you don't have to choose eitheryellow orblinking. You can have your cake and eat it too:

你不必选择要么黄色闪烁。你可以吃蛋糕也可以吃:

echo  -e "3[33;5mTitle of the Program3[0m"

Some systems ignore blink codes

一些系统忽略闪烁代码

Your system may ignore the blink code—this appears to be quite common. If you want to make the text prominent, but blinking is being ignored, you can instead invert the colors with 7:

您的系统可能会忽略闪烁代码——这似乎很常见。如果您想让文本突出显示,但闪烁被忽略,您可以使用以下命令反转颜色7

echo  -e "3[33;7mTitle of the Program3[0m"

Or you can use blinking andinversion of colors (and yellow):

或者您可以使用颜色(和黄色)的闪烁反转:

echo  -e "3[33;5;7mTitle of the Program3[0m"

回答by Thomas Dickey

Whether you can make blinkwork or not depends upon the terminal emulator. The systemitself is irrelevant.

您是否可以使眨眼工作取决于终端仿真器。该系统本身是无关的。

The example given in the question was close, needing only a change to the escape sequence to "work" with any POSIX shell:

问题中给出的示例很接近,只需要更改转义序列即可与任何POSIX shell一起“工作” :

echo  "3[33;5mTitle of the Program3[0m"

The -esuggested is unneeded (it is a bashism, non-standard, and usually unneeded). Changing the 7(reverse) to 5(blink) does what was asked.

-e建议是不需要的(它是一个bashism,非标准的,通常不需要)。将7(反向)更改为5(闪烁)执行所要求的操作。

Rather than hardcoding the escape, you could use tput, e.g.,

而不是硬编码转义,你可以使用tput,例如,

printf '%s%s%s%s' "$(tput setaf 3)" "$(tput blink)" "Title of the Program" "$(tput sgr0)"

for the same effect, with two differences:

同样的效果,有两个区别:

  • the expression is arguably more readable,
  • actually uses the known terminal capabilities, but
  • assumes you are using a suitable terminal description, i.e., $TERM.
  • 该表达可以说更具可读性,
  • 实际上使用已知的终端功能,但是
  • 假设您使用的是合适的终端描述,即$TERM.

For example VTE (the library used for various skinssuch as gnome-terminal) does not support blink (and it's possible to find its developers' opinions on the topic in various bug reports). Using infocmpto display the corresponding terminal capabilities shows that, plus some other differences:

例如,VTE(用于gnome-terminal 等各种皮肤的库)不支持闪烁(并且可以在各种错误报告中找到其开发人员对该主题的意见)。使用infocmp来显示相应的终端功能表明,加上一些其他差异:

$ infocmp vte xterm
comparing vte to xterm.
    comparing booleans.
        km: F:T.
        mc5i: F:T.
        npc: F:T.
    comparing numbers.
    comparing strings.
        blink: NULL, '\E[5m'.
        cnorm: '\E[?25h', '\E[?12l\E[?25h'.
        cvvis: NULL, '\E[?12;25h'.
        enacs: '\E)0', NULL.
        is2: '\E[m\E[?7h\E[4l\E>\E7\E[r\E[?1;3;4;6l\E8', '\E[!p\E[?3;4l\E[4l\E>'.
        kb2: '\E[E', '\EOE'.
        kfnd: '\E[1~', NULL.
        kslt: '\E[4~', NULL.
        mc0: NULL, '\E[i'.
        mc4: NULL, '\E[4i'.
        mc5: NULL, '\E[5i'.
        rep: NULL, '%p1%c\E[%p2%{1}%-%db'.
        rmacs: '^O', '\E(B'.
        rmcup: '\E[2J\E[?47l\E8', '\E[?1049l'.
        rmm: NULL, '\E[?1034l'.
        rs2: '\E7\E[r\E8\E[m\E[?7h\E[!p\E[?1;3;4;6l\E[4l\E>\E[?1000l\E[?25h', '\E[!p\E[?3;4l\E[4l\E>'.
        setb: NULL, '\E[4%?%p1%{1}%=%t4%e%p1%{3}%=%t6%e%p1%{4}%=%t1%e%p1%{6}%=%t3%e%p1%d%;m'.
        setf: NULL, '\E[3%?%p1%{1}%=%t4%e%p1%{3}%=%t6%e%p1%{4}%=%t1%e%p1%{6}%=%t3%e%p1%d%;m'.
        sgr: '\E[0%?%p6%t;1%;%?%p2%t;4%;%?%p5%t;2%;%?%p7%t;8%;%?%p1%p3%|%t;7%;m%?%p9%t6%e7%;', '%?%p9%t\E(0%e\E(B%;\E[0%?%p6%t;1%;%?%p5%t;2%;%?%p2%t;4%;%?%p1%p3%|%t;7%;%?%p4%t;5%;%?%p7%t;8%;m'.
        sgr0: '\E[0m7', '\E(B\E[m'.
        smacs: '^N', '\E(0'.
        smcup: '\E7\E[?47h', '\E[?1049h'.
        smm: NULL, '\E[?1034h'.

If you happen to use KDE konsole, the differences are longer (although konsolehappens to support blink).

如果您碰巧使用 KDE konsole,则差异会更长(尽管konsole碰巧支持blink)。

回答by Nike

A small work around to make the line blink in bash script

使行在 bash 脚本中闪烁的小工作

for (( i=0;i<=3;i++))
do
#Below line will deleted the before printed line
echo -en "3[1A"
echo -en "EmpNo:$empno already exists\n";
sleep 0.4s;
#Below line to print a blank line
echo -en "3[1A"
echo -en "                                                            \n";                                                                         
sleep 0.2s;
done
echo -en "3[2A"
echo -en "                                                            \n";
echo -en "3[1A"
echo -en "Enter the empno       : "; read empno1;

回答by AMADANON Inc.

As far as I can tell, it's meant to be:

据我所知,它的意思是:

echo " 3[5mTitle of the Program3[0m"

The only change is before the title, the 33m is replaced by 5m. See Ansi Escape Codes

唯一的变化是在标题之前,33m 被 5m 取代。查看Ansi 转义码

Note that some systems filter out blink, because it's really annoying. Your mileage may vary.

请注意,某些系统会过滤掉眨眼,因为这真的很烦人。你的旅费可能会改变。