Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place. |
| Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
06-12-2010, 04:35 AM
|
#1
|
|
Member
Registered: May 2010
Posts: 391
Rep:
|
Checking the existence of env variable
I have two questions:
1- How can I check a directory path is in $PATH?
2- I wrote this script to check an environment variable:
Code:
echo -n "checking for \$SOMETHING... "
if [ -n "$SOMETHING" ]
then
echo "yes"
else
echo "export SOMETHING=$HOME/sim" >> $HOME/.bashrc
fi;
but here is the result I get:
Code:
mahmood@hocalhost:myfoler$ echo $SOMETHING
mahmood@hocalhost:myfoler$ ./install.sh
checking for $SOMETHING... yes
Why it doesn't work?
Thanks,
|
|
|
|
06-12-2010, 05:45 AM
|
#2
|
|
Member
Registered: Jan 2007
Location: Canton, MI
Distribution: CentOS, SuSE, Red Hat, Debian, etc.
Posts: 697
Rep:
|
Question 1:
if echo :"$PATH": | grep :"$SomeDirectory": > /dev/null
then echo "$SomeDirectory" is in the current PATH
else echo "$SomeDirectory" is not in the current PATH
fi
Question 2:
How do you know it isn't working?
From your results, we can't tell whether or not $SOMETHING
is empty. For example it may have a space character.
It looks to me like it is working. The logic says if
$SOMETHING is empty then echo yes, and it did.
|
|
|
|
06-12-2010, 06:26 AM
|
#3
|
|
Member
Registered: May 2010
Posts: 391
Original Poster
Rep:
|
Consider this script:
Code:
echo -n "checking for \$SOMETHING... "
if [ -n "$SOMETHING" ]
then
echo "yes"
else
export SOMETHING=$HOME/sim
echo "export SOMETHING=$HOME/sim" >> $HOME/.bashrc
echo "no -> added"
fi;
Now I run this script for the first time:
Code:
mahmood@localhost:~$ ./install.sh
checking for $SOMETHING... no -> added
without closing the current terminal, I checked:
Code:
mahmood@localhost:~$ grep "SOMETHING" .bashrc
export SOMETHING=/home/mahmood/sim
mahmood@localhost:~$ echo $SOMETHING
mahmood@localhost:~$
I ran the script again and got this output:
Code:
mahmood@localhost:~$ ./install.sh
checking for $SOMETHING... no -> added
mahmood@localhost:~$ grep "SOMETHING" .bashrc
export SOMETHING=/home/mahmood/sim
export SOMETHING=/home/mahmood/sim
If I close the terminal and reopen it again, every thing is fine:
Code:
mahmood@localhost:~$ echo $SOMETHING
/home/mahmood/sim
mahmood@localhost:~$ ./install.sh
checking for $SOMETHING... yes
I don't know what is wrong with that. I know that the script logic is true.
|
|
|
|
06-12-2010, 06:52 AM
|
#4
|
|
Moderator
Registered: May 2001
Posts: 24,823
|
AFAIK there is nothing wrong. The first time SOMETHING is empty (and exporting SOMETHING would then affect the environment of the current shell and children) but since you dumped the export in your .bashrc instead (which isn't exactly behaviour you would want to propagate beyond your own system) it gets sourced when starting a new shell session. See the "invocation" part in 'man bash'?
|
|
|
|
06-12-2010, 07:23 AM
|
#5
|
|
Member
Registered: May 2010
Posts: 391
Original Poster
Rep:
|
In this part:
Code:
else
export SOMETHING=$HOME/sim
echo "export SOMETHING=$HOME/sim" >> $HOME/.bashrc
echo "no -> added"
First I am exporting the variable in the current session and then add that entry to the .bashrc file for future uses.
Now:
Code:
mahmood@localhost:~$ grep "SOMETHING" .bashrc
export SOMETHING=/home/mahmood/sim
mahmood@localhost:~$ echo $SOMETHING
mahmood@localhost:~$
variable is added to the .bashrc file and *seems* that the first command (exporting to current shell) was not added because echo command return nothing.
|
|
|
|
06-12-2010, 07:41 AM
|
#6
|
|
Moderator
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.4 OpenSuSE 12.2
Posts: 9,897
|
Please re-read the post from unSpawn, since it has the answer to your question:
Code:
.bashrc ... gets sourced when starting a new shell session
this means that it's not enough to write the export statement into $HOME/.bashrc... it has to be sourced to apply the changes (otherwise it is simply something written in a text file and nothing more). If you want to apply the changes to the current shell just do:
Anyway, what is the aim of this? If you want to provide an installation script, I think it's better to not modify the user's .bashrc without he/she is aware of the change. I personally would prefer the installation script warns me about the changes I must apply (that is it makes notice about a necessary environment variable) then I will apply it at my pleasure, eventually following the advice to write the export statement in my .bashrc. Just my 
Finally, following again the answer from unSpawn, you should definitively have a look at the bash man page or at the Bash reference manual to see how environment variables work and how they are assigned by the login shell when invoked.
|
|
|
|
06-12-2010, 08:58 AM
|
#7
|
|
Member
Registered: May 2010
Posts: 391
Original Poster
Rep:
|
believe me that sourcing .bashrc in a bash file does not work.
This is my script (I remove all other actions)
Code:
#! /bin/bash
echo "export SOMETHING=$HOME/sim" >> $HOME/.bashrc
source .bashrc
And this is the run:
Code:
mahmood@localhost:~$ chmod +x var-test.sh
mahmood@localhost:~$ ./var-test.sh
mahmood@localhost:~$ echo $SOMETHING
mahmood@localhost:~$ grep "SOMETHING" .bashrc
export SOMETHING=/home/mahmood/sim
but sourcing from the terminal works:
Code:
mahmood@localhost:~$ source .bashrc
mahmood@localhost:~$ echo $SOMETHING
/home/mahmood/sim
I will be very thankful if you run this script in your own system and let me know the result.
Last edited by mahmoodn; 06-12-2010 at 09:02 AM.
|
|
|
|
06-12-2010, 09:14 AM
|
#8
|
|
Moderator
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.4 OpenSuSE 12.2
Posts: 9,897
|
Quote:
Originally Posted by mahmoodn
believe me that sourcing .bashrc in a bash file does not work.
|
Believe me, I believe you! The shell cannot retrieve environment variable from its childs. Every variable assigned into the script (even with a source command) is lost when the script terminates. The parent shell will not be aware of the changes.
Indeed it would not be useful at all trying to set environment variables from a script. This is the reason why configuration files like .bashrc, .bash_profile or the system-wide /etc/bashrc and so on, exist and are sourced every time you invoke a new shell.
In your example, you modify the $HOME/.bashrc from a script, but you have to source it in the parent shell (after the script has finished its job) to see the changes applied to the current shell (without closing it).
|
|
|
|
06-12-2010, 09:51 AM
|
#9
|
|
Member
Registered: May 2010
Posts: 391
Original Poster
Rep:
|
So I think I have to leave it at this time until finding a better solution. I will write in my readme file that the users should enter the variables manually and then run the install script.
Using this solution will then satisfy the privacy of users because they are aware of changing their env variables.
Thank you all 
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 01:02 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|