From a99d4758a39ec13ad48313c3af96688710bedf58 Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Thu, 25 Jun 2020 14:08:02 +0200 Subject: [PATCH] Updating files/bashrc-el7 to our needs --- files/bashrc-centos8 | 2 +- files/bashrc-el7 | 261 ++++++++++++++++++++++++++++++++++++------- 2 files changed, 222 insertions(+), 41 deletions(-) diff --git a/files/bashrc-centos8 b/files/bashrc-centos8 index 1915cbd..01c8048 100644 --- a/files/bashrc-centos8 +++ b/files/bashrc-centos8 @@ -196,7 +196,7 @@ if [[ -z "$BASHRCSOURCED" ]]; then fi - if [ -z "$PROMPT_COMMAND" ]; then + if [[ -z "$PROMPT_COMMAND" ]]; then PROMPT_COMMAND='history -a' case $TERM in diff --git a/files/bashrc-el7 b/files/bashrc-el7 index 4a8c4cd..84f47c7 100644 --- a/files/bashrc-el7 +++ b/files/bashrc-el7 @@ -1,3 +1,5 @@ +#!/bin/bash + # /etc/bashrc # System wide functions and aliases @@ -8,44 +10,221 @@ # /etc/profile.d/ to make custom changes to your environment, as this # will prevent the need for merging in future updates. +#------------------------------------------------------------------- +prompt_status() { + local e=$?; + [[ $e != 0 ]] && echo -e "$e "; +} + +#------------------------------------------------------------------- +append_path() { + if [[ "$#" == 0 ]] ; then + echo "append_path(): No path given." >&2 + return 2 + fi + + local verbose= + if [[ "$VERBOSE" ]] ; then + if [[ "$VERBOSE" =~ ^[n|f] || "$VERBOSE" == "0" ]] ; then + : + else + verbose=y + fi + fi + + local path= + while [[ "$#" != 0 ]] ; do + path="$1" + shift + + if [[ ! -e "${path}" ]] ; then + if [[ $verbose ]] ; then + echo "append_path(): directory '${path}' does not exists." >&2 + fi + elif [[ ! -d "${path}" ]] ; then + echo "append_path(): path '${path}' is not a directory." >&2 + continue + fi + + if [[ -z "${PATH}" ]] ; then + export PATH="${path}" + elif [[ "${PATH}" =~ ^${path}: || "${PATH}" =~ :${path}: || "${PATH}" =~ :${path}$ ]] ; then + if [[ $verbose ]] ; then + echo "append_path(): path '${path}' already included." >&2 + fi + continue + else + export PATH="${PATH}:${path}" + fi + + if [[ $verbose ]] ; then + echo "append_path(): PATH is now '${PATH}'." + fi + + done +} + +#------------------------------------------------------------------- +prepend_path() { + + if [[ "$#" == 0 ]] ; then + echo "prepend_path(): No path given." >&2 + return 2 + fi + + local verbose= + if [[ "$VERBOSE" ]] ; then + if [[ "$VERBOSE" =~ ^[n|f] || "$VERBOSE" == "0" ]] ; then + : + else + verbose=y + fi + fi + + local path= + while [[ "$#" != 0 ]] ; do + path="$1" + shift + + if [[ ! -e "${path}" ]] ; then + if [[ $verbose ]] ; then + echo "prepend_path(): directory '${path}' does not exists." >&2 + fi + elif [[ ! -d "${path}" ]] ; then + echo "prepend_path(): path '${path}' is not a directory." >&2 + continue + fi + + if [[ -z "${PATH}" ]] ; then + export PATH="${path}" + elif [[ "${PATH}" =~ ^${path}: || "${PATH}" =~ :${path}: || "${PATH}" =~ :${path}$ ]] ; then + if [[ $verbose ]] ; then + echo "prepend_path(): path '${path}' already included." >&2 + fi + continue + else + export PATH="${path}:${PATH}" + fi + + if [[ $verbose ]] ; then + echo "prepend_path(): PATH is now '${PATH}'." + fi + + done +} + +#------------------------------------------------------------------- +# console colors: + +detect_color() { + + local safe_term="${TERM//[^[:alnum:]]/?}" + local match_lhs="" + local use_color="false" + local term= + + if [[ -f ~/.dir_colors ]] ; then + match_lhs="${match_lhs}$( grep '^TERM ' ~/.dir_colors | sed -e 's/^TERM *//' -e 's/ .*//')" + fi + if [[ -f /etc/DIR_COLORS ]] ; then + match_lhs="${match_lhs}$( grep '^TERM ' /etc/DIR_COLORS | sed -e 's/^TERM *//' -e 's/ .*//')" + fi + if [[ -z ${match_lhs} ]] ; then + type -P dircolors >/dev/null && \ + match_lhs=$(dircolors --print-database | grep '^TERM ' | sed -e 's/^TERM *//' -e 's/ .*//') + fi + for term in ${match_lhs} ; do + # shellcheck disable=SC2053 + if [[ "${safe_term}" == ${term} || "${TERM}" == ${term} ]] ; then + use_color="true" + break + fi + done + + # console colors: + if [[ "${use_color}" == "true" ]] ; then + HAS_COLOR="y" + fi + +} + +BASHRCSOURCED="Y" +HAS_COLOR="n" + +detect_color + +# configure history +HISTSIZE=100000 +HISTFILESIZE=100000 +HISTTIMEFORMAT='%Y-%m-%d %H:%M:%S ' +HISTCONTROL=ignorespace + +# hold these special chars when piping to less +export LESS="--RAW-CONTROL-CHARS" + +# make less more friendly for non-text input files, see lesspipe(1) +[[ -x /bin/lesspipe ]] && eval "$(SHELL=/bin/sh lesspipe)" + # are we an interactive shell? -if [ "$PS1" ]; then - if [ -z "$PROMPT_COMMAND" ]; then - case $TERM in - xterm*|vte*) - if [ -e /etc/sysconfig/bash-prompt-xterm ]; then - PROMPT_COMMAND=/etc/sysconfig/bash-prompt-xterm - elif [ "${VTE_VERSION:-0}" -ge 3405 ]; then - PROMPT_COMMAND="__vte_prompt_command" - else - PROMPT_COMMAND='printf "\033]0;%s@%s:%s\007" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/~}"' - fi - ;; - screen*) - if [ -e /etc/sysconfig/bash-prompt-screen ]; then - PROMPT_COMMAND=/etc/sysconfig/bash-prompt-screen - else - PROMPT_COMMAND='printf "\033k%s@%s:%s\033\\" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/~}"' - fi - ;; - *) - [ -e /etc/sysconfig/bash-prompt-default ] && PROMPT_COMMAND=/etc/sysconfig/bash-prompt-default - ;; - esac - fi - # Turn on parallel history - shopt -s histappend - history -a - # Turn on checkwinsize - shopt -s checkwinsize - [ "$PS1" = "\\s-\\v\\\$ " ] && PS1="[\u@\h \W]\\$ " - # You might want to have e.g. tty in prompt (e.g. more virtual machines) - # and console windows - # If you want to do so, just add e.g. - # if [ "$PS1" ]; then - # PS1="[\u@\h:\l \W]\\$ " - # fi - # to your custom modification shell script in /etc/profile.d/ directory +if [[ "$PS1" ]]; then + + if [[ "${HAS_COLOR}" == "y" ]] ; then + if type -t __git_ps1 >/dev/null ; then + if [[ "$UID" = 0 ]]; then + PS1='\[\e[31m\]$(prompt_status)\[\033[01;31m\]\u\[\033[01;33m\]@\[\033[01;36m\]\h \[\033[01;33m\]\w\[\033[01;31m\]$(__git_ps1)\[\033[01;35m\] \$ \[\033[00m\]' + else + PS1='\[\e[31m\]$(prompt_status)\[\033[01;32m\]\u\[\033[01;33m\]@\[\033[01;36m\]\h \[\033[01;33m\]\w\[\033[01;31m\]$(__git_ps1)\[\033[01;35m\] > \[\033[00m\]' + fi + else + if [[ "$UID" = 0 ]]; then + PS1='\[\e[31m\]$(prompt_status)\[\033[01;31m\]\u\[\033[01;33m\]@\[\033[01;36m\]\h \[\033[01;33m\]\w \[\033[01;35m\]\$ \[\033[00m\]' + else + PS1='\[\e[31m\]$(prompt_status)\[\033[01;32m\]\u\[\033[01;33m\]@\[\033[01;36m\]\h \[\033[01;33m\]\w \[\033[01;35m\]> \[\033[00m\]' + fi + fi + else + if type -t __git_ps1 >/dev/null ; then + PS1='$(prompt_status)\u@\h \w$(__git_ps1) \$ ' + else + PS1='$(prompt_status)\u@\h \w\$ ' + fi + fi + + if [[ -z "$PROMPT_COMMAND" ]]; then + + PROMPT_COMMAND='history -a' + case $TERM in + xterm*|rxvt*) + PROMPT_COMMAND='history -a; printf "\033]0;%s@%s:%s\007" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/~}"' + ;; + screen) + PROMPT_COMMAND='history -a; printf "\033]0;%s@%s:%s\033\\" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/~}"' + ;; + *) + [[ -e /etc/sysconfig/bash-prompt-default ]] && PROMPT_COMMAND=/etc/sysconfig/bash-prompt-default + ;; + esac + fi + + # Turn on parallel history + shopt -s histappend + history -a + # Turn on checkwinsize + shopt -s checkwinsize + + alias ls='ls --color=auto' + + alias ..='cd ..' + alias ...='cd ../..' + alias cd..='cd ..' + alias cd...='cd ../..' + alias l='ls -l' + alias la='ls -la' + alias ll='ls -lA' + alias md='mkdir -p' + + alias vi='vim' + fi if ! shopt -q login_shell ; then # We're not a login shell @@ -67,10 +246,10 @@ if ! shopt -q login_shell ; then # We're not a login shell # Current threshold for system reserved uid/gids is 200 # You could check uidgid reservation validity in # /usr/share/doc/setup-*/uidgid file - if [ $UID -gt 199 ] && [ "`/usr/bin/id -gn`" = "`/usr/bin/id -un`" ]; then - umask 002 + if [[ "${UID}" -gt 199 && "$( id -gn )" == "$( id -un )" ]]; then + umask 002 else - umask 022 + umask 022 fi SHELL=/bin/bash @@ -88,5 +267,7 @@ if ! shopt -q login_shell ; then # We're not a login shell unset i unset -f pathmunge + fi + # vim:ts=4:sw=4 -- 2.39.5