You are not logged in.

#1 2013-09-24 18:37:28

kuraku
Member
From: planet Earth
Registered: 2012-01-03
Posts: 199

[SOLVED] How to create array in bash from some lines in config file

I have config file for small script and it looks like this:

local_directory="$HOME/etc" 

excluded_directory="$HOME/etc1"
excluded_directory2="$HOME/etc2"
excluded_directory3="$HOME/etc3"

I would like to place all "excluded_directoryXX" config options into array that can be parsed to program. This way i have to hardcode it and it is not practical at all.

Can somebody help me out?

Last edited by kuraku (2013-09-24 20:45:42)

Offline

#2 2013-09-24 18:46:28

jasonwryan
Forum & Wiki Admin
From: .nz
Registered: 2009-05-09
Posts: 18,531
Website

Re: [SOLVED] How to create array in bash from some lines in config file

mapfile -t configs </etc; echo "${configs[@]}"

Arch + dwm   •   Mercurial repos  •   Github

Registered Linux User #482438

Offline

#3 2013-09-24 19:31:41

kuraku
Member
From: planet Earth
Registered: 2012-01-03
Posts: 199

Re: [SOLVED] How to create array in bash from some lines in config file

@jasonwryan
Thank you, i was not aware of that builtin. At first i wanted to use this in combination with "grep --after-context=XX" and then i realized that i can use it this way:

mapfile -s XX -t configs </etc; echo "${configs[@]}"

Thank you very much.

Offline

#4 2013-09-24 20:39:51

jasonwryan
Forum & Wiki Admin
From: .nz
Registered: 2009-05-09
Posts: 18,531
Website

Re: [SOLVED] How to create array in bash from some lines in config file

Glad you got what you needed. I wasn't sure what sort of output you were looking for, it helps if you include that to remove ambiguity and/or uncertainty.

Please remember to mark your thread as [Solved] by editing your first post and prepending it to the title.


Arch + dwm   •   Mercurial repos  •   Github

Registered Linux User #482438

Offline

#5 2013-09-24 21:40:49

alphaniner
Member
From: Ancapistan
Registered: 2010-07-12
Posts: 2,617

Re: [SOLVED] How to create array in bash from some lines in config file

o_0

I am completely failing to understand what " </etc" is supposed to achieve. Is it shorthand for " </etc/somefile" or ... ?


But whether the Constitution really be one thing, or another, this much is certain - that it has either authorized such a government as we have had, or has been powerless to prevent it. In either case, it is unfit to exist.
-Lysander Spooner

Offline

#6 2013-09-24 21:45:09

kuraku
Member
From: planet Earth
Registered: 2012-01-03
Posts: 199

Re: [SOLVED] How to create array in bash from some lines in config file

alphaniner wrote:

o_0

I am completely failing to understand what " </etc" is supposed to achieve. Is it shorthand for " </etc/somefile" or ... ?

Yes, it is reading stdin and in this case, it is somefile.

Offline

Board footer

Powered by FluxBB