#!/bin/bash # Author: Nils Gerstner # Last revision: 15 nov 2022 18:00 # Version: v0.1 # Description: #=== VARIABLES ================================================================# declare -r SCRIPT_NAME=$(basename "$BASH_SOURCE" .sh) declare -r HELP_MSG="Usage: $SCRIPT_NAME [OPTION]... [ARG]... -h display this help and exit " #=== FUNCTIONS ================================================================# # Print colored messages _cmsg() { case "$1" in black) COLOR="\033[0;30m" ;; white) COLOR="\033[1;37m" ;; grey) COLOR="\033[1;30m" ;; lgrey) COLOR="\033[0;37m" ;; red) COLOR="\033[0;31m" ;; lred) COLOR="\033[1;31m" ;; green) COLOR="\033[0;32m" ;; lgreen) COLOR="\033[1;32m" ;; brown) COLOR="\033[0;33m" ;; yelow) COLOR="\033[1;33m" ;; blue) COLOR="\033[0;34m" ;; lblue) COLOR="\033[1;34m" ;; purple) COLOR="\033[0;35m" ;; lpurple) COLOR="\033[1;35m" ;; cyan) COLOR="\033[0;36m" ;; lcyan) COLOR="\033[1;36m" ;; nocolor) COLOR="\033[0m" ;; esac printf "${COLOR}${2}\33[0m\n" } # exit the shell(default status code: 1) after printing the message to stderr _bail() { >&2 echo -ne "$1" exit ${2-1} } # print the usage and exit the shell(default status code: 2) _usage() { local status=2 if [[ "$1" =~ ^[0-9]+$ ]]; then status=$1 shift fi _bail "${1}$HELP_MSG" $status } # print fancy error message _failure() { local lineno=$1 local msg=$2 >&2 echo "Failed at $lineno: $msg" } #=== SET ENVIRONMENT AND EXIT CONDITIONS ======================================# # -e Exit immediately on error # -E ERR trap is inherited by shell functions set -eE -o functrace trap '_failure ${LINENO} "$BASH_COMMAND"' ERR #=== HANDLE ARGUMENTS =========================================================# while getopts ":hie:" opt; do case $opt in i) # Do something #(for ex. execute a function or set a switch variable) ;; e) # Option with argument e_argument=${OPTARG} ;; h) _usage 0 ;; \?) _usage "Invalid option: -$OPTARG \n" ;; esac done shift $OPTIND [[ "$#" -lt 1 ]] && usage "Too few arguments\n" #=== MAIN CODE GOES HERE ======================================================# echo "Start!"