-2

I'm trying to write a bash script that will read pairs of variables from the file and then uses them in a loop. i'm trying to use this

while read p; do $p; echo "$a and $b"; done < text.txt

with the text.txt containing the following:

a="teststring"; b="anothertest"
a="teststring1"; b="anothertest1"
a="teststring2"; b="anothertest2" 

the output looks like that:

bash: a="teststring";: command not found
 and
bash: a="teststring1";: command not found
 and

I have found similar question command line arguments from a file content

But couldn't figure out how to apply the answers to my particular case. Thanks!

  • Possible duplicate of https://stackoverflow.com/questions/29108949/awk-parse-out-key-value-pairs-into-variables – tripleee Jan 31 '18 at 17:03

1 Answers1

0

One solution using the evil eval, just for tests purpose, not to use in production :

while read line; do
    eval "$line"
    echo "$a and $b"
done < file.txt

Output:

teststring and anothertest
teststring1 and anothertest1
teststring2 and anothertest2

Please read

http://wiki.bash-hackers.org/commands/builtin/eval

Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223
  • 1
    Beat me, including warning for `eval`! Personal use is fine, for scripting stuff, but no production. – kabanus Jan 31 '18 at 16:14