├── LICENSE ├── README.md ├── examples └── vhost-php.conf └── templater.sh /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Sébastien Lavoie 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # BASH Templater 2 | Very simple templating system that replace {{VAR}} by $VAR environment value 3 | Supports default values by writting {{VAR=value}} in the template 4 | 5 | ## Author 6 | 7 | Sébastien Lavoie 8 | 9 | Johan Haleby 10 | 11 | See http://code.haleby.se/2015/11/20/simple-templating-engine-in-bash/ and http://blog.lavoie.sl/2012/11/simple-templating-system-using-bash.html for more details 12 | 13 | ## Installation 14 | 15 | To install templater in linux type: 16 | 17 | sudo curl -L https://raw.githubusercontent.com/johanhaleby/bash-templater/master/templater.sh -o /usr/local/bin/templater 18 | sudo chmod +x /usr/local/bin/templater 19 | 20 | ## Usage 21 | 22 | ```bash 23 | VAR=value templater template 24 | ``` 25 | 26 | Read variables from file: 27 | 28 | ```bash 29 | templater template -f variables.txt 30 | ``` 31 | 32 | e.g.: 33 | ```bash 34 | # variables.txt 35 | # The author 36 | AUTHOR=Johan 37 | # The version 38 | VERSION=1.2.3 39 | ``` 40 | 41 | Don't print any warning messages: 42 | 43 | ```bash 44 | templater template -f variables.txt -s 45 | ``` 46 | 47 | ## Examples 48 | See examples/ 49 | -------------------------------------------------------------------------------- /examples/vhost-php.conf: -------------------------------------------------------------------------------- 1 | {{LOG_DIR=/var/log/apache2}} 2 | {{RUN_DIR=/var/run/php-fpm}} 3 | {{FCGI=$RUN_DIR/$DOMAIN.fcgi}} 4 | {{SOCKET=$RUN_DIR/$DOMAIN.sock}} 5 | {{EMAIL=$USER@$DOMAIN}} 6 | {{DOC_ROOT=/home/$USER/sites/$DOMAIN/htdocs}} 7 | 8 | ServerAdmin {{EMAIL}} 9 | ServerName {{DOMAIN}} 10 | ServerAlias www.{{DOMAIN}} 11 | 12 | DocumentRoot "{{DOC_ROOT}}" 13 | 14 | 15 | AllowOverride All 16 | Order allow,deny 17 | Allow From All 18 | 19 | 20 | AddHandler php-script .php 21 | Action php-script /php5.fastcgi virtual 22 | Alias /php5.fastcgi {{FCGI}} 23 | FastCGIExternalServer {{FCGI}} -socket {{SOCKET}} 24 | 25 | LogLevel warn 26 | CustomLog {{LOG_DIR}}/{{DOMAIN}}.access.log combined 27 | ErrorLog {{LOG_DIR}}/{{DOMAIN}}.error.log 28 | 29 | -------------------------------------------------------------------------------- /templater.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # 3 | # Very simple templating system that replaces {{VAR}} by the value of $VAR. 4 | # Supports default values by writting {{VAR=value}} in the template. 5 | # 6 | # Copyright (c) 2017 Sébastien Lavoie 7 | # Copyright (c) 2017 Johan Haleby 8 | # 9 | # Permission is hereby granted, free of charge, to any person obtaining a copy 10 | # of this software and associated documentation files (the "Software"), to deal 11 | # in the Software without restriction, including without limitation the rights 12 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 | # copies of the Software, and to permit persons to whom the Software is 14 | # furnished to do so, subject to the following conditions: 15 | # 16 | # The above copyright notice and this permission notice shall be included in all 17 | # copies or substantial portions of the Software. 18 | # 19 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 25 | # SOFTWARE. 26 | # 27 | # See: https://github.com/johanhaleby/bash-templater 28 | # Version: https://github.com/johanhaleby/bash-templater/commit/5ac655d554238ac70b08ee4361d699ea9954c941 29 | 30 | # Replaces all {{VAR}} by the $VAR value in a template file and outputs it 31 | 32 | readonly PROGNAME=$(basename $0) 33 | 34 | config_file="" 35 | print_only="false" 36 | silent="false" 37 | 38 | usage="${PROGNAME} [-h] [-d] [-f] [-s] -- 39 | 40 | where: 41 | -h, --help 42 | Show this help text 43 | -p, --print 44 | Don't do anything, just print the result of the variable expansion(s) 45 | -f, --file 46 | Specify a file to read variables from 47 | -s, --silent 48 | Don't print warning messages (for example if no variables are found) 49 | 50 | examples: 51 | VAR1=Something VAR2=1.2.3 ${PROGNAME} test.txt 52 | ${PROGNAME} test.txt -f my-variables.txt 53 | ${PROGNAME} test.txt -f my-variables.txt > new-test.txt" 54 | 55 | if [ $# -eq 0 ]; then 56 | echo "$usage" 57 | exit 1 58 | fi 59 | 60 | if [[ ! -f "${1}" ]]; then 61 | echo "You need to specify a template file" >&2 62 | echo "$usage" 63 | exit 1 64 | fi 65 | 66 | template="${1}" 67 | 68 | if [ "$#" -ne 0 ]; then 69 | while [ "$#" -gt 0 ] 70 | do 71 | case "$1" in 72 | -h|--help) 73 | echo "$usage" 74 | exit 0 75 | ;; 76 | -p|--print) 77 | print_only="true" 78 | ;; 79 | -f|--file) 80 | config_file="$2" 81 | ;; 82 | -s|--silent) 83 | silent="true" 84 | ;; 85 | --) 86 | break 87 | ;; 88 | -*) 89 | echo "Invalid option '$1'. Use --help to see the valid options" >&2 90 | exit 1 91 | ;; 92 | # an option argument, continue 93 | *) ;; 94 | esac 95 | shift 96 | done 97 | fi 98 | 99 | vars=$(grep -oE '\{\{[A-Za-z0-9_]+\}\}' "${template}" | sort | uniq | sed -e 's/^{{//' -e 's/}}$//') 100 | 101 | if [[ -z "$vars" ]]; then 102 | if [ "$silent" == "false" ]; then 103 | echo "Warning: No variable was found in ${template}, syntax is {{VAR}}" >&2 104 | fi 105 | fi 106 | 107 | # Load variables from file if needed 108 | if [ "${config_file}" != "" ]; then 109 | if [[ ! -f "${config_file}" ]]; then 110 | echo "The file ${config_file} does not exists" >&2 111 | echo "$usage" 112 | exit 1 113 | fi 114 | 115 | source "${config_file}" 116 | fi 117 | 118 | var_value() { 119 | eval echo \$$1 120 | } 121 | 122 | replaces="" 123 | 124 | # Reads default values defined as {{VAR=value}} and delete those lines 125 | # There are evaluated, so you can do {{PATH=$HOME}} or {{PATH=`pwd`}} 126 | # You can even reference variables defined in the template before 127 | defaults=$(grep -oE '^\{\{[A-Za-z0-9_]+=.+\}\}' "${template}" | sed -e 's/^{{//' -e 's/}}$//') 128 | 129 | for default in $defaults; do 130 | var=$(echo "$default" | grep -oE "^[A-Za-z0-9_]+") 131 | current=`var_value $var` 132 | 133 | # Replace only if var is not set 134 | if [[ -z "$current" ]]; then 135 | eval $default 136 | fi 137 | 138 | # remove define line 139 | replaces="-e '/^{{$var=/d' $replaces" 140 | vars="$vars 141 | $current" 142 | done 143 | 144 | vars=$(echo $vars | sort | uniq) 145 | 146 | if [[ "$print_only" == "true" ]]; then 147 | for var in $vars; do 148 | value=`var_value $var` 149 | echo "$var = $value" 150 | done 151 | exit 0 152 | fi 153 | 154 | # Replace all {{VAR}} by $VAR value 155 | for var in $vars; do 156 | value=$(var_value $var | sed -e "s;\&;\\\&;g" -e "s;\ ;\\\ ;g") # '&' and is escaped 157 | if [[ -z "$value" ]]; then 158 | if [ $silent == "false" ]; then 159 | echo "Warning: $var is not defined and no default is set, replacing by empty" >&2 160 | fi 161 | fi 162 | 163 | # Escape slashes 164 | value=$(echo "$value" | sed 's/\//\\\//g'); 165 | replaces="-e 's/{{$var}}/${value}/g' $replaces" 166 | done 167 | 168 | escaped_template_path=$(echo $template | sed 's/ /\\ /g') 169 | eval sed $replaces "$escaped_template_path" 170 | --------------------------------------------------------------------------------