./evalnprint.sh
./abc.props: line 2: siteA.host=siteAhostname: command not found
./abc.props: line 4: ${protocol}:/${siteA.host}/pagea/blabla?v1=32: bad substitution
./abc.props: line 5: ${protocol}:/${siteA.host}/${pageA}/blabla?v1=32: bad substitution
./abc.props: line 6: test.enc=AFSAF!+==: command not found
props are defined for huge real world app, I can't change existing format.
If its just keyName I would have replaced '.' with '_' and back. values have all kinds of special characters like '='
You could write a load-prop function that replaces all non-alphanumeric characters in the variable names with underscore like this:
#!/bin/bash
function load-prop {
. <(
awk '
FNR==NR {
p=index($0, "=")
if (!p) next
VAR=substr($0,1,p-1)
NEW=VAR
while (match(NEW, "[^_A-Za-z0-9]"))
NEW=substr(NEW, 0, RSTART - 1) "_" substr(NEW, RSTART + 1)
if(NEW != VAR) repl[VAR]=NEW
next
}
{
for(rep in repl)
gsub(rep, repl[rep], $0)
}
1
' $1 $1)
}
load-prop ./myfile.props
echo "SITEAURL2 is $siteAURL2"
echo "test.enc is $test_enc"
Output:
SITEAURL2 is http:/siteAhostname/pageNameA/blabla?v1=32
test.enc is AFSAF!+==
Edit:
Just noticed that test.enc has had the string $fsafasf incorrectly expanded by the shell.
Single quotes could avoid this issue, easy if you can update your prop file(s).
The awk script could adjust for this automatically, but it needs to identify when this is needed
If all variables use the ${} format then $ characters without a following { could be used to trigger the quoting eg:
test_enc='AFSAF!$fsafasf+=='
You may also have issues with spaces within the variable values.
Thanks XL. I do not see single quotes in properties, I will try to wrap values in single quotes first, then use your script to expand variable substitution. Thanks for taking time to look into it.