├── screenshot.png ├── .gitattributes ├── LICENSE ├── example.bash ├── template_script.bash └── README.md /screenshot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lfkdev/bashtemplate/HEAD/screenshot.png -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # linefeed instead of CLRG 2 | *.sh text eol=lf 3 | *.bash text eol=lf 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Leon Felix Klostermann 4 | 5 | Github: github.com/lfkdev/bashtemplate 6 | 7 | Permission is hereby granted, free of charge, to any person obtaining a copy 8 | of this software and associated documentation files (the "Software"), to deal 9 | in the Software without restriction, including without limitation the rights 10 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | copies of the Software, and to permit persons to whom the Software is 12 | furnished to do so, subject to the following conditions: 13 | 14 | The above copyright notice and this permission notice shall be included in all 15 | copies or substantial portions of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | SOFTWARE. 24 | -------------------------------------------------------------------------------- /example.bash: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ######################################################## 4 | # Author: lfkdev # 5 | # Info: example usage script # 6 | # License: MIT (c) # 7 | ######################################################## 8 | 9 | # color pallet 10 | readonly cf="\\033[0m" 11 | readonly red="\\033[0;31m" 12 | readonly green="\\033[0;32m" 13 | readonly yellow="\\033[0;33m" 14 | readonly purple="\\033[0;35m" 15 | 16 | is_debug=false # debugmode 17 | 18 | trap exit_EXIT EXIT 19 | trap exit_CTRL QUIT SIGINT 20 | 21 | main() { 22 | example 23 | } 24 | 25 | example() { 26 | info "Information" 27 | warn "Warning!" 28 | succ "Success!" 29 | debug "Debug" 30 | } 31 | 32 | err() { 33 | local _date 34 | _date=$(showdate) 35 | echo -e "[$_date][${red}ERROR${cf}]: $1" 1>&2 36 | } 37 | 38 | err_die() { 39 | local _date 40 | _date=$(showdate) 41 | echo -e "[$_date][${red}ERROR${cf}]: $1 -> use -h parameter for help." 1>&2 42 | echo -e "[$_date][${red}ERROR${cf}]: Cleaning & Exiting." 43 | if [[ "$2" == "1" ]]; then 44 | showhelp 45 | fi 46 | exit 1 47 | } 48 | 49 | warn() { 50 | local _date 51 | _date=$(showdate) 52 | echo -e "[$_date][${yellow}WARNING${cf}]: $1" 53 | } 54 | 55 | info() { 56 | local _date 57 | _date=$(showdate) 58 | echo -e "[$_date][INFO]: $1 " 59 | } 60 | 61 | succ() { 62 | local _date 63 | _date=$(showdate) 64 | echo -e "[$_date][${green}SUCCESS${cf}]: $1" 65 | } 66 | 67 | showdate() { 68 | local _date 69 | _date=$(date +%d-%H.%M) 70 | printf "%s" "$_date" 71 | } 72 | 73 | debug () { 74 | local _date 75 | _date=$(showdate) 76 | if [[ "$is_debug" == "true" ]]; then 77 | echo -e "[$_date][${purple}DEBUG${cf}]: $1" 78 | fi 79 | } 80 | 81 | exit_EXIT() { 82 | info "Script ended! Cleanup & Exit." 83 | cleanup 84 | exit 1 85 | } 86 | 87 | exit_CTRL() { 88 | err "User pressed CTRL+C!" 89 | exit 1 90 | } 91 | 92 | cleanup() { 93 | info "cleanup.." 94 | } 95 | 96 | showhelp() { 97 | echo " Help:" 98 | echo " Usage: $0 [-d] / [-h]" 99 | echo " Where:" 100 | echo " -d: For optional debug messages." 101 | echo " -h: Shows this help text." 102 | echo "" 103 | echo " Example:" 104 | echo " Info:" 105 | echo "" 106 | } 107 | 108 | while getopts ":hd" o; do 109 | case "${o}" in 110 | h) 111 | showhelp 112 | exit 1 113 | ;; 114 | d) 115 | is_debug=true 116 | ;; 117 | *) 118 | err "No valid option choosed." 119 | ;; 120 | esac 121 | done 122 | 123 | main 124 | -------------------------------------------------------------------------------- /template_script.bash: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ######################################################## 4 | # Author: # 5 | # Info: # 6 | # License: # 7 | ######################################################## 8 | 9 | # color pallet 10 | readonly cf="\\033[0m" 11 | readonly red="\\033[0;31m" 12 | readonly green="\\033[0;32m" 13 | readonly yellow="\\033[0;33m" 14 | readonly purple="\\033[0;35m" 15 | 16 | is_debug=false # debugmode 17 | 18 | #catch getting closed and interrupted by ctrl+c 19 | trap exit_EXIT EXIT 20 | trap exit_CTRL QUIT SIGINT 21 | 22 | main() { 23 | #function1 24 | #function2 25 | } 26 | 27 | # The err() function redirects output to STDERR 28 | err() { 29 | local _date 30 | _date=$(showdate) 31 | echo -e "[$_date][${red}ERROR${cf}]: $1" 1>&2 32 | } 33 | 34 | # The err_die() function redirects the message to STDERR, 35 | # starts the cleanup function and then exits. 36 | # You can call err_die() with "1" as argument to show the help before exiting. 37 | err_die() { 38 | local _date 39 | _date=$(showdate) 40 | echo -e "[$_date][${red}ERROR${cf}]: $1 -> use -h parameter for help." 1>&2 41 | echo -e "[$_date][${red}ERROR${cf}]: Cleaning & Exiting." 42 | if [[ "$2" == "1" ]]; then 43 | showhelp 44 | fi 45 | exit 1 #or use $2 instead of 1 to work with exit arguments 46 | } 47 | 48 | # The following function warn, info, succ and debug are for 49 | # output information, use warn for warnings, succ for success etc. 50 | # You can change the colors at the top. 51 | warn() { 52 | local _date 53 | _date=$(showdate) 54 | echo -e "[$_date][${yellow}WARNING${cf}]: $1" 55 | } 56 | 57 | info() { 58 | local _date 59 | _date=$(showdate) 60 | echo -e "[$_date][INFO]: $1 " 61 | } 62 | 63 | succ() { 64 | local _date 65 | _date=$(showdate) 66 | echo -e "[$_date][${green}SUCCESS${cf}]: $1" 67 | } 68 | 69 | showdate() { 70 | local _date 71 | _date=$(date +%d-%H.%M) 72 | printf "$_date" 73 | } 74 | 75 | # The debug() funktion will only show up if boolean 'is_debug' is true 76 | debug () { 77 | local _date 78 | _date=$(showdate) 79 | if [[ "$is_debug" == "true" ]]; then 80 | echo -e "[$_date][${purple}DEBUG${cf}]: $1" 81 | fi 82 | } 83 | 84 | exit_EXIT() { 85 | info "Script ended! Cleanup & Exit." 86 | cleanup 87 | exit 1 88 | } 89 | 90 | exit_CTRL() { 91 | err "User pressed CTRL+C!" 92 | exit 1 93 | } 94 | 95 | cleanup() { 96 | info "cleanup.." 97 | # cleanup tmp files, kill process etc. 98 | } 99 | 100 | showhelp() { 101 | echo " Help:" 102 | echo " Usage: $0 [-d] / [-h]" 103 | echo " Where:" 104 | echo " -d: For optional debug messages." 105 | echo " -h: Shows this help text." 106 | echo "" 107 | echo " Example:" 108 | echo " Info:" 109 | echo "" 110 | } 111 | 112 | # Add all the parameter you whish, -h will show help and -d will 113 | # trigger debug messages to show up 114 | while getopts ":hd" o; do 115 | case "${o}" in 116 | h) 117 | showhelp 118 | exit 1 119 | ;; 120 | d) 121 | is_debug=true 122 | ;; 123 | *) 124 | err "No valid option choosed." 125 | ;; 126 | esac 127 | done 128 | 129 | main 130 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Bash template 2 | Use this bash-template for your next script! This will give you cleaner output, better error handling and more features. 3 | This script is based on the rules of 4 | [Googles Styleguide](https://google.github.io/styleguide/shell.xml) 5 | and 6 | [bahamas10's Styguide](https://github.com/bahamas10/bash-style-guide) 7 | 8 | Output example: 9 |

10 | 11 |

12 | 13 | There are the following output classes: info, warn, success, debug and error. 14 | The classes are structured as follows (info function as example): 15 | ```bash 16 | info() { 17 | local _date 18 | _date=$(date +%d-%H.%M) 19 | echo -e "[$_date][INFO]: $1 " 20 | } 21 | ``` 22 | 23 | So you can easily filter the output your script produces. 24 | Example usage: 25 | ```bash 26 | info " I am some random info." 27 | warn "This is not normal!" 28 | succ "Finished without errors!" 29 | debug "I'm some info just for the devs!" 30 | err "Error, something is wrong!" 31 | err_die "Error, exiting now!" 32 | ``` 33 | 34 | In addition, some functions have more features than just clean output. 35 | ### Debug function 36 | The debug() function will only show up if boolean 'is_debug' is true. This is made by the -d parameter. So devs can just execute the script with -d to view all debug messages. 37 | ```bash 38 | $ bash /usr/local/bin/script.sh -d 39 | ``` 40 | ### Err-Die function 41 | The err_die() function redirects the message to STDERR, 42 | starts the cleanup function and then exits. 43 | You can call err_die() with "1" as argument to show the help before exiting. 44 | ```bash 45 | err_die "Please use parameter." 1 46 | ``` 47 | You can call normal errors without exiting the script by just 48 | ```bash 49 | err "This is an error but I can continue anyway." 50 | ``` 51 | 52 | ### Traps 53 | The following traps are builtin: 54 | ```bash 55 | trap exit_EXIT EXIT 56 | trap exit_CTRL QUIT SIGINT 57 | ``` 58 | The trap function ensures that the script recognizes if it is aborted by the user (ctrl+c), or if it crashes due to other reasons. 59 | As soon as it detects an abort, the cleanup function is started, thus ensuring that the cleanup function also runs in case of unplanned aborts. 60 | 61 | This function is ALWAYS started when the script ends. Therefore the cleanup function is not included in the main function. 62 | ```bash 63 | exit_EXIT() { 64 | info "Script ended! Cleanup & Exit." 65 | cleanup 66 | exit 1 67 | } 68 | ``` 69 | 70 | This function is called when the user presses ctrl+c. 71 | ```bash 72 | exit_CTRL() { 73 | err "User pressed CTRL+C!" 74 | exit 1 75 | } 76 | ``` 77 | 78 | ## Changing Colors 79 | Just change the color pallet values at the top of the script as you whish. 80 | ```bash 81 | readonly cf="\\033[0m" 82 | readonly red="\\033[0;31m" 83 | readonly green="\\033[0;32m" 84 | readonly yellow="\\033[0;33m" 85 | readonly purple="\\033[0;35m" 86 | ``` 87 | 88 | ## Quick Start: 89 | Just download the script 90 | ```bash 91 | wget -O mynewscript.sh https://raw.githubusercontent.com/lfkdev/bashtemplate/master/template_script.bash 92 | chmod +x mynewscript.sh 93 | ``` 94 | 95 | Fill your information into the metabox 96 | ```bash 97 | #################################################### 98 | # Author: # 99 | # Info: # 100 | # License: # 101 | #################################################### 102 | ``` 103 | 104 | Now start creating your code with the output functions and write every function in the main one! Have fun 105 | 106 | If you have any problems or wishes let me now. 107 | --------------------------------------------------------------------------------