Here's an implementation of envsubst (part of GNU gettext). Ignore the email 
from earlier - sorry! This one is better formatted.

envsubst, is frequently used in minimal container images for CI/CD purposes.
However, busybox lacks this functionality despite implementing other GNU 
gettext tools, so people often have to include the script in their images, or 
use a base image that includes a envsubst binary.
I made this implementation in awk because it's fast and already part of 
busybox. I doubt a C implementation would be worth it.

Signed-off-by: Simon Ungar Felding <[email protected]>
---
 applets_sh/envsubst | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
 create mode 100755 applets_sh/envsubst

diff --git a/applets_sh/envsubst b/applets_sh/envsubst
new file mode 100755
index 000000000..ecd53fb56
--- /dev/null
+++ b/applets_sh/envsubst
@@ -0,0 +1,19 @@
+#! /usr/bin/awk -E
+BEGIN {
+    if (ARGC > 1) {
+        if (match(ARGV[1], /^-h$|^--help$|^-v$/)) {help = 1}
+        print "Busybox implementation of envsubst.\n\nUsage: envsubst < 
[INPUT-FILE] > [OUTPUT-FILE]\n\nOutputs stdin, with $variable or ${variable} 
substituted for the corresponding environment variable."
+        if (help) {exit 0} else {exit 1}
+    }
+}
+{
+    while (match($0, /\$\w+|\${\w+}/)) {
+        start = RSTART
+        len = RLENGTH
+        var = substr($0, start + 1, len - 1)
+        gsub(/[{}]/, "", var)
+        value = ENVIRON[var]
+        $0 = substr($0, 1, start - 1) value substr($0, start + len)
+    }
+    print
+}
--
2.43.0
_______________________________________________
busybox mailing list
[email protected]
https://lists.busybox.net/mailman/listinfo/busybox

Reply via email to