Exporting a config file into the environment
Exporting a config file into the environment
am 05.12.2007 17:51:40 von s1037989
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
Re: Exporting a config file into the environment
am 05.12.2007 18:31:32 von Cyrus Kriticos
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.
Re: Exporting a config file into the environment
am 05.12.2007 19:26:06 von s1037989
On Dec 5, 11:31 am, Cyrus Kriticos
wrote:
> Stefan wrote:
> > Say I have a text file:
>
> > $ cat /etc/sysconfig/organization
> > ORGCODE=Focus
> > ORGEMAI...@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
Re: Exporting a config file into the environment
am 05.12.2007 19:57:04 von Stephane CHAZELAS
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
Re: Exporting a config file into the environment
am 05.12.2007 19:58:20 von Stephane CHAZELAS
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@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
Re: Exporting a config file into the environment
am 05.12.2007 20:00:52 von cfajohnson
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
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
Re: Exporting a config file into the environment
am 06.12.2007 06:39:53 von s1037989
On Dec 5, 12:57 pm, Stephane Chazelas
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...@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