Discussion Locked

Shell Scripts and Exporting

0 helpful
9 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

Hello, I am new to Linux and we plan to develop application servers in Linux. Currently, I am trying to get Weblogic's Javabean container and Console running from a script. I can run the Console and the container from the command line no problem however, I am having difficulty getting the shell scripts to work. Help?

I mean, I can not even get the weblogic .bash_profile for the user I set up to export the PS1 variable I assigned or any other. Also, Weblogic has a setEnv.sh script to set up and export some more special variables such as JAVA_HOME but after you run the script "sh setEnv.sh" and then type "echo $Java_Home" nothing. I have already made sure weblogic owns the file,directory and permissions are set 755. This can't be rocket science and the funny thing is if I assign variables and export them in etc/profile, they sometimes work with no problem but I do not want to clutter etc/profile if I do not have to or, is there where this stuff should go anyway. Please tellme what is going wrong with my shell scripts? P.S. I am running Red Hat 6.2

9 replies

by tastin

I played around with a quick test file and found that typing "source shell.sh" set the variables I placed inside them in my local enviroment. I'll keep checking this out. I haven't messed with shell scripts in a while. Hope this helps.

by hasse mcse/brainbench

Hi!

I'm not sure about this, but in order to get variables to work you can either user source a .sh file as stated above or use the users .bashrc and .profile.
.bashrc in users home dir is used if that users logs in and has defualt shell bash.
If you instead have #!/bin/sh at the top of your start and stop scripts then this shell won't read .bashrc .profile or any other startupfiles.

So this menas you'll have to set them in the shell script.

/Hasse

by jpereira

Your problem is that when you run a program or script from another script, Unix actually creates a new process which is a complete duplicate of the original process (including its environment) and runs the new script in that. Any changes to the environment are made to the duplicate environment which goes away when the new script exits. If you want to have variables change as a result of running a script, you need to "source" the script i.e. run it without creating a new process. This is done in a korne shell as follows: ". your_script" (note the leading "."). I think the same thing will work in bash.

by zbrain75

jpereia@sendmail.com is correct. I made the following testfile called test1:
#!/bin/bash
export MYVAR="this is a test"

If it is run from the command line as follows:
test1
the variable is not set, but if run as follows:
. test1
You can type "echo $MYVAR" and see the variable and also see it with the "env" command.

The problem with your PS1 variable is that in Redhat, the user's $HOME/.bashrc script runs the /etc/bashrc file which resets the PS1 variable to a preset value. I recommend commenting out the call from $HOME/.bashrc to /etc/bashrc, and setting the PS1 var in the user's .bash_profile script. See the Linux tip at http://ctdp.homestead.com/files/os/linux/tips/index.html Tip#2 The same info may be more accessable at http://ctdp.tripod.com/os..

This discussion is closed to new replies.