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

difference between ./sh and . ./sample.sh

kunal kulkarni
Greenhorn

Joined: Mar 29, 2012
Posts: 11

Hi Folks,
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:
. ./sample.sh

when I try to execute the same script by
./sample.sh
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,
Kunal
Peter Johnson
author
Bartender

Joined: May 14, 2008
Posts: 5830
    
    7

kunal, welcome to Java Ranch

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.

JBoss In Action
Koen Aerts
Ranch Hand

Joined: Feb 07, 2012
Posts: 344

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.

Edit: Peter beat me to it
kunal kulkarni
Greenhorn

Joined: Mar 29, 2012
Posts: 11

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:
export LDR_CNTRL=
export DB2_INCLUDE=
export DB2PATH=
. $DB2PATH/db2profile
)

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)

thanks,
Kunal
Peter Johnson
author
Bartender

Joined: May 14, 2008
Posts: 5830
    
    7

Reread my answer #2.

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?
kunal kulkarni
Greenhorn

Joined: Mar 29, 2012
Posts: 11

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,
Kunal
Peter Johnson
author
Bartender

Joined: May 14, 2008
Posts: 5830
    
    7

So if I get this correct, you have an executable, and that executable expects certain environment variables to be set. And you would like to set those env vars before running the executable.

How are you running that executable?
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4658
    
    5

Write one script that sets the environment variables and executes the program.

That is the Unix/Linux way
kunal kulkarni
Greenhorn

Joined: Mar 29, 2012
Posts: 11

@Pat: thanks. Your solution worked. I can now set env to run my exe via java
@Peter: thank you for your help. Appreciate it.

Regards,
Kunal
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: difference between ./sh and . ./sample.sh