( Nextra | 2022. 01. 23., v – 14:33 )

Szerkesztve: 2022. 01. 23., v – 14:33

Ezt alkottam:

#!/bin/bash
# Colorize the bash prompt based on which user is currently logged in.
# For this to work, the - prompt="myPromptx" - must be present in the user's .bashrc file.
# If not present, or #comment then set default myPrompt1 example.
# 
# complett rewrite 2022 - made by csablak
# tested Mageia 8

# foreground color
#Black="\e[30m"
Red="\e[91m"
Green="\e[92m"
Yellow="\e[93m"
#Blue="\e[94m"
#Magenta="\e[95m"	
Cyan="\e[96m"
White="\e[97m"
NC="\e[0m"

# background color
#BlackBG="\e[40m"
#RedBG="\e[41m"
#GreenBG="\e[42m"
#YellowBG="\e[43m"
#BlueBG="\e[44m"
#MagentaBG="\e[45m"	
#CyanBG="\e[46m"
#WhiteBG="\e[47m"

# color setting example: \[$Red\]

# $ - user login - always green (exclude myPrompt0)
# # - root login - always red (exclude myPrompt0)

# examples
myPrompt0 () {
    # nocolor: user@localhost[~]$
    export PS1="\n\u@\h:[\w]\\$ "
}


myPrompt1 () {
    ######## one color: user- green, root- red. This is default colorprompt.
    # user@localhost[~]$
    if [ -n "$PS1" ]; then
        if [ "$EUID" = "0" ]; then
            export PS1="\n\[$Red\]\u@\h:[\w]\\$ \[$NC\]"
        else
            export PS1="\n\[$Green\]\u@\h:[\w]\\$ \[$NC\]"
        fi
    fi
}

myPrompt2 () {
######## three color: # colour user, @, [~] 
# user@localhost[~]$
if [ -n "$PS1" ]; then
    if [ "$EUID" = "0" ]; then
        export PS1="\n\[$Red\]\u\[$White\]@\[$Red\]\h:\[$Cyan\][\w]\[$Red\]\\$ \[$NC\]"	
    else
        export PS1="\n\[$Green\]\u\[$White\]@\[$Green\]\h:\[$Yellow\][\w]\[$Green\]\\$ \[$NC\]"
    fi
fi
}


myPrompt3 () {
######### two line more color + date, time: 
# user@localhost: Thu jan 20 - 09:48:26
# bash 5.1 [~]$
if [ -n "$PS1" ]; then
    if [ "$EUID" = "0" ]; then
        export PS1="\n\[$Red\]\u\[$White\]@\[$Red\]\h: \[$White\]\d - \t\n\[$Red\]\s \v \[$Yellow\][\w]\[$Red\]\\$ \[$NC\]"
    else
        export PS1="\n\[$Green\]\u\[$NC\]@\[$Green\]\h: \[$White\]\d - \t\n\[$Green\]\s \v \[$Yellow\][\w]\[$Green\]\\$ \[$NC\]"
    fi
fi
}

myPrompt4 () {
######### my prompt (csablak) two line 2 color:
# ┌─user:[folder]
# └──$
if [ -n "$PS1" ]; then
    if [ "$EUID" = "0" ]; then
        export PS1="\n\[$Red\]┌─\u:\[$Cyan\][\w]\[$Red\] \n└──\\$ \[$NC\]"
    else
        export PS1="\n\[$Green\]┌─\u:\[$Yellow\][\w]\[$Green\] \n└──\\$ \[$NC\]"
    fi
fi
}

myPrompt5 () {
# Two color. Import from 92user-color-clock
# (17:43:02) [user@localhost ~]$
if [ -n "$PS1" ]; then
    if [ "$EUID" = "0" ]; then
        export PS1="(\t) \[$Red\][\u@\h \w]\\$ \[$NC\]"
    else
        export PS1="(\t) \[$Green\][\u@\h \w]\\$ \[$NC\]"
    fi
fi
}

myPrompt6 () {
# Two color + PS2 (no full path)
# [09:28] [user@localhost ~]$
if [ -n "$PS1" ]; then
    if [ "$EUID" = "0" ]; then
	export PS1="\n\[${Cyan}\][\A]\[${Red}\][\u@\h \W]\[$NC\]# "
	export PS2="\n\[${Cyan}\][\A]\[${Red}\][\u@\h \W]\[$NC\]> "
    else
	export PS1="\n\[${Cyan}\][\A]\[${Green}\][\u@\h \W]\[$NC\]$ "
	export PS2="\n\[${Cyan}\][\A]\[${Green}\][\u@\h \W]\[$NC\]> "
    fi
fi
}

### TODO #### for now only max 9 example possible.  
# Warning! If add plus myPrompt example, then change maxnumber!
maxnumber="6"

# currently used
prompt="$(awk -F '"' '/^prompt/ {print $2}' ~/.bashrc)"
number="$(echo "${prompt}" | tail -c2)"

if (( number > maxnumber )); then
    myPrompt1
elif [ "${number}" = "" ]; then
    myPrompt1
else
    ${prompt}
fi

 Ha a .bashrc fájl tartalmaz prompt="myPrompt" sort, akkor a számnak megfelelő PS1 aktiválódik.