UNIX Script - setting dynamic variables (indirect variable reference)

[亡魂溺海] 提交于 2019-12-31 04:06:13

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!