Add Variable in .bash_profile

Hi,

I wanted to do the following, but the command does not seem to work. Any ideas or suggestions please help.

#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,
Dinesh

This sed command inserts (i) a line 'IMPACT_HOME=/my/new/path' before every line in infile; which is almost certainly not what you want.
You may want to make #1 look like:

grep -q IMPACT_HOME infile || sed '$ a IMPACT_HOME=/my/new/path' infile

which will append (a) the line at the end of file ($).

#2 is a little trickier. I think the intention was to substitute the definition of IMAPCT_HOME with a new path, however, this is a not quite well designed solution. The sed command there will replace any occurence of IMPACT_HOME until the end of line with 'IMPACT_HOME=/my/new/path'.
But what if infile contains some other statements containing IMPACT HOME, that are not a definition of it? E.g.:

if [ -z "$IMPACT_HOME" ] ; then 
 #do something
fi

would change into

if [ -z "$IMPACT_HOME=/my/new/path
 #do something
fi

and create syntax error(s).
I'd write #2 like this:

grep -q IMPACT_HOME infile && sed -i 's!^\([^#]*IMPACT_HOME=\)\(.*\)!\1/my/new/path #\2!' infile

Which will change the first line containing 'IMPACT_HOME='.
It will put the old definition (string after equal sign) into a comment on the same line. So, e.g. line:

IMPACT_HOME=/old/path

will become

IMPACT_HOME=/my/new/path #/old/path

It will only change (at most) one line (no 'g' at the end); and it will not change lines containing pound symbol before IMPACT_HOME (comments).

#1 and #2 can actually be put together into one (albeit long) line:

grep -q IMPACT_HOME infile && sed -i 's!^\([^#]*IMPACT_HOME=\)\(.*\)!\1/my/new/path #\2!' infile || sed '$ a IMPACT_HOME=/my/new/path' infile 

Hi Mate,

Thanks for your reply and for your explanations.

I tried your below commands but I could not see the result that I intend to.

  1. I have this file .bash_profile
  2. Currently I don't have any IMPACT_HOME variable inside this file. I want to add a variable with the path, I tried your #2 but it did not add the variable into the file.

I just wanted to know say the IMPACT_HOME variable is not there then what modification I need to do with the sed command? You suggested me in case if the variable is there then yes your command works fine. If its not there how to add it and add the path to it?

Not sure if I had to add anything apart from the command you have given

Thanks for your help.

Regards,
Dinesh

It works for me:

$ cat test
x,y,z
a,b,c
l,m,n
o,p,q
$ grep -q IMPACT_HOME test || sed '$ a IMPACT_HOME=/my/new/path' test
x,y,z
a,b,c
l,m,n
o,p,q
IMPACT_HOME=/my/new/path

What system are you on? Can you please post output of

sed --version | head -1 

Invoke the command without the '-i' switch in sed, so that the output goes to terminal. Does it append the line?
Do you have write permissions on that file? I assume you would, if it is your .bash_profile, but please check.

Easier pure-shell alternative would be:

grep -q IMPACT_HOME test || echo "IMPACT_HOME=/my/new/path" >> file

Hi,

Sorry mate.

I tested on ZLinux. It did not even execute.

But the same script works fine on RHEL5

Sorry for the confusion. I am not sure why in ZLinux it does not execute.

Anyway in ZLinux the output you requested

lxsg0001:/opt/NETCOOLINST # sed --version | head -1

GNU sed version 4.1.4

But thanks for your solution. Works perfectly.

Thanks
Dinesh