A practical approach to PS1 command prompt format for Linux in batch server management _linux

Source: Internet
Author: User
Tags centos

At this time, we will certainly often encounter such a problem: the operation of the server because of something interrupted, go back to the operation will certainly be ifconfg to confirm whether I want to operate the server, because the image can not be recognized.

Therefore, it is necessary for us to optimize this PS1 command line prompt. Every operation of the Victoria siege Lions must have their own habits, but I still recommend a server batch management comparison of the use of PS1 format it!

PS1 is a god horse? PS1 is a default environment variable in Linux, and as for how the current system PS1 is set, you can use the command "Env|grep PS1" to view it.

In fact, PS1 is the environment variable used to set command prompt format.

The following PS1 configuration parameters are attached:

 \d: Represents a date, in the form of weekday month date, for example: "Mon Aug 1"
 \h: 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, such as the previous example, the fc4,.linux is omitted
 \ t: Display time is 24-hour format, such as: HH:MM:SS
 \ t: Display time is 12-hour format
 \a: Display time is 24-hour format: hh:mm
 \u: Current user's account name
 \v:bash version information
 \w: Full working directory name. Home directory will replace
 \w: Use basename to get the working directory name, so only the last directory
 \#: The first few commands
 \$: Prompt character, if root, the prompt is: #, the normal user is: $

Of course, in order to better identify, we can add some color settings, this is not to repeat, Baidu shell color can, of course, remember to refer to the following PS1 for customization.

To better differentiate the server, I recommend using the following format:

[Username@ipaddress/pwd]#|$

For example:
[root@192.168.1.1/data/]

So PS1 can be set as follows:

Export ps1= ' \[\e[32m\][\u@192.168.1.1:\[\e[m\]\[\e[33m\]\w\[\e[m\]\[\e[32m\]]\[\e[m\]\$ '

But there are too many machines, this IP can not be manually modified each time, so it is still write a script to modify it! (Of course, you can also get the IP first, assign the variable to join the PS1)

The script is simple:

#!/bin/sh ######################################################################### # Update PS1 like [ root@192.168.1.113/data]# # ######################################################################### #先判断网卡是否存在,
 My side eth1 is the intranet network card ifconfig eth1 >/dev/null 2>&1 if [[$!= 0]] then echo ' interface eth1 not exsit! ';
 Exit 1 fi #Centos/redhat 7 ifconfig shows the result is not inet addr: But inet direct IP, so here needs to judge: function Get_eth1ip () {if [[$1-eq 7]] Then #for CentOS 7 eth1_ip=$ (ifconfig eth1 |awk '/inet/{print $} ' |awk ' {print $} ') Else eth1_ip=$ (ifconfig ETH 1 |awk-f ":" '/inet addr:/ {print $} ' |awk ' {print} ') fi} test-f/etc/redhat-release && grep 7/etc/redhat-release >/dev/null 2>  &1 && get_eth1ip 7 test-f/etc/centos-release && grep 7/etc/redhat-release >/dev/null 2>&1 && Get_eth1ip 7 | | Get_eth1ip Echo $eth 1_ip | Grep-e "[0-9]{1,3}\. [0-9] {1,3}\. [0-9] {1,3}\. [0-9] {1,3} ' >/dev/null 2>&1 if [$?!= 0]] then echo ' Eth1_ip is empty! ' Exit 1 fi function export () {echo "Export ps1= ' \[\e[32m\][\u@${eth1_ip}:\[\e[m\]\[\e[33m\]\w\[\e[m\]\[\e[32m\]]\[\e[m" \]\\$ ' >>${1} && \ echo-e ' \033[32m Update \033[0m \033[33m${1}\033[33m \033[32msuccess! Please relogin your system for Refresh ... \033[0m "} function home_env () {if [[!-Z $]] then home=$1 else home =/root fi #有的用户可能会在家目录下自定义一些配置, that is. Proflie this hidden file, so you also need to update test-f $home/.profile && (sed-i '/export ps1=/d ' $ Home/.profile Export $home/.profile)} #获取当前用户id, if the root group can operate/etc/profile userid=$ (ID | awk ' {print} ' | sed-e ' s/=//'-E ' s/(//' E ' s/)//' |awk ' {print $} ') if [[$userid = 0]] Then #for all sed-i '/export ps1=/d '/etc/profi Le export/etc/profile #for root home_env #如果其他用户需要修改, just open three lines and modify other to user name #id other >/dev/null 2>&1 &A mp;& (# home_env ~other #) Else #for userself home_env ~ fi

OK, finally direct source./update_ps1.sh can see the effect:

Re-login or source/etc/profile, you can see the effect of:

After this setting, you can clearly know what the server is now, without confusing.

Original: http://zhangge.net/5058.html

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.