Exporting a config file into the environment

Say I have a text file:

$ cat /etc/sysconfig/organization
ORGCODE=Focus
ORGEMAIL=a [at] a.com

And then I load it into the environment:

$ . /etc/sysconfig/organization

How can I export every variable defined in that file (aside from
`export ORGCODE ORGEMAIL')? It's likely that the file will not have
the same set of variables every time and also likely that new ones may
get added over time.

I have a cron bash script which sources the config file and then calls
a Perl program that expects to read those new environment variables
defined by the calling cron bash script.

Thanks for any suggestions!!

Stefan Adams
s1037989 [ Mi, 05 Dezember 2007 17:51 ] [ ID #1885922 ]

Re: Exporting a config file into the environment

Stefan wrote:
> Say I have a text file:
>
> $ cat /etc/sysconfig/organization
> ORGCODE=Focus
> ORGEMAIL=a [at] a.com
>
> And then I load it into the environment:
>
> $ . /etc/sysconfig/organization
>
> How can I export every variable defined in that file (aside from
> `export ORGCODE ORGEMAIL')? It's likely that the file will not have
> the same set of variables every time and also likely that new ones may
> get added over time.
>
> I have a cron bash script which sources the config file and then calls
> a Perl program that expects to read those new environment variables
> defined by the calling cron bash script.

TMPFILE="$(mktemp)"
sed "s/^/export /" /etc/sysconfig/organization > "$TMPFILE"
.. "$TMPFILE"
rm -f "$TMPFILE"

--
Best regards | Be nice to America or they'll bring democracy to
Cyrus | your country.
Cyrus Kriticos [ Mi, 05 Dezember 2007 18:31 ] [ ID #1885926 ]

Re: Exporting a config file into the environment

On Dec 5, 11:31 am, Cyrus Kriticos <cyrus.kriti... [at] googlemail.com>
wrote:
> Stefan wrote:
> > Say I have a text file:
>
> > $ cat /etc/sysconfig/organization
> > ORGCODE=Focus
> > ORGEMAI... [at] a.com
>
> > And then I load it into the environment:
>
> > $ . /etc/sysconfig/organization
>
> > How can I export every variable defined in that file (aside from
> > `export ORGCODE ORGEMAIL')? It's likely that the file will not have
> > the same set of variables every time and also likely that new ones may
> > get added over time.
>
> > I have a cron bash script which sources the config file and then calls
> > a Perl program that expects to read those new environment variables
> > defined by the calling cron bash script.
>
> TMPFILE="$(mktemp)"
> sed "s/^/export /" /etc/sysconfig/organization > "$TMPFILE"
> . "$TMPFILE"
> rm -f "$TMPFILE"
>
> --
> Best regards | Be nice to America or they'll bring democracy to
> Cyrus | your country.- Hide quoted text -
>
> - Show quoted text -

That's a nice suggestion, Cyrus! But, what if the config file has
comments or a blank line? Or what if there is other stuff in that
file that wouldn't fair well with being prepended by "export "? For
example, what if someone were to mod the config file to be dynamic?
In other words, it's almost as if I need a parser...! Or could I be
safe with:

TMPFILE="$(mktemp)"
sed -r -e "/^\s*#/d" -e "s/^/export /" /etc/sysconfig/organization >
"$TMPFILE"
.. "$TMPFILE"
rm -f "$TMPFILE"

Thanks for the suggestion! Definitely a good one!

Stefan
s1037989 [ Mi, 05 Dezember 2007 19:26 ] [ ID #1885927 ]

Re: Exporting a config file into the environment

On Wed, 5 Dec 2007 08:51:40 -0800 (PST), Stefan wrote:
> Say I have a text file:
>
> $ cat /etc/sysconfig/organization
> ORGCODE=Focus
> ORGEMAIL=a [at] a.com
>
> And then I load it into the environment:
>
> $ . /etc/sysconfig/organization
>
> How can I export every variable defined in that file (aside from
> `export ORGCODE ORGEMAIL')? It's likely that the file will not have
> the same set of variables every time and also likely that new ones may
> get added over time.
[...]

set -a
.. /etc/sysconfig/organization
set +a

--
Stephane
Stephane CHAZELAS [ Mi, 05 Dezember 2007 19:57 ] [ ID #1885928 ]

Re: Exporting a config file into the environment

On Wed, 05 Dec 2007 18:31:32 +0100, Cyrus Kriticos wrote:
> Stefan wrote:
>> Say I have a text file:
>>
>> $ cat /etc/sysconfig/organization
>> ORGCODE=Focus
>> ORGEMAIL=a [at] a.com
>>
>> And then I load it into the environment:
>>
>> $ . /etc/sysconfig/organization
>>
>> How can I export every variable defined in that file (aside from
>> `export ORGCODE ORGEMAIL')? It's likely that the file will not have
>> the same set of variables every time and also likely that new ones may
>> get added over time.
>>
>> I have a cron bash script which sources the config file and then calls
>> a Perl program that expects to read those new environment variables
>> defined by the calling cron bash script.
>
> TMPFILE="$(mktemp)"
> sed "s/^/export /" /etc/sysconfig/organization > "$TMPFILE"
> . "$TMPFILE"
> rm -f "$TMPFILE"

You don't need a tempfile:

eval "$(
sed 's/^[^#]/export &/' < /etc/sysconfig/organization
)"

But see my other answer about "set -a".

--
Stephane
Stephane CHAZELAS [ Mi, 05 Dezember 2007 19:58 ] [ ID #1885929 ]

Re: Exporting a config file into the environment

On 2007-12-05, Stefan wrote:
>
> Say I have a text file:
>
> $ cat /etc/sysconfig/organization
> ORGCODE=Focus
> ORGEMAIL=a [at] a.com
>
> And then I load it into the environment:
>
> $ . /etc/sysconfig/organization
>
> How can I export every variable defined in that file (aside from
> `export ORGCODE ORGEMAIL')? It's likely that the file will not have
> the same set of variables every time and also likely that new ones may
> get added over time.

export $( cut -s -d= -f1 /etc/sysconfig/organization )

--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell/>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
cfajohnson [ Mi, 05 Dezember 2007 20:00 ] [ ID #1885930 ]

Re: Exporting a config file into the environment

On Dec 5, 12:57 pm, Stephane Chazelas <stephane_chaze... [at] yahoo.fr>
wrote:
> On Wed, 5 Dec 2007 08:51:40 -0800 (PST), Stefan wrote:
> > Say I have a text file:
>
> > $ cat /etc/sysconfig/organization
> > ORGCODE=Focus
> > ORGEMAI... [at] a.com
>
> > And then I load it into the environment:
>
> > $ . /etc/sysconfig/organization
>
> > How can I export every variable defined in that file (aside from
> > `export ORGCODE ORGEMAIL')? It's likely that the file will not have
> > the same set of variables every time and also likely that new ones may
> > get added over time.
>
> [...]
>
> set -a
> . /etc/sysconfig/organization
> set +a

FANTASTIC!

This is perfect! It's so perfect that it seems the purpose of set -
a / set +a was designed for exactly the purpose of what I proposed!

Thanks, Stephane; and to everyone else that contributed ideas!

Stefan

> --
> Stephane
s1037989 [ Do, 06 Dezember 2007 06:39 ] [ ID #1886755 ]
Linux » comp.unix.shell » Exporting a config file into the environment

Vorheriges Thema: exit codes of background jobs?
Nächstes Thema: Calculating using let and expr