help with creating a suitable config file

Hi, currently, my shell script hard codes several array variables (etc.

PEOPLE=(
Vincar
Matthew
)
 
FRUITS=(
Banana
Apple
Orange
Pineapple
)

I want to move these information into one config file and then in my shell script, parse the config file into the appropriate variables. Do you have any suggestions on how to do this? Thanks!

If you make your config file a shell script, your loading script could be as simple as source ~/.configfile

Gentoo actually does this for its system config files(/etc/conf.d/...) So does firehol. Very handy if you want to use its settings in your own scripts.

1 Like
set -A

is another option.

1 Like

Thanks, I don't know why this still isn't working though. Btw, I'm using bash.

For example, I have a file script.sh

echo PEOPLE: $PEOPLE
echo FRUITS: $FRUITS 
and a config.configfile
PEOPLE=(
Vincar
Matthew
)
FRUITS=(
Banana
Apple
Orange
Pineapple
)

when I run ./script.sh config.configfile , the output is

PEOPLE:
FRUITS:

the contents do not show...=(
thanks.

If ksh shell scripting make your config file

touch init.cfg

init.cfg:

#formulations properties (to shape without spaces and with separator "="
#
###########################################################################
#global parameters
###########################################################################
#
##### Variable PEOPLE
PEOPLE="Vincar Matthew"
#
##### Variable FRUITS
FRUITS="Banana Apple Orange Pineapple"

and your loading script could be as simple as ./init.cfg

is another option, but the shell she must be /usr/bin/ksh!

Germano

1 Like

I don't see that you're actually loading the config file, it doesn't just happen unless you tell it. You have to do source config.configfile Which in your case might be source "$1" though you might want to check that "$1" exists and is a file and all that before you source it.

---------- Post updated at 11:20 AM ---------- Previous update was at 11:18 AM ----------

I don't think that's correct. You must source the script, not just run it, for the variables to end up in your current context and not a new one.

1 Like

thanks Corona, I got this to work...I didn't know "source" was a function command

IMO source is a bashism. According to the POSIX spec a dot should be used:

. ~/.configfile

Shell Command Language

--
This page seems to be incorrect in stating that source is a "bourne shell builtin and a POSIX `special' builtin"
http://ss64.com/bash/period.html

1 Like

I just tested [edit]seven shells on two OSes. BASH, CSH, ZSH, TCSH, two versions of KSH, and busybox ASH(which is a stripped-down descendant of a fairly old BSD bourne shell). The only one that did not have 'source' was an out-of-date ksh. The newer KSH was fine.

So, while I can see why you'd be sensitive to the use of that keyword, no, it's not a BASH-ism, and would appreciate it if you stopped trying to blame every bad shell habit on it. Nearly every imaginable shell has it except yours.

I considered using . but it's difficult to read, especially for people new to the language. They could easily skip the space and mistake it for part of the path.

Of course it is in the csh and derivatives. In the Bourne shell derivatives it is present in bash. In ksh88 it is not there. In ksh93 it is added as an alias to .
In zsh source is like . but it differs in the sense that the current directory is always scanned first, which is not advisable....
In dash source does not work. Source is not in the POSIX specification.

Certainly not just bash, if you've been listening.

Not all ksh93 has it.

I'd consider that a bug, and suspect that will be fixed soon.

In any case you're ignoring the point: Shells that don't implement a 'source' are in the minority. That your preferred shell is in this minority doesn't change this.

You are ignoring the point that it is not POSIX. I think it is in the interest of the forum that such an observation is made. The only other time I ever used the term "bash-ism" in of all of my posts, was to point out that something was not a bash-ism, so I feel your reaction is bit out of place.
dash does not have a bug, it tries to adhere to the POSIX specification. In zsh it is not a synonym to .

I concede the point that it's not POSIX.

I don't understand why it's a BASH-ism however. This feature's present in modern versions of your own shell of choice, entire historical branches of others, some ancient bourne derivitaves, and things you might've not encountered at all. BASH gets enough bashing already, I think. :stuck_out_tongue:

I put forward that "in my opinion" it is a bash-ism, I am not stating a known fact. The reason I say this is that source was in the csh line of shell, but not part of the orginal Bourne shell, nor was it in ksh. Somehow it was re-introduced and I would suspect this is because of bash.

In zsh source is not the same as . so it may give unexpected results.
dash is a modern shell, BTW. It is /bin/sh on Ubuntu, so scripts will break if one uses source, and there are more Posix shells on e.g. HP-UX or Solaris (/usr/xpg4/bin/sh). One would have to test if it worked there. In the HP-UX man page there is no mention of "source" http://h20000.www2.hp.com/bc/docs/support/SupportManual/c02267598/c02267598.pdf