Add env variable in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Add env variable in a file
# 1  
Old 05-15-2011
Add env variable in a file

Hi,

I have a small requirement

Code:
set -x
`grep IMPACT_HOME=/opt/impact /opt/NETCOOLINST/r.txt | cut -d'=' -f2`
if [ "$?" = "1" ];
then
sed 's#IMPACT_HOME=[^ ]*#d' /opt/NETCOOLINST/r.txt
echo "IMPACT_HOME=" >> /opt/NETCOOLINST/r.txt
sed 's#IMPACT_HOME=[^ ]*#IMPACT_HOME=/opt/impact#g' /opt/NETCOOLINST/r.txt
fi

I want to do the following:

1. If the particular ENV variable IMPACT_HOME is not there in a file with the said path, I want add that variable with the path that is constant.
2. If the ENV variable IMPACT_HOME is already there then I want to remove it and add the path that I have decided.

How to do it?

Thanks & Regards,
Dinesh
# 2  
Old 05-15-2011
Try this

Code:
#1. If the particular ENV variable IMPACT_HOME is not there in a file
grep -q IMPACT_HOME infile || sed -i 'i IMPACT_HOME=/my/new/path' infile

#2. If the ENV variable IMPACT_HOME is already there then
grep -q IMPACT_HOME infile && sed -i 's!IMPACT_HOME.*!IMPACT_HOME=/my/new/path!g' infile

regards,
Ahamed

Last edited by ahamed101; 05-15-2011 at 02:33 PM..
# 3  
Old 05-23-2011
Hi Mate,

This does not seem to be working.

I just created a sample .bash_profile with no IMPACT_HOME variable and ran the command

Code:
grep -q IMPACT_HOME .bash_profile || sed -i 'i IMPACT_HOME=/my/new/path' .bash_profile

I see nothing gets added in this file

Also I checked the IMPACT_HOME variable is there it does not work.

Any other alternative how to do this.

Thanks for any help.

Regards,
Dinesh
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Treat value of variable as env variable

Hi All, I have a requirement where I have a config file, which contains 2 coulmn.values of first column are environmnet variable, whose value is defined in an environment file. In my script I need to read the config file, and get the value of the config file variable from env file. I... (2 Replies)
Discussion started by: alok2082
2 Replies

2. Shell Programming and Scripting

Insert value of env variable in xml file

Hello, I have the following variables set in my env echo $MY_XSD_FILE /home/jak/sample.xsd echo $MY_INTERVAL_VALUE 4 I want to insert them between the xml tags in my xml file cat sample.xml ::::::::::::::: ::::::::::::::: <property name="FILE"></property> :::::::::::::::::::::::... (2 Replies)
Discussion started by: jakSun8
2 Replies

3. Web Development

Deny from env=env-variable Does not work

(Above from Apache docs). On my system, using: SetEnvIf User-Agent Mozilla IsBad=1 Order allow,deny Allow from all Deny from env=IsBad ...I see that environment variable is set (using phpinfo()) but the page is still served. No errors in the Apache logs. (1 Reply)
Discussion started by: gnurob
1 Replies

4. Shell Programming and Scripting

[solved] how to get specific env variable values into a file

greetings! how do i get an env variable that looks like this when echoed: into a file that looks like this: keeping in mind that the two constants are the fields from the env variable will always be in odd positions of the string that need to go into the file AND they will always start... (3 Replies)
Discussion started by: crimso
3 Replies

5. Shell Programming and Scripting

Env variable

Hello, I want to cange env variable on SunOS. I tried: export GONGA=$GONGA:/users/BANK1/basic/queues/SARON_SPACE1 it changed it only localy for my session. when i opened a new session (telnet etc') the old value exist. How can I change it to effact all sessions. Thanks. (2 Replies)
Discussion started by: LiorAmitai
2 Replies

6. Shell Programming and Scripting

get env variable from last script

I have 2 scripts t2.sh calls t1.sh. I need to get the vaule of a env variable from t1.sh /tmp/test$ cat t1.sh #!/bin/sh INSTANCE="font/fc-cache" export INSTANCE svcadm disable ${INSTANCE} /tmp/test$ cat t2.sh #!/bin/sh . /tmp/test/t1.sh echo ${INSTANCE} The above works... (9 Replies)
Discussion started by: honglus
9 Replies

7. Shell Programming and Scripting

Doubt on ENV variable

Question 1: If I set ENV=$HOME/myenvprofile.ksh, will my script get executed when ever I login to my with KSH. My doubt is we used to put this in .profile of our home directory. SO when ever I login will it executed? QUestion 2: If I set ENV=`echo "hi"` or 'echo "hi" ', what would be the output.... (0 Replies)
Discussion started by: ramkrix
0 Replies

8. UNIX for Dummies Questions & Answers

Env Variable

Hi, I have a doubt on Environment variable. I want to know where and when the envirnment variables are defined? Thanks & Regards, Siba (1 Reply)
Discussion started by: siba.s.nayak
1 Replies

9. UNIX for Dummies Questions & Answers

PWD env variable

Could you please tell me, which process / file is responsible for the setting of PWD env variable in Solaris Thanks (1 Reply)
Discussion started by: chaandana
1 Replies

10. Shell Programming and Scripting

bash env variable containing @

I want to set a bash env variable which has @ in its name, for example, @YOGESH@ may i know how do i do this? (4 Replies)
Discussion started by: Yogesh Sawant
4 Replies
Login or Register to Ask a Question