bash 如何编写带有颜色的文本文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22380733/
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
How to write a text file with color
提问by yhabib
I'm trying to write a text file with two colors depending on the output. I using the echo -e
command as I would when printing with colors in the console, like this:
我正在尝试根据输出编写具有两种颜色的文本文件。我echo -e
像在控制台中打印颜色一样使用命令,如下所示:
RETURNED=$?
if [ $RETURNED == 0 ]
then
echo -e "\e[1;32mffmpeg -t $DURACION -f x11grab -s $RESOLUCION -r ${FPS[j]} -b:v $BR -i :0.0 -y $NOMBRE\e[0m" >> file.txt
fi
The idea would be: If the command worked, then write a green line, else use red. However I'm not getting any colored line in the text file.
想法是:如果命令有效,则写一条绿线,否则使用红色。但是,我没有在文本文件中看到任何彩色线条。
采纳答案by l0b0
Have you tried less -R file.txt
? That should show you the colour (works for me at least).
你试过less -R file.txt
吗?那应该显示颜色(至少对我有用)。
If you want colour coding which is supported by non-shell applications your best bet is probably to output HTML, for example:
如果您想要非 shell 应用程序支持的颜色编码,您最好的选择可能是输出 HTML,例如:
printf '<code style="color: %s;">%s</code>' "green" "ffmpeg -t $DURACION -f x11grab -s $RESOLUCION -r ${FPS[j]} -b:v $BR -i :0.0 -y $NOMBRE" >> file.html
回答by mklement0
Try printf
instead of echo -e
- the latter doesn't seem to work on all platforms (at least not on OSX) with ANSI escape sequences.
尝试printf
而不是echo -e
- 后者似乎不适用于具有 ANSI 转义序列的所有平台(至少不适用于 OSX)。
printf "\e[1;32mffmpeg -t $DURACION -f x11grab -s $RESOLUCION -r ${FPS[j]}\
-b:v $BR -i :0.0 -y $NOMBRE\n\e[0m" >> file.txt
Note the the \n
to print a newline before the closing escape sequence.
请注意在\n
结束转义序列之前打印换行符。