LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
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



Reply
 
Search this Thread
Old 06-12-2010, 05:35 AM   #1
mahmoodn
Member
 
Registered: May 2010
Posts: 403

Rep: Reputation: 16
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,
 
Old 06-12-2010, 06:45 AM   #2
carltm
Member
 
Registered: Jan 2007
Location: Canton, MI
Distribution: CentOS, SuSE, Red Hat, Debian, etc.
Posts: 700

Rep: Reputation: 96
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.
 
Old 06-12-2010, 07:26 AM   #3
mahmoodn
Member
 
Registered: May 2010
Posts: 403

Original Poster
Rep: Reputation: 16
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.
 
Old 06-12-2010, 07:52 AM   #4
unSpawn
Moderator
 
Registered: May 2001
Posts: 27,770
Blog Entries: 54

Rep: Reputation: 2976Reputation: 2976Reputation: 2976Reputation: 2976Reputation: 2976Reputation: 2976Reputation: 2976Reputation: 2976Reputation: 2976Reputation: 2976Reputation: 2976
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'?
 
Old 06-12-2010, 08:23 AM   #5
mahmoodn
Member
 
Registered: May 2010
Posts: 403

Original Poster
Rep: Reputation: 16
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.
 
Old 06-12-2010, 08:41 AM   #6
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
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:
Code:
. $HOME/.bashrc
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.
 
Old 06-12-2010, 09:58 AM   #7
mahmoodn
Member
 
Registered: May 2010
Posts: 403

Original Poster
Rep: Reputation: 16
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 10:02 AM.
 
Old 06-12-2010, 10:14 AM   #8
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Quote:
Originally Posted by mahmoodn View Post
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).
 
Old 06-12-2010, 10:51 AM   #9
mahmoodn
Member
 
Registered: May 2010
Posts: 403

Original Poster
Rep: Reputation: 16
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
 
  


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 On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help needed in setting env variable mageshvan Programming 2 10-01-2007 02:47 AM
Checking whether build env is sane? sly0ne Linux From Scratch 1 05-22-2007 08:28 PM
Checking existence on remote server MikeAtVillage Programming 2 04-03-2006 12:16 PM
env variable allelopath Linux - Software 3 04-14-2005 01:07 PM
How to pass env variable to KDE? koyi Linux - General 0 09-03-2003 04:29 AM


All times are GMT -5. The time now is 07:31 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration