Bash Parse Arrays From Config File

前端 未结 4 1857
孤独总比滥情好
孤独总比滥情好 2021-02-06 12:28

I need to have an array for each \"section\" in the file containing:

[array0]
value1=asdf
value2=jkl

[array1]
value1=1234
value2=5678

I want t

4条回答
  •  悲&欢浪女
    2021-02-06 13:09

    I am about to go out, but I think you can do something like this (untested) and maybe someone clever , like @anubhava, will pick it up and finish it off...

    eval $(gawk -F= '/^\[/{name=gensub(/\[|\]/,"","g");x=0} /=/{print "name[",x++,"]=",$2," "}' config)
    

    Basically, when it sees a line starting with "[" it picks up the array name in the variable name and strips off the square brackets with gensub(). Then, when it sees a line with "=" in it, it outputs the array name and an increasing index "x" for eval to pick up.

    Gotta dash - look at the examples for stat -s here.

提交回复
热议问题