• 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

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

 
Greenhorn
Posts: 11
Mac PPC
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author
Posts: 5856
7
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 344
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 11
Mac PPC
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5856
7
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 11
Mac PPC
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5856
7
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Rancher
Posts: 4803
7
Mac OS X VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Write one script that sets the environment variables and executes the program.

That is the Unix/Linux way
 
kunal kulkarni
Greenhorn
Posts: 11
Mac PPC
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic