1

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.

2 Answers 2

1
source input.ini

Or

. input.ini

More info

<source | .> filename [arguments]
    Execute commands from a file in the current shell.
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your answer but I don't understand what you mean ... and in fact I fixed the problem after searching the web (I've never written this kind of code). See next answer
0

Solved

using :

file="install.ini"
while IFS== read -r f v
do
  eval "$f=$v"   
done <"$file"

did the trick.

3 Comments

Why do it manually when it can be done using a shell command like source as indicated in previous answer?
.. because I don't understand how it works .. I read it's used to execute a file passed as argument but I don't see the relationship with my question ...
executes commands from a file, in your case the variable assignments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.