Skip to content
Snippets Groups Projects
inc_bash.sh 2.42 KiB
# ======================================================================
#
# shared bash functions
#
# ======================================================================

# ----------------------------------------------------------------------
# returncodes
# ----------------------------------------------------------------------

typeset -i rc=0

# Fetch returncode of last executed command.
# - It summarizes all exitcodes into rc (= if any job failed it is <> 0)
# - It stores the last exitcode in myrc
function fetchrc(){
  myrc=$?
  rc=$rc+$myrc

  if [ $myrc -eq 0 ]; then
    cecho ok "OK"
  else
    cecho error "FAILED (rc=$myrc)"
  fi
}


# ----------------------------------------------------------------------
# colors
# ----------------------------------------------------------------------

  # ------------------------------------------------------------
  # color text
  # ------------------------------------------------------------

  # set a terminal color by a keyword
  # param  string  keyword to set a color; one of reset | head|cmd|input | ok|warning|error
  function color(){
    sColorcode=""
    case $1 in
      "reset") sColorcode="0"
               ;;
      "head")  sColorcode="33" # yellow
               ;;
      "cmd")   sColorcode="94" # light blue
               ;;
      "input") sColorcode="92" # green
               ;;
      "ok") sColorcode="92" # green
               ;;
      "warning") sColorcode="33" # yellow
               ;;
      "error") sColorcode="91" # red
               ;;
    esac
    if [ ! -z ${sColorcode} ]; then
      echo -ne "\e[${sColorcode}m"
    fi    
  }

  # echo a colored text and reset color
  # param  string  keyword to set a color - see function color() above
  # param  string  text to show
  function cecho(){
    color $1
    shift
    echo $*
    color reset
  }

  function showPrompt(){
    color input
    echo -n "$*"
    color reset
  }
  # ----------------------------------------------------------------------
  # headlines 
  # ----------------------------------------------------------------------

  function h1(){
    color head
    echo
    echo
    echo "########## $* ##########"
    echo
    color reset
  }


  function h2(){
    color head
    echo
    echo "========== $* =========="
    echo
    color reset
  }

  function h3(){
    color head
    echo
    echo "---------- $*"
    color reset
  }

# ----------------------------------------------------------------------