• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

getting and setting environment variables

 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
deep venu
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
deep venu
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Frank
thanx a lot...
very embarassing to know that i cldn't think of this option...:-)
 
Ranch Hand
Posts: 336
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic