How to generate output with colors from shell script bash to file and maintain the colors?

2

I'm developing a utility in a bash shell script (file.sh), I can generate colors when the output goes to console, for example with a code like:

echo -e "\e[1;33m Aqui el mensaje \e[0m";

But my idea is to create a log file as identical as possible to the output that I send by console, for which I would like to have the text in colors also in the log file. I am using this statement to issue both the console and the log file:

echo -e "\e[1;33m Aqui el mensaje \e[0m" 2>&1 | tee --append $nombreArchivo;

The command ' tee ' is what makes it possible for the input stream to go to the console and to the log file.

The problem with this is that in the log file the characters ' \ e [1; 33m ' for example, are not interpreted, so these characters are written as they are in the file and it looks horrible; but I suppose it should be possible because the command ' ls -l 2 > & 1 | tee --append $ filename 'if you type in the file the colors of the command' ls 'as it is sent by console.

I guess you have to use a different nomenclature to use the colors, but I do not know it.

Here the output of ' ls -l ' with color, for example:

    
asked by Ricardo Gabriel 29.10.2018 в 18:27
source

1 answer

2

There is a small misconception which, I believe, is the cause of the confusion.

The colors of the ls -l output that you see in Visual Studio Code are not the product of a special command-specific nomenclature that can be replicated from a shell script.

You can realize this because if you open the file in a simpler editor the color is not shown, in addition to the discrepancies that are in the coloring (in the date column, "2018" is in blue and the hours in green, file names are not completely colored, only the nombre.ext part for those with an extension) and because ls only colors the file names according to their type (at least in the GNU implementation) ).

What actually happens is that VS Code is adding syntax highlighting on its own. < I do not know what language the text is interpreting (maybe it's some kind of generic highlight), but it's definitely not something you can use at your convenience arbitrarily without first modifying the editor itself.

In a previous comment I mentioned that my recommendation was to add a custom syntax highlight to your editor, but thinking about it is not very good advice as it does not apply to arbitrary cases and for every change you make to the script it is possible that you need to modify the highlight.

The possible solutions depend on the purpose of the log file: Will it be processed by other tools or is it intended to be solely for human consumption?

If the file is or will be processed by other tools ( awk , grep , sed , etc.): the ideal is to keep it as simple as possible (plain text) to facilitate processing. In this case, the color would be an obstacle.

If the file is only intended for people: adapt the output of the log file to markup language of your preference and make sure you use an appropriate interpreter to view the file. In the case of ANSI escape codes (the ones you originally used), the terminal is the right interpreter (not a plain text editor or IDE).

In the case of HTML, there are terminals that allow you to save the information, color included, in this language ( xfce4-terminal , for example). Or you could manage everything from the script itself:

#!/usr/bin/env bash

log='log.html'

printf '<!doctype html>\n' >> "${log}"
printf '<html>\n' >> "${log}"
printf '<head>\n' >> "${log}"
printf '<meta charset="utf-8"/>\n' >> "${log}"
printf '<title>¡Hola, mundo!</title>\n' >> "${log}"
printf '</head>\n' >> "${log}"
printf '<body style="background-color: #131926;">\n' >> "${log}"
printf '<pre>' >> "${log}"

# Este texto va hacia la terminal
printf '\e[1;32m¡Hola, mundo!\e[0m\n'

# Este texto va hacia el archivo log
printf '<strong style="color: #4CE64C;">¡Hola, mundo!</strong>\n' >> "${log}"

printf '</pre>\n' >> "${log}"
printf '</body>\n' >> "${log}"
printf '</html>\n' >> "${log}"

    
answered by 05.11.2018 / 19:34
source