aspose file tools*
The moose likes Linux / UNIX and the fly likes getting and setting environment variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Linux / UNIX
Bookmark "getting and setting environment variables" Watch "getting and setting environment variables" New topic
Author

getting and setting environment variables

deep venu
Ranch Hand

Joined: Jun 09, 2001
Posts: 56
How does one set and get values of an environment variable that is being passed as an argument to a particular shell script?
Thank You
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
I think we need to know a little more. In particular how the environment variable is being "passed in", and which shell you are using.
Do you mean that you want to access an envoronment variable inside a script? Do you mean that you want to access one of the command-line arguments to the script? Do you mean that the name of an environment variable is passed into the script as an argument?


Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
deep venu
Ranch Hand

Joined: Jun 09, 2001
Posts: 56
Hi,
Sorry for incomplete question. but yeah, I am talking about a name of an environment variable being passed as a command line argument to a shell script. That is incase we want get the value , when we need to set a value, along with the name of environment variable the value is also passed as another command line argument.
Snigdha Solanki
Ranch Hand

Joined: Sep 07, 2000
Posts: 128
Not sure if I fully understood your question. But if your problem is getting command line parameters, you can get them by using $1,$2 and so on.


Snigdha<br />Sun Certified Programmer for the Java™ 2 Platform
deep venu
Ranch Hand

Joined: Jun 09, 2001
Posts: 56
Hi snigdha
nope that was not my question :-)
my question was that I have a shell script A.sh.
Now, I need to use this shell script set/get the values of environment variables.
Now if I need to get the value of an environment variable I would need to do something like
A.sh <environment- name>
NOw how does the shell script take this $1 argument and refer to it as an environment variable and get the value ?
i.e. the code var=$1 echo $var will only get me the name of the environment variable that was passed to the shell script rather than the value of environment variable.
similarly for setting i want to know how it can be done.
additionally i am calling this shell script running in K shell by using Runtime.getExec() from a java program
[ July 31, 2002: Message edited by: deep venu ]
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
This all seems a very complicated process you are going through. I have a strong feeling that you may not need to do this at all.
Can you explain for us a bit more about what you are doing all this for? why do you need to read/set these environment variables?
deep venu
Ranch Hand

Joined: Jun 09, 2001
Posts: 56
Hi Frank,
ok didn't realize that i was walking into a complicated puddle :-)
OK, Ill try and explain the scenario. Basically we trying to migrate incremental data from one database to another, put in a very skeletal manner.
Now we extract incremental data by running a shell script that compares two unl files(ascii file generated by unloading tables in a database)
The differential/incremental data(with each record marked as updated, inserted deleted etc) is then formatted into a certain XML file and then sent across to another database.
Now we have requirement that we may need to run a first time migration cycle at any point of time, in which case this script has to just append a inserted tag at the end of every record.
Now the shell script understands this by cheking the value of an environment variable. This is the environment variable is the one that a java program reads from a config file and sets the value for the shell script to take appropriate action. phew ! :-) I hope i made some sense :-)
so i wanted ot know whether it is possible by a java program to set and get values of environment variables ?
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
a java program reads from a config file and sets the value for the shell script to take appropriate action.
I thought it might be something like this. Have you considered just putting a set for the environment variable at the start of the command string you execute, for example:

Or is this approach not possible for some reason ?
deep venu
Ranch Hand

Joined: Jun 09, 2001
Posts: 56
Hi Frank,
yup..it makes sense but a small lingering question. Now if the java program in which this particular code is being written also needs to check this value...any idea how we could do this?
some more info.
we plan to initially set this environment variable thru an installation script. Hence, the java program needs access this variable, and probably set a different value for this variable based on some business considerations.
but thanks for showing the way to set.. I want to GET toooooooooooooooo ?? :
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
the java program in which this particular code is being written also needs to check this value
The way I usually do this is to start my java program using a small shell script:

Then in the code I can use

If you don't mind using a deprecated method, you could alternatively try System.getenv(String name) which reads an environment variable directly on platforms which support it.
deep venu
Ranch Hand

Joined: Jun 09, 2001
Posts: 56
Hi Frank,
yup i had tried out the System.getProperty() and it gets values which have been set by java -D option but not environment varaibles. I am just trying the getEnv () method ..but i do really need one which provides the same function w/o being a deprecated one i may not be allowed to use it. nay pointers?
some more info
java -D cannot be done in our case cause the initiation of the service in being done via a rmi lookup and an api exposed by the rmi server java program.
am i getting too confusing ?
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
i do really need one which provides the same function w/o being a deprecated one
There are several reasons why getenv() is deprecated.
One is because it is not at all portable. The basic method will work on Windows, Linux and Unix, but the environment variable conventions are quite different. Many other platforms where Java runs have no concept of environment variables at all.
Another, more subtle problem is that it's hard to work out what it should do in a multi-threaded system. Imagine the confusion if one thread tries to change an environment variable and another tries to read it!
nay pointers?
If you really have to do this, you can ask the system to echo the environment variable and read the output in your program:
deep venu
Ranch Hand

Joined: Jun 09, 2001
Posts: 56
Hi Frank
thanx a lot...
very embarassing to know that i cldn't think of this option...:-)
Leslie Chaim
Ranch Hand

Joined: May 22, 2002
Posts: 336
I would not be so deeply embarrassed. It still does not work for me...
I was only interested in getting and a search turned up this thread but it still does not work for me.
What am I missing, Frank?

To test this from the shell:
var=hello
export var
If I go in a subshell and
echo $var
I get my hello.
But when I run my java program:
java my_util var
Nothing happens.
Please advise.
Thanks.
[ August 06, 2002: Message edited by: Leslie Chaim ]

Normal is in the eye of the beholder
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: getting and setting environment variables