Operating System - HP-UX
1847466 Members
3285 Online
110265 Solutions
New Discussion

Setting my SHELL variables

 
Nagendra_6
Occasional Contributor

Setting my SHELL variables

Hi ,
I have shell script(run.sh) where i set my variables like
--------------------------
PATH=/opt/bea/hom
CLASSPATH=/opt/bea/weblogic.jar
export PATH
export CLASSPATH
---------------------------
I do the following
1.sh run.sh
2.echo $CLASSPATH

FOr some reasons, the one set in my shell script is not being reflected in my shell, Can you help know where i am doing it wrong.

Tks,

13 REPLIES 13
Jeff Schussele
Honored Contributor

Re: Setting my SHELL variables

Hi Nagendra,

You need to source the file, not run it - EX:

. run.sh

That way it applies to your current shell. When you run it it only applies to the shell that runs it...and then exits.

HTH,
Jeff
PERSEVERANCE -- Remember, whatever does not kill you only makes you stronger!
Hoefnix
Honored Contributor

Re: Setting my SHELL variables

just try,

./run.sh

Without the sh.

HTH,
Peter
Jim Mallett
Honored Contributor

Re: Setting my SHELL variables

Nagendra,

The variables are being set in a child shell when you run # sh run.sh
Those variables, even if exported, are not passed back to the parent shell that ran the program.

Instead you could do: ./run.sh

Jim
Hindsight is 20/20
Nagendra_6
Occasional Contributor

Re: Setting my SHELL variables

I did

1) ./run.sh and
2) echo $PATH

But the path is not being reflected :(


Hoefnix
Honored Contributor

Re: Setting my SHELL variables

and if you do:

export PATH=/opt/bea/hom

And then do:
echo $PATH
It should reflect your PATH.

If you try to source the file like Jeff suggest, will that work?

HTH,
Peter
Jim Mallett
Honored Contributor

Re: Setting my SHELL variables

Sorry, use Jeff's solution. I should have known because that is how I have to set my DISPLAY variable from time to time.

Just tested it:

. run.sh


/no points please/
Hindsight is 20/20
Tanmay_1
Occasional Advisor

Re: Setting my SHELL variables

Hi try this.

Rename the file from run.sh to .run.sh
chmod 666 .run.sh
$. ./.run.sh

Thanks
Patrick Wallek
Honored Contributor

Re: Setting my SHELL variables

No need to rename the file.

You just need to do:

. run.sh

Note that there is a SPACE between the first . and the run.sh. That is what tells the shell to apply the settings to your running shell.

Nagendra_6
Occasional Contributor

Re: Setting my SHELL variables

Thanks for your replies.

When i do a
$ . run.sh

i get the following,

$ su: run.sh not found.

any idea where i could be wrong. I have the permissions..

Tks, Nag

Hoefnix
Honored Contributor

Re: Setting my SHELL variables

Just do:
. ./run.sh

Your local-path is not your PATH variable so use ./ infront of it.

HTH,
Peter
Jeff Schussele
Honored Contributor

Re: Setting my SHELL variables

Hi (again) Nagendra,

That's because your current dir is not in the path - do it this way

. ./run.sh

./ tells it to look in your current dir.

Rgds,
Jeff
PERSEVERANCE -- Remember, whatever does not kill you only makes you stronger!
Bill Hassell
Honored Contributor

Re: Setting my SHELL variables

When you simply type the name of any shell script (or in your case, sh <script-name>, a separate child or subprocess shell is started and your script runs there. When the script ends, the sub-shell is removed and nothing is returned to the parent. What you require is called 'sourcing' and it is essentially a way to have the current shell run each of the script's steps. It is run with the smallest shell command: a period (or dot).

Sourcing can be included in a script and in your example for WebLogic, you can source the run.sh as mentioned above with just . shell-script. This is a common way to provide shell variab;les to every user login too. Just source a script in /etc/profile and now everyone gets the env values.

You might want to update your script to take advantage of POSIX shell features such as type and assign on one line:

export PATH=/opt/bea/hom
export CLASSPATH=/opt/bea/weblogic.jar

Also, all your shell scripts should explicitly define the shell interpreter on line 1. Just insert this as line 1:

#!/usr/bin/sh


Bill Hassell, sysadmin
A. Clay Stephenson
Acclaimed Contributor

Re: Setting my SHELL variables

There is one other rule that you need to apply to any sourced file within a shell script. It must not contain an exit statement or a return statement (unless inside a function) because the exit or return will have the effect of exiting the foreground process --- which is probably not your intent.
If it ain't broke, I can fix that.