I have a text file like this:
melon = [2 2 4 5];
apple = [3 6 4 4];
lemon = [1 5 4 8];
And I want to make a function that reads a named variable into a bash array. This is what I came up with - that doesn't work since the variable $FruitToParse
doesn't get expanded:
#!/bin/bash
set -e
set -u
function file_to_array {
local FileToParse=${1}
local FruitToParse=${2}
for i in `cat ${FileToParse} | sed -n -e 's/.*${FruitToParse} = \[\(.*\)\];/\1/p'`); do
echo ${i}
done
}
file_to_array fruits.txt apple