Environment Files

Environment Files

am 15.04.2008 15:48:57 von mtek

Hi,

We have an environment file that gets included at the top of every
script we have. At this time, we want to be able to pass a parameter
to that environment file and create a variable based on it.

Is this possible? The environment file is nothing but variable
declarations. Can this be done so I can create a variable like:
$LOG_DIRECTORY/$parameter/$logfile_name

Thanks!

John

Re: Environment Files

am 15.04.2008 17:04:39 von PK

On Tuesday 15 April 2008 15:48, Mtek wrote:

>
> Hi,
>
> We have an environment file that gets included at the top of every
> script we have.

By sourcing it?

> At this time, we want to be able to pass a parameter
> to that environment file and create a variable based on it.
>
> Is this possible? The environment file is nothing but variable
> declarations. Can this be done so I can create a variable like:
> $LOG_DIRECTORY/$parameter/$logfile_name

These above are variables already defined in the file? Please provide an
example input, with expected output or result.

Re: Environment Files

am 15.04.2008 22:46:16 von wayne

Mtek wrote:
> Hi,
>
> We have an environment file that gets included at the top of every
> script we have. At this time, we want to be able to pass a parameter
> to that environment file and create a variable based on it.
>
> Is this possible? The environment file is nothing but variable
> declarations. Can this be done so I can create a variable like:
> $LOG_DIRECTORY/$parameter/$logfile_name
>
> Thanks!
>
> John

In the "environment file" script add:

[ $# != 0 ] && eval "$@"

Then if the parameter is:

. env_file foo=bar

That should set the variable foo to the value bar in the
current environment. (when you said "included" I assume
you meant "sourced".)

But it seems to me this is not the best solution to whatever
your problem is. What is the real problem for which you think
this mechanism is the solution? I bet this group can come up
with suggestions for you to solve your problem a better way.

-Wayne

Re: Environment Files

am 18.04.2008 14:47:08 von Geoff Clare

Wayne wrote:

> In the "environment file" script add:
>
> [ $# != 0 ] && eval "$@"
>
> Then if the parameter is:
>
> . env_file foo=bar
>
> That should set the variable foo to the value bar in the
> current environment. (when you said "included" I assume
> you meant "sourced".)

The OP didn't specify a particular shell, so we should assume a
portable solution is wanted. Not all shells support passing
parameters when sourcing (it's not required by POSIX).

--
Geoff Clare

Re: Environment Files

am 18.04.2008 18:32:40 von OldSchool

On Apr 15, 9:48=A0am, Mtek wrote:
> Hi,
>
> We have an environment file that gets included at the top of every
> script we have. =A0At this time, we want to be able to pass a parameter
> to that environment file and create a variable based on it.
>
> Is this possible? =A0The environment file is nothing but variable
> declarations. =A0Can this be done so I can create a variable like:
> $LOG_DIRECTORY/$parameter/$logfile_name
>
> Thanks!
>
> John

it sounds like he wants the something along the lines of the following

env_file
==================== =====3D=
======

LOG_DIR=3D/somepath
logfile=3Dsomefile

LOGPATH=3D$LOG_DIR/$1/$logfile
export ......
==================== =====3D=
======

then he procedes to do something like:

./env_file test
echo $LOG_PATH
/somepath/test/somefile

./env_file prod
/somepath/prod/somefile