LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-27-2010, 06:11 AM   #1
vinayakshinde1987
LQ Newbie
 
Registered: Oct 2010
Posts: 4

Rep: Reputation: 0
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
 
Old 10-27-2010, 06:20 AM   #2
prayag_pjs
Senior Member
 
Registered: Feb 2008
Location: Pune - India
Distribution: Fedora,RedHat,CentOS,Gentoo
Posts: 1,138
Blog Entries: 4

Rep: Reputation: 147Reputation: 147
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
 
Old 10-27-2010, 07:02 AM   #3
alli_yas
Member
 
Registered: Apr 2010
Location: Johannesburg
Distribution: Fedora 14, RHEL 5.5, CentOS 5.5, Ubuntu 10.04
Posts: 559

Rep: Reputation: 92
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.
 
Old 10-28-2010, 02:24 AM   #4
vinayakshinde1987
LQ Newbie
 
Registered: Oct 2010
Posts: 4

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by vinayakshinde1987 View Post
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.
 
Old 10-28-2010, 02:30 AM   #5
prayag_pjs
Senior Member
 
Registered: Feb 2008
Location: Pune - India
Distribution: Fedora,RedHat,CentOS,Gentoo
Posts: 1,138
Blog Entries: 4

Rep: Reputation: 147Reputation: 147
Hi,

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

Code:
echo $MYNAME
Code:
export MYNAME=vinayak
Code:
echo $MYNAME
 
Old 10-28-2010, 02:31 AM   #6
alli_yas
Member
 
Registered: Apr 2010
Location: Johannesburg
Distribution: Fedora 14, RHEL 5.5, CentOS 5.5, Ubuntu 10.04
Posts: 559

Rep: Reputation: 92
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.
 
Old 10-28-2010, 03:02 AM   #7
vinayakshinde1987
LQ Newbie
 
Registered: Oct 2010
Posts: 4

Original Poster
Rep: Reputation: 0
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
 
Old 10-28-2010, 03:09 AM   #8
alli_yas
Member
 
Registered: Apr 2010
Location: Johannesburg
Distribution: Fedora 14, RHEL 5.5, CentOS 5.5, Ubuntu 10.04
Posts: 559

Rep: Reputation: 92
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.
 
Old 10-28-2010, 03:21 AM   #9
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
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
 
1 members found this post helpful.
Old 10-28-2010, 05:10 AM   #10
vinayakshinde1987
LQ Newbie
 
Registered: Oct 2010
Posts: 4

Original Poster
Rep: Reputation: 0
Thanx colucix,It worked..for dynamic variables we regenerate the required file.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
export environment variables at boot-up shan23 Linux - Newbie 2 03-02-2009 08:28 AM
exporting environment variables from a file? concoran Linux - Newbie 4 04-19-2008 06:49 PM
How to export special characters into environment variables? das_Schaf Linux - General 6 06-24-2006 08:17 AM
how to refer to environment variables in an ant file irfanhab Programming 1 09-12-2005 07:15 AM
really easy question: why won't my environment variables export digitized_funk Linux - Newbie 3 10-22-2003 06:22 AM


All times are GMT -5. The time now is 11:58 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration