Change the color of a font or background in a shell or Perl _linux Shell

Source: Internet
Author: User
Tags vps

There are several prompt variables to mention when the color of the terminal is mentioned under the shell:

PS1: The default prompt, that is, after the remote boarding system, open the terminal, we see the familiar prompt every day;

[Root@vps ~] $echo $PS 1
[\u@\h \w]$

PS2: It is our command line to write the "\" on our command line, and the one that is displayed on the other line;

[Root@vps ~] $echo "Hello" \
> "World"
Hello World
[Root@vps ~] $echo $PS 1
[\u@\h \w]$

PS3: That's the one we show when we use select in the shell script;

Copy Code code as follows:

#!/bin/bash
#PS3 = "Select a script language (1-4):"
Select I in Perl php python shell exit
Todo
Case $i in
Perl) echo "I like Perl";;
php) echo "PHP is good";;
Python) echo "Xiangjun like Python";;
Shell) echo "Shell is my favourite";;
exit) exit;;
Esac
Done

[Root@vps tmp] $bash select.sh
1) Perl
2) PHP
3) Python
4) Shell
5) Exit
#? 1
I like Perl
#?

The default is "#?", hehe, let's change the script

Copy Code code as follows:

#!/bin/bash
ps3= "Select a script language (1-4):"
Select I in Perl php python shell exit
Todo
Case $i in
Perl) echo "I like Perl";;
php) echo "PHP is good";;
Python) echo "Xiangjun like Python";;
Shell) echo "Shell is my favourite";;
exit) exit;;
Esac
Done

[Root@vps tmp] $bash select.sh
1) Perl
2) PHP
3) Python
4) Shell
5) Exit
Select a script language (1-4): 3
Xiangjun like Python
Select a script language (1-4):

It's changed, haha.

PS4: We debug the shell script and we'll bash-x myscripts.sh (or set-x in script) that prompt;

Copy Code code as follows:

Select a script language (1-4): 5
[Root@vps tmp] $bash-x select.sh
+ ps3= ' Select a script language (1-4): '
+ Select I in Perl php python shell exit

That is the "+", we reset;

Copy Code code as follows:

[Root@vps tmp] $export ps4= ">>"
[Root@vps tmp] $bash-x select.sh
>>ps3= ' Select a script language (1-4): '
>>select i in Perl php python shell exit

Well, after recalling some basic knowledge, back to our topic: We take PS1 as an example to illustrate:
So where is this variable set up? In our current redhat (including CentOS, of course) is in the/ETC/BASHRC file:

["$PS 1" = "\\s-\\v\\\$"] && ps1= "[\u@\h \w]\\$"

What do these \w \u mean? were as follows:

\d: Represents a date, formatted as weekday month date, for example: "Mon Aug 1″
\h: The full host name. For example: My machine name is: Fc4.linux, then this name is Fc4.linux
\h: Only the first name of the host, as in the example above, is omitted for Fc4,.linux
\ t: Displays a time of 24-hour format, such as: HH:MM:SS
\ t: Display time in 12-hour format
\a: Display time is 24 hour format: hh:mm
\u: Current user's account name
Version information for \v:bash
\w: The full working directory name. Home directory will be replaced by ~
\w: Use basename to get the working directory name, so only the last directory will be listed
\#: The first few orders issued
\$: Prompt character, if root, the prompt is: #, the average user is: $
We are free to play our default prompts what it looks like, below do not deviate from our theme, our theme is color;




So what is the syntax of it?

\e[--the starting position of the hint that represents the color
x;ym--the code that represents the color. The color code is described below (of course you can write multiple, with ";" Separate, such as 1;5;35m)
\e[m--the end position of the hint that represents the color of the generation

The value of X is:

0 off
1 highlighting
4 Underline
5 flashes
7 Anti-white display
8 Not visible

Value of y:

Foreground background color
---------------------------------------
30 40 Black
31 41 Red Color
32 42 Green
33 43 Yellow Color
34 44 Blue
35 45 Purple red color
36 46 Blue Blue
37 47 White

The grammar instructions tell you, then you are free to play;

How to use in the script, the first example;



OK, but if you don't want to affect the color behind us, we'd better write it off at the end: \e[0m



Flashing and changing the color of one:

Echo-e ' \e[35;5;1mfor example:\e[0m '

35 is the color, 5 represents the flashing, 1 represents the foreground color, when used ";" Apart, the order is indifferent; \e can be written as \033;

What about in Perl? This we are using Term::ansicolor this module:

Copy Code code as follows:

#!/usr/bin/perl
Use strict;
Use Term::ansicolor;
Print color ' bold red ';
Print "Hello word\n";
Print color ' reset ';




This writing is very troublesome, write a function to forget:

Copy Code code as follows:

#!/usr/bin/perl
Use strict;
Use Term::ansicolor;
Sub colormessage{
My ($colors, $messages) = @_;
Print color "bold $colors";
print "$messages \ n";
Print color ' reset ';
}

Colormessage (' green ', ' Hello word ');


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.