0

I have a file like this:

(blank line)
[Test1]
a=1
b=2
c=3

I have to read value of a,b,c in variables, so I used source command and it did the job. But for the line 2 i.e. [Test1], it show command not found.

How can I take [Test1] in a variable too?

Basically I am expecting:

$some_var=[Test1]
$a=1
$b=2
$c=3

So I can use these values further.

Currently what I am doing is removing first two lines from the file and then source file, and by doing this I am not getting line 2 in a variable. I am looking for a better way. I am doing it all inside a bash script.

5
  • What sort of format is your source file? Is that a standard format of some sort, perhaps an INI file? Is whitespace allowed in the source file variable values? How about between the = and the value? Are quotes allowed in the source file?
    – Wildcard
    Dec 9, 2016 at 7:51
  • File looks like same as given in question. Yes it is an ini file. No quotes are not allowed.
    – serenesat
    Dec 9, 2016 at 8:59
  • Do you only want the variables specified under a particular heading in the INI file, or do you want all variables? (In most INI files I have seen, each separate heading may reuse the same variable name with a different associated value. Would you want to just let the final definition clobber any previous values?)
    – Wildcard
    Dec 9, 2016 at 9:01
  • In this there is only one heading.. and first line is blank. I want that heading in one variable and variables specified under that heading.
    – serenesat
    Dec 9, 2016 at 9:06
  • Alternatively, you can use a ini file parser to parse your file. Even though it may be a bit overkill, it should do the job
    – Ikaros
    Dec 9, 2016 at 14:42

2 Answers 2

1

Assuming you have process substitution, you can do something like this:

source <(sed 's/^\[/some_var=\[/' file)
0

If the files are not big (since bash 4.4):

readarray -t arr <infile         # the value in zero is a blank line.
some_var="${arr[1]}"             # use the first line.
. <(printf '%s\n' "${arr[@]:2}") # source the rest.
echo "some_var=$some_var a=$a a=$b a=$c"

When executed:

$ ./script
some_var=[Test1] a=1 a=2 a=3

For older bash, the solution becomes much more convoluted, but possible.
This is the equivalent since bash 2.04 (2000-03-21).

unset arr; i=0
while IFS='' read -r a ; do arr[i]="$a"; ((i++));done <infile
some_var="${arr[1]}"                    # use the first line.
. <(printf '%s\n' "${arr[@]:2}")        # source the rest.
4
  • readarray: command not found, this is what I am getting when I execute this in my bash script.
    – serenesat
    Dec 9, 2016 at 15:42
  • @serenesat Added solution for (very) old bash versions, since 2.04. Please try again.
    – user232326
    Dec 9, 2016 at 16:28
  • When I echo $some_var, I get the desire output but echoing $a, $b or $c, it is not showing anything. How to achieve that?
    – serenesat
    Dec 10, 2016 at 7:03
  • @serenesat The code as written works for me here. I added some detail on the first script (copy to the second) and try. If it still fails, please provide a run with bash -x ./script to debug.
    – user232326
    Dec 10, 2016 at 18:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .