LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   to export environment variables in a file (https://www.linuxquestions.org/questions/linux-newbie-8/to-export-environment-variables-in-a-file-840716/)

vinayakshinde1987 10-27-2010 05:11 AM

to export environment variables in a file
 
hi,
I want to export the env variables in a file using a script,i tried using the below:
for var in 'env'
do
var2=env|awk -F '=' '{print$1}'
echo "$var;export $var2">file.txt
done
but i m nt getting the desird o/p,i need every variable to be appended with ;export "variable name" in the target file wil sm1 hlp pls

prayag_pjs 10-27-2010 05:20 AM

Hi,

What you have written is very confusing.Please elaborate with proper example.

Refer this link for exporting environmental variables http://tldp.org/LDP/abs/html/othertypesv.html

alli_yas 10-27-2010 06:02 AM

Hi

Prayag is right - explain in what context you are trying to export your environment variables - what is it that you're trying to achieve in the bigger picture?

Also:

Quote:

wil sm1 hlp pls
Please read the LQ Rules. Writing in "text" short form of words is not allowed - so please refrain from doing this.

vinayakshinde1987 10-28-2010 01:24 AM

Quote:

Originally Posted by vinayakshinde1987 (Post 4140738)
hi,
I want to export the env variables in a file using a script,i tried using the below:
for var in 'env'
do
var2=env|awk -F '=' '{print$1}'
echo "$var;export $var2">file.txt
done
but i m nt getting the desird o/p,i need every variable to be appended with ;export "variable name" in the target file wil sm1 hlp pls

Hi,

I wish to take the o/p of env command(which will give the list of environment variables) in a file say env_file.txt.for example, say env_file has the below content:

ORACLE_HOME=/data/oracle/app/product/ora10g
PWD=/home/amit
.
.
.
Now , i wish to create another file myappfile through a script which will have the contents like below:
ORACLE_HOME=/data/oracle/app/product/ora10g;export ORACLE_HOME
PWD=/home/amit;export PWD

'myappfile' is required for my application.

prayag_pjs 10-28-2010 01:30 AM

Hi,

Follow this example on your command line you will get the answer yourself:

Code:

echo $MYNAME
Code:

export MYNAME=vinayak
Code:

echo $MYNAME

alli_yas 10-28-2010 01:31 AM

Hi

From the terminal try:

Code:

# env >> myappfile.txt
This should create myappfile.txt with the environment variables. Test this and if it gives you what you require then you can easily put into a script.

vinayakshinde1987 10-28-2010 02:02 AM

I have one application which requires the 'myappfile' as it is below:

ORACLE_HOME=/data/oracle/app/product/ora10g;export ORACLE_HOME
PWD=/home/amit;export PWD


Currently what we do is we manually create the myappfile by adding ";export variablename " for each environment variable.

I need a script which will append the ";export variablename " at the end of each variable and the endresult would be :

ORACLE_HOME=/data/oracle/app/product/ora10g;export ORACLE_HOME
PWD=/home/amit;export PWD

alli_yas 10-28-2010 02:09 AM

Read this post around how to append to a file with the sed command.

Thus your process to do this would be to write the env variables to your output file and then use sed to append "; export XXXXX"

Note you need to read up and understand sed before you can achieve this.

colucix 10-28-2010 02:21 AM

Code:

awk -F= '{ print $0 "; export", $1 }' input_file > output_file
I don't think it is a good idea, anyway. The reason is that the env command prints out all the current environment, including dynamic variables like PWD, OLDPWD and _. Why not create your own environment, instead? You can limit the environment to what actually is necessary to your script. Just my :twocents:

vinayakshinde1987 10-28-2010 04:10 AM

Thanx colucix,It worked..for dynamic variables we regenerate the required file.


All times are GMT -5. The time now is 06:26 AM.