diff options
-rw-r--r-- | lib/format.lua | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/lib/format.lua b/lib/format.lua index 60fda82..5b3e220 100644 --- a/lib/format.lua +++ b/lib/format.lua @@ -159,3 +159,27 @@ function md5sum_string ( str) f:close() return checksum[1] end + + +-- Takes a str and expands any ${...} constructs with the Lua variable +-- ex: a="foo"; print(expand_bash_syntax_vars("a=${a}) - > "a=foo" + +expand_bash_syntax_vars = function ( str ) + + local deref = function ( f) + local v = _G + for w in string.gfind(f, "[%w_]+") do + v = v[w] + end + return v + end + + for w in string.gmatch (str, "${[^}]*}" ) do + local rvar = string.sub(w,3,-2) + local rval = ( deref(rvar) or "nil" ) + str = string.gsub (str, w, rval) + end + return (str) +end + + |