Monday, August 3, 2009

How Much For A Betty Boop Cake

Script Tip # 9: color text output in the terminal

Werden Farben richtig eingesetzt, erleichtern sie die Erkennung durch ihre strukturierende Wirkung ungemein. Das gilt auch für das Terminal, leider sind dort die Farbkennungen weder leicht zu errinnern, noch erhöhen sie die Lesbarkeit des Skriptes. Um dieses Problem zu umgehen, kann man sich eine kleine Funktion schreiben, der man als Parameter einen Text und einen Farbnamen passes. The function does the rest, is the text color on the terminal.
Farbige_Textausgabe function () {local color case "$ 2" r *) color = '31m ';; # red ge *) color = '1; 33m';; # yellow gr *) color = '1; 32m ' ;; # green b *) color = '34m ';; blue esac # echo-e "\\ e [$ color $ 1 \\ e [0m"} Here are just four colors have been defined, but each is free to add more . The call in a script is then very simple:

that remains is the question of what to do with the function? You can paste them into the bashrc, but who frequently works with all scripts should write more frequently used functions in a file (for example, function library), which at the beginning of each script to be "gesourced".!

 # / usr / bin / env bash # # creates # Author # # Description changed parameters source $ HOME / bin / Function Library ... exit 0 

Thus prepared at every point of the script can now be called the function Farbige_Textausgabe. The script is readable and clear, just as the output. Now it is only to keep a color scheme, for example, error messages, red, yellow and hints success stories green.

 What color combinations are available and how they look on screen? This question is answered a little script that outputs all the combinations of characters and background color, and their respective coding: 

# / usr / bin / env bash # # Filename color table # # Author Globenaut created 03/08/2008 modified 15/11/2007 # # Description Returns a table of the ANSI color. # Parameter $ 1: (! 3 characters) short sample text, optional. t = $ {1:-GM8} echo-e "\\ n 40m 41m 42m 43m 44m 45m 46m 47m" for VGE in 'M' '1m' '30m' '1; 30m '' 31m ''1; 31m' '32m' '1; 32m '' 33m ''1; 33m' '34m' '1; 34m '' 35m ' 1; 35m '' 36m ''1; 36m' '37m' '1; 37m ', Thu VG = $ {VGE / / /} echo-en "$ VGE \\ e [$ VG $ t" for HG in 40m 41m 42m 43m 44m 45m 46m 47m; do echo-en "\\ e [$ VG \\ e [$ HG $ t \\ e [0m" done echo done echo

 If the call to the script not pass a parameter, GM8 (a letter with In length, a wider capital letter and one number is used) as example text, otherwise the text from the parameters. For more than three characters, however, gets mixed up the table. The end result should look something like this: 

ANSI colors against a black background terminal

 

0 comments:

Post a Comment