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

Added in place option #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ Sébastien Lavoie <[email protected]>

Johan Haleby

Stefan Rinke

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

To install templater in linux type:

sudo curl -L https://raw.githubusercontent.com/johanhaleby/bash-templater/master/templater.sh -o /usr/local/bin/templater
sudo curl -L https://raw.githubusercontent.com/sker65/bash-templater/master/templater.sh -o /usr/local/bin/templater
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should it really say sker65 if we merge this PR?

sudo chmod +x /usr/local/bin/templater

## Usage
Expand All @@ -38,6 +40,14 @@ AUTHOR=Johan
VERSION=1.2.3
```

Added an option -r to replace all variables in the templace *in place* and save the original template as .bak.

example:
```bash
AUTHOR=Stefan templater template -r
```


Don't print any warning messages:

```bash
Expand Down
11 changes: 10 additions & 1 deletion templater.sh
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ where:
Don't do anything, just print the result of the variable expansion(s)
-f, --file
Specify a file to read variables from
-r, --replace
Replace template file with rendered version
-s, --silent
Don't print warning messages (for example if no variables are found)

Expand Down Expand Up @@ -76,6 +78,9 @@ if [ "$#" -ne 0 ]; then
-p|--print)
print_only="true"
;;
-r|--replace)
replace="true"
;;
-f|--file)
config_file="$2"
;;
Expand Down Expand Up @@ -165,5 +170,9 @@ for var in $vars; do
replaces="-e 's/{{$var}}/${value}/g' $replaces"
done

if [[ "$replace" == "true" ]]; then
SED_OPT="-i .bak"
fi

escaped_template_path=$(echo $template | sed 's/ /\\ /g')
eval sed $replaces "$escaped_template_path"
eval sed $SED_OPT $replaces "$escaped_template_path"