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?
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.
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 ]
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?
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 ?
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 ?
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 ?? :
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 ?
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:
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 ]