Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple improvements #6

Open
wants to merge 27 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
0411864
Added support for reading variables from a file
johanhaleby Nov 18, 2015
2df7d2a
Updated readme
johanhaleby Nov 18, 2015
c7a1298
Update README.md
johanhaleby Nov 18, 2015
a20838f
Update README.md
johanhaleby Nov 20, 2015
8c7b73e
Update README.md
johanhaleby Nov 20, 2015
c000ac4
Can now use spaces in path to template file
johanhaleby Dec 16, 2015
bed9100
Added support &
johanhaleby Feb 2, 2016
c519c41
Improved escaping of & again
johanhaleby Feb 2, 2016
5ac655d
Fixing bugs
johanhaleby Feb 2, 2016
783ed74
Create LICENSE
johanhaleby Jan 13, 2017
d0dca1b
Added license header
johanhaleby Jan 18, 2017
1599113
Fixed typo
johanhaleby Jan 18, 2017
3a2a402
Add file to read variables from file
Globegitter Jul 11, 2017
7cd682c
Merge pull request #2 from Globegitter/patch-1
johanhaleby Jul 12, 2017
978e078
Instalation instruction added
gustawdaniel Feb 6, 2018
c8d0ab9
Merge pull request #3 from gustawdaniel/patch-1
johanhaleby Feb 7, 2018
2a6f9cb
Change escape position of specific character
hiono Mar 20, 2018
c6eefe9
Merge pull request #4 from hiono/hiono-patch-1
johanhaleby Mar 20, 2018
8cfcc18
Fix spelling errors in installation instructions
matsjonas Jul 1, 2018
8441dfd
Merge pull request #5 from matsjonas/installation-instructions-fix
johanhaleby Jul 2, 2018
b10473a
resolves johanhaleby/bash-templater#10
Dec 25, 2020
fbaf786
Merge remote-tracking branch 'original/master' into master
Dec 25, 2020
c38728b
resolves johanhaleby/bash-templater#12
Dec 25, 2020
1843813
fixed --nounset handling
Dec 25, 2020
6e6803e
fixed no-variable-template handling.
Dec 25, 2020
659cee8
added "--verbose" switch for a clearer output
Dec 25, 2020
da62a99
working directory of config file is now its own dir
Jan 7, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,34 @@ Supports default values by writting `{{VAR=value}}` in the template.

Sébastien Lavoie <github@lavoie.sl>

See http://blog.lavoie.sl/2012/11/simple-templating-system-using-bash.html for other details
Johan Haleby

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

## Installation

`templater.sh` has no external dependencies. You can use it by directly executing.

To install `templater.sh` globally in Linux, type:

sudo curl -L https://raw.githubusercontent.com/johanhaleby/bash-templater/master/templater.sh -o /usr/local/bin/templater.sh
sudo chmod +x /usr/local/bin/templater.sh

## Usage

VAR=value templater.sh template
```

Read variables from file:

```bash
templater.sh template -f variables.txt
```

```bash
# Using external configuration file (and don't print the warnings)
templater.sh template -f variables.txt -s

```sh
# Passing arguments directly
VAR=value templater.sh template

Expand Down
141 changes: 132 additions & 9 deletions templater.sh
Original file line number Diff line number Diff line change
@@ -1,19 +1,136 @@
#!/bin/bash
#
# Very simple templating system that replaces {{VAR}} by the value of $VAR.
# Supports default values by writting {{VAR=value}} in the template.
#
# Copyright (c) 2017 Sébastien Lavoie
# Copyright (c) 2017 Johan Haleby
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
# See: https://github.com/johanhaleby/bash-templater
# Version: https://github.com/johanhaleby/bash-templater/commit/5ac655d554238ac70b08ee4361d699ea9954c941

# Replaces all {{VAR}} by the $VAR value in a template file and outputs it
# Use with -h to output all variables

if [[ ! -f "$1" ]]; then
echo "Usage: VAR=value $0 template" >&2
readonly PROGNAME=$(basename $0)

config_file="<none>"
print_only="false"
silent="false"
nounset="false"
verbose="false"

usage="${PROGNAME} [-h] [-d] [-f] [-s] --

where:
-h, --help
Show this help text
-p, --print
Don't do anything, just print the result of the variable expansion(s)
-f, --file
Specify a file to read variables from
-s, --silent
Don't print warning messages (for example if no variables are found)
-u, --nounset
Unset variables throws error instead of a warning
-v, --verbose
Verbose output

examples:
VAR1=Something VAR2=1.2.3 ${PROGNAME} test.txt
${PROGNAME} test.txt -f my-variables.txt
${PROGNAME} test.txt -f my-variables.txt > new-test.txt"

if [ $# -eq 0 ]; then
echo "$usage"
exit 1
fi


if [ "$#" -ne 0 ]; then
while [ "$#" -gt 0 ]
do
case "$1" in
-h|--help)
echo "$usage"
exit 0
;;
-p|--print)
print_only="true"
;;
-f|--file) shift
config_file="$1"
;;
-s|--silent)
silent="true"
;;
-u|--nounset)
nounset="true"
;;
-v|--verbose)
verbose="true"
;;
-*)
echo "Invalid option '$1'. Use --help to see the valid options" >&2
exit 1
;;
# an option argument, this must be the template
*)
template="$1"
;;
esac
shift
done
fi

if [[ ! -f "$template" ]]; then
echo "You need to specify a template file" >&2
echo "$usage"
exit 1
fi

template="$1"


vars=$(grep -oE '\{\{\s*[A-Za-z0-9_]+\s*\}\}' "$template" | sort | uniq | sed -e 's/^{{//' -e 's/}}$//')

if [[ -z "$vars" ]]; then
echo "Warning: No variable was found in $template, syntax is {{VAR}}" >&2
if [ "$verbose" == "true" ]; then
echo "Warning: No variable was found in ${template}" >&2
fi
cat $template
exit 0
fi

# Load variables from file if needed
if [ "${config_file}" != "<none>" ]; then
if [[ ! -f "${config_file}" ]]; then
echo "The file ${config_file} does not exists" >&2
echo "$usage"
exit 1
fi
_pwd=$PWD
cd "$(dirname "$config_file")"
source "${config_file}"
cd "$_pwd"
fi

var_value() {
var="${1}"
eval echo \$"${var}"
Expand Down Expand Up @@ -48,7 +165,8 @@ replaces=()
# Reads default values defined as {{VAR=value}} and delete those lines
# There are evaluated, so you can do {{PATH=$HOME}} or {{PATH=`pwd`}}
# You can even reference variables defined in the template before
defaults=$(grep -oE '^\{\{[A-Za-z0-9_]+=.+\}\}$' "${template}" | sed -e 's/^{{//' -e 's/}}$//')
defaults=$(grep -oE '^\{\{[A-Za-z0-9_]+=.+\}\}' "${template}" | sed -e 's/^{{//' -e 's/}}$//')
#????defaults=$(grep -oE '^\{\{[A-Za-z0-9_]+=.+\}\}$' "${template}" | sed -e 's/^{{//' -e 's/}}$//')
IFS=$'\n'
for default in $defaults; do
var=$(echo "${default}" | grep -oE "^[A-Za-z0-9_]+")
Expand All @@ -69,7 +187,7 @@ done

vars="$(echo "${vars}" | tr " " "\n" | sort | uniq)"

if [[ "$2" = "-h" ]]; then
if [[ "$print_only" == "true" ]]; then
for var in $vars; do
value="$(var_value "${var}")"
echo_var "${var}" "${value}"
Expand All @@ -79,9 +197,14 @@ fi

# Replace all {{VAR}} by $VAR value
for var in $vars; do
value="$(var_value "${var}")"
value=$(var_value $var | sed -e "s;\&;\\\&;g" -e "s;\ ;\\\ ;g") # '&' and <space> is escaped
if [[ -z "$value" ]]; then
echo "Warning: $var is not defined and no default is set, replacing by empty" >&2
if [[ $nounset == "false" ]]; then
[ $silent == "true" ] || echo "Warning: $var is not defined and no default is set, replacing by empty" >&2
else
echo "ERROR: $var is not defined and no default is set." >&2
exit 1
fi
fi

# Escape slashes
Expand Down