1

I have the file "Testfile", whit a register in every line, each register having some fields:

foo1=32=test01=30/03/2012 10:03 p.m
foo2=54=test02=30/03/2012 10:05 p.m
foo3=912=test03=30/03/2012 10:08 p.m

("=" is the field delimiter)

I read the first field for every register and store them in an array, and the same for the second field of every register:

NAMES=(`cut -f1 -d'=' Testfile`)
VALUES=(`cut -f2 -d'=' Testfile`)

That gives me two arrays like these: (foo1 foo2 foo3) and (32 54 912)

Is it possible to dynamically create variables named foo1 foo2 and foo3, and assign them their respective values?

I could do:

foo1 = ${VALUES[0]}
foo2 = ${VALUES[1]}
foo3 = ${VALUES[2]}

But I want to get the names of the variables dynamically, regarding the names that I've got from the file.

2
  • 1
    Give an example of exactly what is in 'Testfile'. As question is now, answers are likely to be right but cannot be verified. Commented Oct 4, 2012 at 5:52
  • sorry, you're right, I've just edited to add an example Commented Oct 5, 2012 at 3:55

3 Answers 3

4
cat Testfile 
foo1=35
foo2=54
foo3=912

for v in $(cat Testfile); do declare "$v"; done

echo $foo1 
35

echo $foo2 
54

that will work if lines in file looks exact like this:

var=val

and here is a version that works with spaces:

cat Testfile
foo1 = 35
foo2 = 54
foo3 = 912

while read line; do
    if [ -z "$line" ]; then continue; fi
    var=$(echo $line | cut -d'=' -f1)
    val=$(echo $line | cut -d'=' -f2)
    declare "$(echo $var)=$(echo $val)"
done < Testfile

echo $foo1 
35
Sign up to request clarification or add additional context in comments.

4 Comments

The loop that handles spaces won't quite work, as the spaces will cause word-splitting. while read s; do ...; done < Testfile should work better.
In the case with only two fields it works fine, but if I have more (like in the example I've added) it has some issues (don't know if I'm right, but it seems that its trying to declare every field as a variable, and since some fields doesn't fit the correct syntax for variable naming, it throws some errors)
well, updated to support multiple = syntax. also, it is echoing var and val to get rid of eventual spaces.
It works now! and its a very clear way to do it also. Thanks slivu
3

If the values have no whitespace (as in the example) or are wrapped in quotes, this is as simple as "sourcing" the file.

source Testfile

This will basically "run" the file, which in this case simply defines some variables and returns.

1 Comment

Thanks. It works if I just declare and assign variables in the file, but not if I have many consecutive fields per line.
1

This might work for you (GNU sed):

NAMES_VALUES=($(sed 's/\(\S*\)\s*\(\S*\)\s*.*/\1=\2/' Testfile))
eval "${NAMES_VALUES[@]}"

EDIT:

If the file format uses = as delimiters:

NAMES_VALUES=($(sed 's/^\([^=]*=[^=]*\)=.*/\1/' Testfile))
eval "${NAMES_VALUES[@]}"

1 Comment

Thanks, this works fine, but whith more than two fields, it takes everything after the first "=" as the value. How could I discard the fields after the second "=", to get only the second field as the value of the variable?

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.