Unix Shell - Exporting a config file into the environment

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > December 2007 > Exporting a config file into the environment





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Exporting a config file into the environment
Stefan

2007-12-05, 1:24 pm

Say I have a text file:

$ cat /etc/sysconfig/organization
ORGCODE=Focus
ORGEMAIL=a@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
Cyrus Kriticos

2007-12-05, 1:24 pm

Stefan wrote:
> Say I have a text file:
>
> $ cat /etc/sysconfig/organization
> ORGCODE=Focus
> ORGEMAIL=a@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.
Stefan

2007-12-05, 1:24 pm

On Dec 5, 11:31 am, Cyrus Kriticos <cyrus.kriti...@googlemail.com>
wrote:
> Stefan wrote:
>
>
>
>
>
>
> 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
Stephane Chazelas

2007-12-05, 1:24 pm

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@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

2007-12-05, 1:24 pm

On Wed, 05 Dec 2007 18:31:32 +0100, Cyrus Kriticos wrote:
> Stefan wrote:
>
> 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
Chris F.A. Johnson

2007-12-05, 1:24 pm

On 2007-12-05, Stefan wrote:
>
> Say I have a text file:
>
> $ cat /etc/sysconfig/organization
> ORGCODE=Focus
> ORGEMAIL=a@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
Stefan

2007-12-06, 1:36 am

On Dec 5, 12:57 pm, Stephane Chazelas <stephane_chaze...@yahoo.fr>
wrote:
> On Wed, 5 Dec 2007 08:51:40 -0800 (PST), Stefan wrote:
>
>
>
>
>
> [...]
>
> 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


Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2009 webservertalk.com