用于加粗/下划线/斜体特定文本的 Unix Bash 脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4414297/
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
Unix Bash script to embolden/underline/italicize specific text
提问by
I've been trawling the web trying to find examples of Unix Bash script that can handle basic text styling (bold/underline/italics), but can't find anything? Is such a thing possible to do?
我一直在网上搜索,试图找到可以处理基本文本样式(粗体/下划线/斜体)的 Unix Bash 脚本示例,但找不到任何东西?这样的事情可以做吗?
For example:
例如:
- Embolden/Underline/Italicize all lines ending in ":"?
- (Turn off) Embolden/Underline/Italicize all lines ending in ":"?
- 将所有以“:”结尾的行加粗/下划线/斜体?
- (关闭)将所有以“:”结尾的行加粗/下划线/斜体?
I want to set it up as a Service via Automator; so using /bin/bashand actioning on "selected text" (in a rich-text-compatible file, of course).
我想通过 Automator 将其设置为服务;所以使用/bin/bash和操作“选定的文本”(当然是在富文本兼容的文件中)。
回答by Chris Cashwell
Basically, you want to do declare some variables with the styling code--something like this:
基本上,你想用样式代码声明一些变量——像这样:
underline=`tput smul`
nounderline=`tput rmul`
bold=`tput bold`
normal=`tput sgr0`
then you can call these for use in your output using the variables, like this:
然后您可以使用变量调用这些以在您的输出中使用,如下所示:
echo "${bold}bold${normal} text stands out!"
echo "${underline}underlined${nounderline} text does, too."
As far as automating it to apply to all lines beginning with a specific character, you're better off just using the variables as shown above. Besides using this method just being easier, it's also cleaner and more usable. For example, when using this method you have the ability to style any number of words in a given output string differently, so as to emphasize a specific word, not the entire sentence (unless of course that's your goal).
至于自动将其应用于以特定字符开头的所有行,最好只使用如上所示的变量。除了使用这种方法更容易之外,它也更干净、更实用。例如,使用此方法时,您可以为给定输出字符串中的任意数量的单词设置不同的样式,以强调特定单词,而不是整个句子(当然,除非这是您的目标)。
For more information, you should check out http://tldp.org/HOWTO/Bash-Prompt-HOWTO/x405.htmland/or man tput
有关更多信息,您应该查看http://tldp.org/HOWTO/Bash-Prompt-HOWTO/x405.html和/或man tput

