问题
How to set shell variables from an input file ?
hello,
I need to set dynamic variable from an .ini file in a shell script.
Assume the input file is input.ini :
var1=val1
var2=val2
var3=val3
In a script I want to set var1, var & var3 respectively to their val1, val2 & val3 to get
echo $var1
val1
echo $var2
val2
...
I've tryed :
file="input.ini"
while IFS== read -r f1 f2
do
eval dynvar=$f1
dynvar=$f2
done <"$file"
echo $var1
echo $var2
echo $var3
the echo $varx commands give no output. How can I work it out ?
thanks in advance.
回答1:
source input.ini
Or
. input.ini
More info
<source | .> filename [arguments] Execute commands from a file in the current shell.
回答2:
Solved
using :
file="install.ini"
while IFS== read -r f v
do
eval "$f=$v"
done <"$file"
did the trick.
来源:https://stackoverflow.com/questions/23727680/unix-script-setting-dynamic-variables-indirect-variable-reference