Add Variable in .bash_profile


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Add Variable in .bash_profile
# 1  
Old 05-24-2011
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.

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,
Dinesh
# 2  
Old 05-24-2011
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:
Code:
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.:
Code:
if [ -z "$IMPACT_HOME" ] ; then 
 #do something
fi

would change into
Code:
if [ -z "$IMPACT_HOME=/my/new/path
 #do something
fi

and create syntax error(s).
I'd write #2 like this:
Code:
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:
Code:
IMPACT_HOME=/old/path

will become
Code:
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:
Code:
grep -q IMPACT_HOME infile && sed -i 's!^\([^#]*IMPACT_HOME=\)\(.*\)!\1/my/new/path #\2!' infile || sed '$ a IMPACT_HOME=/my/new/path' infile

# 3  
Old 05-24-2011
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

Last edited by dbashyam; 05-24-2011 at 02:53 AM..
# 4  
Old 05-24-2011
It works for me:
Code:
$ 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
Code:
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:
Code:
grep -q IMPACT_HOME test || echo "IMPACT_HOME=/my/new/path" >> file

# 5  
Old 05-24-2011
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

.bash_profile question

Hello everyone, I'm trying to set my .bash_profile to change my primary prompt from this: banbatchtest1v:MCPPRD:~>to this: banbatchtest1v:MCPPRD:/home/rcarvall> Here's what my .bash_profile looks like right now: # .bash_profile # Get the aliases and functions if ; then .... (2 Replies)
Discussion started by: galileo1
2 Replies

2. Shell Programming and Scripting

Is there a way to organize bash_profile across different platforms

I want to have one .bash_profile works on multiple platform, ubuntu, debian, redhat, cygwin, osx. So how should I organize .bash_profile? It can be multiple files in some subdir Let me brief you: what i want is a way to organize bash_profile across platforms so I can use one set of profiles... (2 Replies)
Discussion started by: John_Peter
2 Replies

3. Red Hat

.bash_profile file corrupted

Hi, Unexpectedly i entered wrong entries in .bash_profile for my user which has administrative permissions. So, i am getting errors for every command. I dont have backup file also, so any body can help me how to recover it. Regards, Mastan (7 Replies)
Discussion started by: mastansaheb
7 Replies

4. Shell Programming and Scripting

[Solved] How to increment and add variable length numbers to a variable in a loop?

Hi All, I have a file which has hundred of records with fixed number of fields. In each record there is set of 8 characters which represent the duration of that activity. I want to sum up the duration present in all the records for a report. The problem is the duration changes per record so I... (5 Replies)
Discussion started by: danish0909
5 Replies

5. Linux

How to add entry into .bash_profile file

Hi, Currently If i have created new userID then .bash_profile file is created under the new user. Now, I would like to add one path in this file. Please help on this . Current file output: # .bash_profile # Get the aliases and functions if ; then . ~/.bashrc fi #... (4 Replies)
Discussion started by: Mani_apr08
4 Replies

6. UNIX for Advanced & Expert Users

bash_profile or .profile

Hi, happy new year. on AIX 6.1 , for user oracle , there are two files : bash_profile and .profile I do not know which one is executed when login ? How to know , More over in both of them we have : in .profile : ORACLE_HOME=/appli/oracle/product/10.2.0/db_1... (5 Replies)
Discussion started by: big123456
5 Replies

7. UNIX for Dummies Questions & Answers

bash_profile does not working

Hi all. when i connect as user megaguru i have a problem my .bash_profile does not working^:( if i do: . ./.bash_profile all enviroment variables are in place. How can i force linux to use .bash_profile before logon process? thanx in advance. (1 Reply)
Discussion started by: smallman
1 Replies

8. Shell Programming and Scripting

question in .bash_profile

We are more users using the oracle account, and people want to include theyr own files in .bash_profile. Like this: while ; do echo -n "LOGNAME is '$LOGNAME' (no sens), who are you? " >/dev/stderr read ln export LOGNAME=$ln done This works well when logging in to... (1 Reply)
Discussion started by: hannem
1 Replies

9. Shell Programming and Scripting

.bash_profile problem

Hi Guys, I modified my .bash_profile script , and tried to change the prompt. Following is the line of code in my .bash_profile script. export PS1=" \W " But I get the output as: \W This appears to be my prompt now. Any idea what should be done.. Thanks! (0 Replies)
Discussion started by: nua7
0 Replies

10. UNIX for Dummies Questions & Answers

Unrecognized Path in .bash_profile

Dear experts, I have installed Ruby in the following directory: $ pwd /home/ewijaya/.ruby $ ls bin lib share And I have also stated the PATH in my bash_profile like this: # .bash_profile # Get the aliases and functions if ; then . ~/.bashrc (1 Reply)
Discussion started by: monkfan
1 Replies
Login or Register to Ask a Question