Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to change the color of font or background in shell or perl

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the knowledge of "how to change the font or background color in shell or perl". In the operation of practical cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

When you mention the color of the terminal under shell, you have to mention several prompt variables:

PS1: the default prompt, that is, the familiar prompt that we see every day after logging on to the system remotely and opening the terminal.

[root@vps ~] $echo $PS1

[\ u@\ h\ W] $

PS2: the one where we type "\" under our command line and the other line shows that we can't finish one line.

[root@vps ~] $echo "hello"\

> world

Hello world

[root@vps ~] $echo $PS1

[\ u@\ h\ W] $

PS3: the one we show when we want to use select in the shell script

The code is as follows:

#! / bin/bash

# PS3= "Select a script language (1-4):"

Select i in perl php python shell exit

Do

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 it in the script.

The code is as follows:

#! / bin/bash

PS3= "Select a script language (1-4):"

Select i in perl php python shell exit

Do

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, .

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

The code is 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's the "+". Let's reset.

The code is 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 of the basics, let's get back to our topic: let's take PS1 as an example:

So where is this variable set? Our current redhat (including centos, of course) is in the / etc/bashrc file:

["$PS1" = "\\ s -\\ v\\ $"] & & PS1= "[\ u@\ h\ W]\ $"

So what do these\ w\ u respectively mean? They are as follows:

\ d: represents a date in the format of weekday month date, for example: "Mon Aug 1"

\ H: full host name. For example, if my machine name is fc4.linux, then this name is fc4.linux

\ h: take only the first name of the host. As in the example above, fc4,.linux is omitted.

\ t: display time in 24-hour format, such as HH:MM:SS

\ t: the display time is in 12-hour format

\ a: display time is 24 hours format: HH:MM

\ U: current user's account name

\ v: version information of BASH

\ w: full working directory name. The home catalogue will be replaced by ~

\ W: use basename to get the working directory name, so only the last directory will be listed

\ #: the number of orders issued

\ $: prompt character. If it is root, the prompt is: #, and the average user is: $

We are free to play what our default prompt looks like. Let's not deviate from our theme. Our theme is color.

So what is its grammar?

\ e [--indicates the starting position of the color prompt

XTYM-the code that represents the color. The color code is described below (of course, you can write more than one in succession, separated by ";", such as 1 / 5 / 35m).

\ e [m-- indicates the end of the prompt for the substitute color.

The value of x is:

0 OFF

1 highlight

4 underline

5 flicker

7 anti-white display

8 not visible

The value of y:

Foreground background color

-

30 40 black

31 41 pale color

32 42 shades

33 43 shades

34 44 shades

35 45 purplish purple

36 46 cyan

37 47 White

I've told you what the grammar says, and then you're free to play.

Let's give an example of how to use it in a script.

Okay, but if we don't want to affect the color behind us, we'd better write this when we turn it off:\ e [0m

One that twinkles and changes color:

Echo-e'\ e [35th 5ten 1m for Example:\ e [0m'

35 is color, 5 is flashing, 1 is foreground color, in which ";" is used, but the order does not matter;\ e can be written as\ 033

What about in perl? We use the Term::ANSIColor module to do this:

The code is as follows:

#! / usr/bin/perl

Use strict

Use Term::ANSIColor

Print color 'bold red'

Print "Hello word\ n"

Print color 'reset'

It's troublesome to write like this, just write a function:

The code is 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')

This is the end of "how to change the font or background color in shell or perl". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report