Linux: Sequences Escape-Control to colorize scripts [Quick Guide]

2:09 pm March 29, 201715202

I. introduction

for the digital age, we seek always to view items or to accentuate the Visual components to highlight any particular conclusion. But be aware that on the shell Linux you can also use some visual artifacts to highlight certain elements.

It is often the case in scripts where you want to distinguish the case of a positive or negative test. a well known example of GNU/Linux administrators: it’s the starting or stopping of the system operating. We very often find the steps highlighted by an indicator [ OK ] or [ ERROR ] , as shown in the shot below:

in fact, this is very easily achievable on any script, as long as you follow the next procedure. In fact, it is possible to colorize the text of our scripts, to put it in italics, bold highlighted, with a more intense display…

Use cases are generally related to the fact of wish:

to highlight a point capital . But we don’t want the user to have control over the indicators. You can then use vim to activate contrast of elements. Because, the tool supports the coloring, highlighted, bold, italic type effects… In this case, there is no guarantee that on another post vim will be him also configured the same way. Portability is not assured.

have the opportunity to make use of readable and portable solution. must be used “escape” ANSI codes. These guarantee ease of use but also the portability and flexibility.

This mode allows to control the color of the text and the background of display.

WARNING : today, most devices support this display mode. But it may be that some former VT require a setting, or even does not support.

The codification is quite complicated, because it implements a sequence of multiple keys. Thus, for the color of text are available the following controls:

– Black: ESC [30m
– Red: ESC [31m
– Green: ESC [32m
– yellow: ESC [33m
– Blue: ESC [34m
– Magenta: ESC [35m
– Cyan: ESC [36m
– white: ESC [37m
– default: ESC [39m

then you can also control the color of the background of the display]]]]]]]]] , via sequences below:

– Black: ESC [40m
– Red: ESC [41m
– Green: ESC [42m
– yellow: ESC [43m
– Blue: ESC [44m
– Magenta: ESC [45m
– Cyan: ESC [46m
– white: ESC [47m
– default: ESC [49m

you can also add effects to text as the highlighted, bold, italics, using the following sequences]]]]]]]]] :

– FAT: ESC [1m
– pointed out: ESC [4m
– flashing: ESC [5m
– Reset/default: ESC [0m

Finally, it is quite possible to mix these components to give several effects to a text or a wallpaper.]]]]

example: to list red text flashing on a white background, it will write the sequence: ESC [5;31;47m

Note: the ESC symbolism is usually codified, within a script, by “33”. []

II. Writing a test for existence of account

in order to illustrate this, Let’s now write a script to test for the existence of a user within the local operating system. What we want, it’s in case of absence, make the creation of said user.

In case of error, during a steps will be posted so either the message [ OK ] or [ ERROR ] message. Using the elements that we seen previously, so we will generate our script. It is a simplistic example, but we can clearly see how to set this function for more complex scripts:

 #! / bin/bash # Script  test  account toto # TSTACCNT = $(getent passwd toto|wc –l) TSTGRP = $(getent group toto|wc –l) if [ $ TSTACCNT –eq 1 ] & [ $TSTGRP –eq 1 ] then echo-"Account already existing. n" $(userdel toto; rm –fR /var/spool/mail/toto;rm –fR /home/toto) if [ $ ? –ne 0 ] then echo "Deleting the toto account existing t" echo - in "[33[01;31m ERROR 33[0m] " "n" else echo "Deleting the toto account existing t" echo - in "[33[01;32m ERROR 33[0m] n" fi fi echo-"Toto tt Account Creation" $(useradd toto 1>/dev/null) "[[ $ ? –eq 0 ]] & {echo-"E[01;32m”33[1m OK 33[0m]n"echo-"Initializing the toton account"passwd toto < < EOF 2> & 1 >/dev/null toto toto EOF echo-"n"} if [ $ ? –ne 0 ] then echo"Initializing account tt"echo - in" [33[01;31m ERROR 33[0m] n "else echo"Initializing account tt"echo - in" [33[01;32m OK 33[0m] n "fi exit 0 

Note: you can completely trivialize the name of the account to create and pass it as a parameter to the script. Simply replace the strings “toto” by $1. You can also customize this script differently. Here, it is one example.

III. Result of the script

IV. Use case

one can imagine to automate the creation of accounts on a system by positioning a default password fixed or generated. Thus, with this script it becomes possible to create users in battery.

Of course, we can also use this kind of mechanism to promote information visualization, through, for example, the home/etc/motd file, like:

– the name of the machine,
– the version of its operating system,
– distribution
services important assets (NTP, SSH, etc.)