I am pretty new to java and unix. I have the following questions:
1. we have a shell script sample.sh that sets environment variable. we always execute the script by first going to the location where the script is kept and then execute the command in following way:
when I try to execute the same script by
the environment variables are not set whereas they are properly set through . ./sample.sh
can anyone please tell me the difference why it fails to set env in second way?
2. I wish to use a java program (to be run in unix) to set the above environment variables. I am really confused about the method I should use for this
a. either call the script
b. directly set the environment variables from the file sample.sh
I fear how i can call . ./sample.sh in java.
my aplogies for the naive question!
thanks in advance,
1) Doing a ". ./sample.sh" means to "source" the command in sample.sh. What this means is that each command in sample.sh is entered as if you had typed it by hand at the command prompt. In other words, the commands are run in the same shell session.
On the other hand, when you so "./sample.sh", a new shell session is started and the commands are run in there. Then when the script is done, that shell session exits, and you lose any settings that were mode. Another way to think of it is like this: you opened a new terminal window, entered the commands, and then exited the terminal window.
2) You cannot use a Java program to set environment variables. What is it that you are really trying to do? Can you give us a concrete example? I suspect that what you really want is a properties file that is read by the Java program.
The first dot in ". ./sample.sh" is an alias for "source" which means to execute the script within the context of the current shell. If you just run "./sample.sh" the script will be executed in a separate shell process, hence why the environment variables set in the separate thread won't change the ones in your current shell.
thanks a lot Peter and Koen.
@Peter: I want to do the same thing like . ./sample.sh through a java program.
My intention is to set the environment variables which are mentioned in the file sample.sh (snippets from the file sample.sh:
I think there should be a way in java program to set these environment variables (either by calling sample.sh through my java program or by directly setting these variables in java)
The answer to my question "What is it that you are really trying to do?" is NOT "My intention is to set the environment variables". That is a circular answer; in other words, it is like saying "I want to set environment variables because I want to set environment variables". What I am asking is what do you hope to gain by setting environment variables from a Java program? What would those variables be used for?
ok Peter. got your point.
I wish to set the environment variables because I would be running an executable. the pre-requisite to run this exe (A Teamceter specific - PLM executable) is that the necessary environment variables should be pre-set, which I currently set in sample.sh
thanks for your interest. appreciate your patience,