If you really have multiple files with varName=value
pairs, then all you need to do is source it. So, iterate over all .properties
files and source each of them :
for file in /path/to/MyProperties/*.properties; do
. "$file"
done
You now have all your variables defined in your script. To illustrate:
$ cat foo.properties
foo="bar"
$ echo "$foo" ## no value
$ . ./foo.properties
$ echo "$foo"
bar
This assumes your .properties
files have nothing but variable=value pairs. When sourcing, each line of the file will be executed in the shell running the script doing the sourcing. So, any commands in your .properties
files will also be executed. And any attacker with access to MyProperties
can add a malicious command there. So only do this if you can be certain of the files' contents.
Note that it's important that the path given to the .
builtin contains a /
character (hence the ./foo.properties
above) as otherwise the file is looked up in the directories of $PATH
instead of the current directory (in the case of bash
and when not in POSIX compliance mode, . file
looks for file
in the current working directory if it has not been found in $PATH
).
HOME
orPATH
), and don't need or want to support the full shell syntax, you could arrange to read them to an associative array... – ilkkachu Apr 25 '17 at 12:29