Raspbian: De bash prompt aanpassen

Wanneer je met behulp van SSH verbinding maakt met een Raspberry Pi dan verschijnt nadat de verbinding tot stand is gekomen en er is ingelogd de bash prompt. De bash prompt is standaard ingesteld op ‘pi@raspberrypi:~ $', dit is de gebruikersnaam, hostnaam en de pwd (present working directory).

pi@raspberrypi:~ $

Via de bash_profile kun jezelf aangeven welke gegevens er moeten verschijnen in de bash prompt. Zo kun je kun bijvoorbeeld de volgorde wijzigen, extra informatie en kleur toevoegen. Een handig hulpmiddel hierbij is de online Bash Profile Generator, met behulp van deze website kun je heel eenvoudig doormiddel van drag & drop je eigen prompt samenstellen.

Bash Profile Generator

Open en een webbrowser de Bash Profile Generator:

http://xta.github.io/HalloweenBash/

bash prompt raspberry pi aanpassen-1

In de sectie ‘Your Configuration' kun jezelf de gewenste info toevoegen of verwijderen. Daarnaast is het mogelijk om de voor en achtergrond kleuren aan te passen naar eigen keuze.

Bash Profile aanpassen

Onderaan de webpagina in sectie 3 verschijnt automatisch de export code die je moet toevoegen aan de bash_profile. Kopieer de gegenereerde code naar het klembord van je computer. Open hierna op je Raspberry Pi met de nano editor het bestand bash_profile.

nano ~/.bash_profile

Plak de gegenereerde code in het bestand. Het kan zijn dat er nog geen bash_profile aanwezig is, in dat geval is het bestand leeg. Sla de wijzigingen op met ctrl+X, Y, gevolgd door ‘Enter'.

bash prompt raspberry pi-aanpassen-2

De wijzigingen zijn niet direct actief, hiervoor moet de bash profile opnieuw geladen worden. Dit kun je doen door de SSH sessie af te sluiten of handmatig het onderstaande commando uit te voeren. Het commando forceert dat de bash_profile opnieuw wordt ingeladen.

. ~/.bash_profile

Na het uitvoeren van het commando of het opnieuw maken van de SSH sessie zal je zien dat de wijzigingen zijn doorgevoerd.

Bash prompt aanpassingen verwijderen

Mocht je terug willen keren naar de standaard instellingen open dan met de nano editor het bash_profile bestand.

nano ~/.bash_profile

bash prompt raspberry pi-aanpassen-3

Verwijder de gehele regel die begint met export PS1= en herlaad de bash profile:

. ~/.bash_profile

1 gedachte over “Raspbian: De bash prompt aanpassen

  1. O.A de kleuren veranderen van de prompt naarmate de load veranderd.
    Groen – alles oke
    Geel – load hoger
    Rood – Load hoog
    Wit – Je Raspberry is witheet ­čÖé
    ————————–

    ## ~/.bashrc: executed by bash(1) for non-login shells.

    ## Note: PS1 and umask are already set in /etc/profile. You should not
    ## need this unless you want different defaults for root.
    ## PS1=’${debian_chroot:+($debian_chroot)}\h:\w\$ ‘
    ## umask 022

    HISTCONTROL=ignoreboth
    export HISTCONTROL=ignoreboth

    ## You may uncomment the following lines if you want `ls’ to be colorized:

    export LS_OPTIONS=’–color=auto’
    eval “`dircolors`”
    alias ls=’ls $LS_OPTIONS’
    alias ll=’ls $LS_OPTIONS -l’
    alias l=’ls $LS_OPTIONS -lA’

    ## Normal Colors
    Black=’\e[0;30m’ # Black
    Red=’\e[0;31m’ # Red
    Green=’\e[0;32m’ # Green
    Yellow=’\e[0;33m’ # Yellow
    Blue=’\e[0;34m’ # Blue
    Purple=’\e[0;35m’ # Purple
    Cyan=’\e[0;36m’ # Cyan
    White=’\e[0;37m’ # White

    ## Bold
    BBlack=’\e[1;30m’ # Black
    BRed=’\e[1;31m’ # Red
    BGreen=’\e[1;32m’ # Green
    BYellow=’\e[1;33m’ # Yellow
    BBlue=’\e[1;34m’ # Blue
    BPurple=’\e[1;35m’ # Purple
    BCyan=’\e[1;36m’ # Cyan
    BWhite=’\e[1;37m’ # White

    ## Background
    On_Black=’\e[40m’ # Black
    On_Red=’\e[41m’ # Red
    On_Green=’\e[42m’ # Green
    On_Yellow=’\e[43m’ # Yellow
    On_Blue=’\e[44m’ # Blue
    On_Purple=’\e[45m’ # Purple
    On_Cyan=’\e[46m’ # Cyan
    On_White=’\e[47m’ # White

    NC=”\e[m” # Color Reset

    ## Some more alias to avoid making mistakes:
    alias rm=’rm -i’
    alias cp=’cp -i’
    alias mv=’mv -i’

    let upSeconds=”$(/usr/bin/cut -d. -f1 /proc/uptime)”
    let mins=$((${upSeconds}/60%60))
    let hours=$((${upSeconds}/3600%24))
    let days=$((${upSeconds}/86400))
    UPTIME=`printf “%02d dagen, %02d uur en %02d minuten” “$days” “$hours” “$mins”`

    echo “Welcome Stranger..”
    echo “$(tput setaf 3)
    System………….: `uname -srmo`
    Date……………: `date +”%A, %e %B %Y, %R Hour”`
    Uptime………….: ${UPTIME}
    Load……………: `cat /proc/loadavg`
    Local IP Addresses.: `ip address list | grep “inet ” | grep -v 127.0.0 | cut -d ” ” -f 6 | cut -d “/” -f 1`
    CPU Temperature….: `exec — /opt/vc/bin/vcgencmd measure_temp | cut -c “6-9″` C
    Volt……………: `exec — vcgencmd measure_volts | cut -c “6-9″` C
    Eindhoven Weather..: `curl -s “http://rss.accuweather.com/rss/liveweather_rss.asp?metric=1&locCode=EUR|NL|NL007|EINDHOVEN” | sed -n ‘/Currently:/ s/.*: \(.*\): \([0-9]*\)\([CF]\).*/\2┬░\3, \1/p’`
    $(tput sgr0)”

    echo ——————-
    MHZ=$(cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq)
    echo CPU Speed $(($MHZ/1000)) Mhz
    echo ——————-

    # curl https://ip.appspot.com

    echo

    #####

    #————————————————————-
    # Shell Prompt – for many examples, see:
    # http://www.debian-administration.org/articles/205
    # http://www.askapache.com/linux/bash-power-prompt.html
    # http://tldp.org/HOWTO/Bash-Prompt-HOWTO
    # https://github.com/nojhan/liquidprompt
    #————————————————————-
    # Current Format: [TIME USER@HOST PWD] >
    # TIME:
    # Green == machine load is low
    # Orange == machine load is medium
    # Red == machine load is high
    # ALERT == machine load is very high
    # USER:
    # Cyan == normal user
    # Orange == SU to user
    # Red == root
    # HOST:
    # Cyan == local session
    # Green == secured remote connection (via ssh)
    # Red == unsecured remote connection
    # PWD:
    # Green == more than 10% free disk space
    # Orange == less than 10% free disk space
    # ALERT == less than 5% free disk space
    # Red == current user does not have write privileges
    # Cyan == current filesystem is size zero (like /proc)
    # >:
    # White == no background or suspended jobs in this shell
    # Cyan == at least one background job in this shell
    # Orange == at least one suspended job in this shell
    #
    # Command is added to the history file each time you hit enter,
    # so it’s available to all shells (using ‘history -a’).

    # Test connection type:
    if [ -n “${SSH_CONNECTION}” ]; then
    CNX=${Green} # Connected on remote machine, via ssh (good).
    elif [[ “${DISPLAY%%:0*}” != “” ]]; then
    CNX=${ALERT} # Connected on remote machine, not via ssh (bad).
    else
    CNX=${BCyan} # Connected on local machine.
    fi

    # Test user type:
    if [[ ${USER} == “root” ]]; then
    SU=${Red} # User is root.
    elif [[ ${USER} != $(logname) ]]; then
    SU=${BRed} # User is not login user.
    else
    SU=${BCyan} # User is normal (well … most of us are).
    fi

    NCPU=$(grep -c ‘processor’ /proc/cpuinfo) # Number of CPUs
    SLOAD=$(( 100*${NCPU} )) # Small load
    MLOAD=$(( 200*${NCPU} )) # Medium load
    XLOAD=$(( 300*${NCPU} )) # Xlarge load

    # Returns system load as percentage, i.e., ’40’ rather than ‘0.40)’.
    function load()
    {
    local SYSLOAD=$(cut -d ” ” -f1 /proc/loadavg | tr -d ‘.’)
    # System load of the current host.
    echo $((10#$SYSLOAD)) # Convert to decimal.
    }

    # Returns a color indicating system load.
    function load_color()
    {
    local SYSLOAD=$(load)
    if [ ${SYSLOAD} -gt ${XLOAD} ]; then
    echo -en ${ALERT}
    elif [ ${SYSLOAD} -gt ${MLOAD} ]; then
    echo -en ${Red}
    elif [ ${SYSLOAD} -gt ${SLOAD} ]; then
    echo -en ${Yellow}
    else
    echo -en ${Green}
    fi
    }

    # Returns a color according to free disk space in $PWD.
    function disk_color()
    {
    if [ ! -w “${PWD}” ] ; then
    echo -en ${Red}
    # No ‘write’ privilege in the current directory.
    elif [ -s “${PWD}” ] ; then
    local used=$(command df -P “$PWD” |
    awk ‘END {print $5} {sub(/%/,””)}’)
    if [ ${used} -gt 95 ]; then
    echo -en ${ALERT} # Disk almost full (>95%).
    elif [ ${used} -gt 90 ]; then
    echo -en ${BRed} # Free disk space almost gone.
    else
    echo -en ${Green} # Free disk space is ok.
    fi
    else
    echo -en ${Cyan}
    # Current directory is size ‘0’ (like /proc, /sys etc).
    fi
    }

    # Returns a color according to running/suspended jobs.
    function job_color()
    {
    if [ $(jobs -s | wc -l) -gt “0” ]; then
    echo -en ${BRed}
    elif [ $(jobs -r | wc -l) -gt “0” ] ; then
    echo -en ${BCyan}
    fi
    }

    # Adds some text in the terminal frame (if applicable).

    # Now we construct the prompt.
    PROMPT_COMMAND=”history -a”
    case ${TERM} in
    *term | rxvt | linux)
    PS1=”\[\$(load_color)\][\A\[${NC}\] ”
    # Time of day (with load info):
    PS1=”\[\$(load_color)\][\A\[${NC}\] ”
    # User@Host (with connection type info):
    PS1=${PS1}”\[${SU}\]\u\[${NC}\]@\[${CNX}\]\h\[${NC}\] ”
    # PWD (with ‘disk space’ info):
    PS1=${PS1}”\[\$(disk_color)\]\W]\[${NC}\] ”
    # Prompt (with ‘job’ info):
    PS1=${PS1}”\[\$(job_color)\]>\[${NC}\] ”
    # Set title of current xterm:
    PS1=${PS1}”\[\e]0;[\u@\h] \w\a\]”
    ;;
    *)
    PS1=”(\A \u@\h \W) > ” # –> PS1=”(\A \u@\h \w) > ”
    # –> Shows full pathname of current dir.
    ;;
    esac

    export TIMEFORMAT=$’\nreal %3R\tuser %3U\tsys %3S\tpcpu %P\n’
    export HISTIGNORE=”&:bg:fg:ll:h”
    export HISTTIMEFORMAT=”$(echo -e ${BCyan})[%d/%m %H:%M:%S]$(echo -e ${NC}) ”
    export HISTCONTROL=ignoredups
    export HOSTFILE=$HOME/.hosts # Put a list of remote hosts in ~/.hosts

Een reactie plaatsen

%d bloggers liken dit: