-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbut.sh
executable file
·56 lines (47 loc) · 985 Bytes
/
but.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/bin/bash
# Based on http://serverfault.com/questions/287688/templating-with-linux-in-a-shell-script
PROG=${0##*/}
usage()
{
echo "${PROG} <template-file> [-o|--output <output-file>] [[[-c|--config] <config-file>] ...]"
}
expand()
{
local template="$(<$1)"
eval "echo \"${template}\""
}
[[ $# -eq 0 ]] && usage && exit 1;
BUT_template_file=$1
BUT_output_file=""
BUT_config_files=""
shift
while [[ $# -gt 0 ]]; do
case "$1" in
-o=*|--output=*)
BUT_output_file="${1#*=}"
;;
-o|--output)
BUT_output_file="$2"
shift
;;
-c=*|--config=*)
BUT_config_files+=" ${1#*=}"
;;
-c|--config)
BUT_config_files+=" $2"
shift
;;
*)
BUT_config_files+=" $1"
;;
esac
shift
done
for BUT_conf_file in $BUT_config_files; do
source $BUT_conf_file
done
if [[ -n "$BUT_output_file" ]]; then
expand $BUT_template_file > $BUT_output_file
else
expand $BUT_template_file
fi