C++ Library containing a lot of needful things: Stack Trace, Command Line Parser, Resource Handling, Configuration Files, Unix Command Execution, Directories, Regular Expressions, Tokenizer, Function Trace, Standard Extensions.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

template.sh 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #!/bin/bash -e
  2. ##########################################################################################
  3. #### template for bash scripts #### START BELOW ##########################################
  4. ##########################################################################################
  5. ############################################################################ begin logging
  6. # check if stdout is a terminal...
  7. if test -t 1; then
  8. # see if it supports colors...
  9. ncolors=$(tput colors)
  10. if test -n "$ncolors" && test $ncolors -ge 8; then
  11. bold="$(tput bold)"
  12. underline="$(tput smul)"
  13. standout="$(tput smso)"
  14. normal="$(tput sgr0)"
  15. black="$(tput setaf 0)"
  16. red="$(tput setaf 1)"
  17. green="$(tput setaf 2)"
  18. yellow="$(tput setaf 3)"
  19. blue="$(tput setaf 4)"
  20. magenta="$(tput setaf 5)"
  21. cyan="$(tput setaf 6)"
  22. white="$(tput setaf 7)"
  23. fi
  24. fi
  25. append_msg() {
  26. if test $# -ne 0; then
  27. echo -n ": ${bold}$*"
  28. fi
  29. echo "${normal}"
  30. }
  31. # write a message
  32. message() {
  33. if test $# -eq 0; then
  34. return
  35. fi
  36. echo "${bold}${white}$*${normal}" 1>&2
  37. }
  38. # write a success message
  39. success() {
  40. echo -n "${bold}${green}success" 1>&2
  41. append_msg $* 1>&2
  42. }
  43. # write a notice
  44. notice() {
  45. echo -n "${bold}${yellow}notice" 1>&2
  46. append_msg $* 1>&2
  47. }
  48. # write a warning message
  49. warning() {
  50. echo -en "${bold}${red}warning" 1>&2
  51. append_msg $* 1>&2
  52. }
  53. # write error message
  54. error() {
  55. echo -en "${bold}${red}error" 1>&2
  56. append_msg $* 1>&2
  57. }
  58. # run a command, print the result and abort in case of error
  59. # option: --ignore: ignore the result, continue in case of error
  60. run() {
  61. ignore=1
  62. while test $# -gt 0; do
  63. case "$1" in
  64. (--ignore) ignore=0;;
  65. (*) break;;
  66. esac
  67. shift;
  68. done
  69. echo -n "${bold}${yellow}running:${white} $*${normal} … "
  70. set +e
  71. result=$($* 2>&1)
  72. res=$?
  73. set -e
  74. if test $res -ne 0; then
  75. if test $ignore -eq 1; then
  76. error "failed with return code: $res"
  77. if test -n "$result"; then
  78. echo "$result"
  79. fi
  80. exit 1
  81. else
  82. warning "ignored return code: $res"
  83. fi
  84. else
  85. success
  86. fi
  87. }
  88. ############################################################################ error handler
  89. function traperror() {
  90. set +x
  91. local err=($1) # error status
  92. local line="$2" # LINENO
  93. local linecallfunc="$3"
  94. local command="$4"
  95. local funcstack="$5"
  96. IFS=" "
  97. for e in ${err[@]}; do
  98. if test -n "$e" -a "$e" != "0"; then
  99. error "line $line - command '$command' exited with status: $e (${err[@]})"
  100. if [ "${funcstack}" != "main" -o "$linecallfunc" != "0" ]; then
  101. echo -n " ... error at ${funcstack} " 1>&2
  102. if [ "$linecallfunc" != "" ]; then
  103. echo -n "called at line $linecallfunc" 1>&2
  104. fi
  105. echo
  106. fi
  107. exit $e
  108. fi
  109. done
  110. success
  111. exit 0
  112. }
  113. # catch errors
  114. trap 'traperror "$? ${PIPESTATUS[@]}" $LINENO $BASH_LINENO "$BASH_COMMAND" "${FUNCNAME[@]}" "${FUNCTION}"' ERR SIGINT INT TERM EXIT
  115. ##########################################################################################
  116. #### START HERE ##########################################################################
  117. ##########################################################################################
  118. ######################################################### commandline parameter evaluation
  119. while test $# -gt 0; do
  120. case "$1" in
  121. (--help|-h) less <<EOF
  122. SYNOPSIS
  123. $0 [OPTIONS]
  124. OPTIONS
  125. --help, -h show this help
  126. DESCRIPTION
  127. EOF
  128. exit;;
  129. (*) error "unknow option $1, try $0 --help"; exit 1;;
  130. esac
  131. if test $# -eq 0; then
  132. error "missing parameter, try $0 --help"; exit 1
  133. fi
  134. shift;
  135. done
  136. ##################################################################################### Main