-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenvsubst.sh
More file actions
executable file
·46 lines (44 loc) · 1.62 KB
/
Copy pathenvsubst.sh
File metadata and controls
executable file
·46 lines (44 loc) · 1.62 KB
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
#!/usr/bin/env sh
# envsubst.sh - POSIX-compatible version of envsubst
#
# Feed it text on standard input, and it prints the text to standard output,
# replacing ${FOO} or $FOO in the text with the value of the variable.
#
# This is *very* slow, but it's about as fast as I can get it using just
# POSIX shell stuff (sed/awk would be faster). Use GNU envsubst for speed.
#
# Since this is a shell script, it conflates shell variables with
# environment variables. You can load this script into your shell
# and it will use those variables specific to your shell session:
# cat Sample.txt | . ./envsubst.sh
#
# Or you can call this script as an external executable and it will
# use only exported variables:
# cat Sample.txt | ./envsubst.sh
#
[ "${DEBUG:-0}" = "1" ] && set -x
while IFS= read -r foo ; do
# Loop until the line no longer has any $FOO or ${FOO} variables
while : ; do
# Match on either $FOO or ${FOO}
match="$( expr "$foo" : '.*${\{0,\}\([a-zA-Z0-9_]*\)}\{0,\}' )"
[ -n "$match" ] || break
eval new="\${$match:-}"
prefix="${foo%\$\{$match\}*}"
suffix="${foo#*\$\{$match\}}"
if [ ! "$suffix" = "$foo" ] ; then
# If suffix had a match, replace the line
foo="${prefix}${new}${suffix}"
else
# Otherwise try the replacement without the ${FOO} form
prefix="${foo%\$$match*}"
suffix="${foo#*\$$match}"
if [ ! "$suffix" = "$foo" ] ; then
foo="${prefix}${new}${suffix}"
else
break
fi
fi
done
printf "%s\n" "$foo"
done