How to set java environment variables using shell script - linux

I know I can just type
$ vi .bashrc
export JAVA_HOME=/usr/lib/jvm/java-7-oracle
export PATH=$JAVA_HOME/bin:$PATH
However how to use shell script to do it? I prefer to write shell script because I need to configure multiple servers, if type one by one I would take me a long time to go.
Can someone guide me how to do this? Thanks a lot!

EDIT: I just realized that you wanted a script to automate the process of adding environment variables. These commands may work for you:
echo "export JAVA_HOME=/usr/lib/jvm/java-7-oracle" >>~/.bashrc
echo "export PATH=$JAVA_HOME/bin:$PATH" >>~/.bashrc
What this does is append the given text to .bashrc. Instead of copying .bashrc files from server to server, run these commands (you could probably write a script for these) on each server. This preserves the contents of the original rc files on each server, which I find is a better idea than completely overwriting the file.
Original answer
Your .bashrc file is actually written as a shell script. You would place the exact same lines in the shell script, possibly with a hashbang at the beginning of the file. For example:
#!/bin/bash
export JAVA_HOME=/usr/lib/jvm/java-7-oracle
export PATH=$JAVA_HOME/bin:$PATH
# do java stuff here...
If you were to "do java stuff" in this script, this would work fine. However, if these variables are going to be used outside of the script, you would have to "source" this file. That is what happens with .bashrc. Before the first prompt is given, Bash runs source ~/.bashrc to publish the variables defined in .bashrc.

Related

Linux: export environment variable in a shell script to make it flexible on any server

In order to run a Tcl script on Linux, I need to set the environment variable "$LD_LIBRARY_PATH" each time.
For convenience, I develop a shell script to do this.Currently, on my own server, if I type
echo $LD_LIBRARY_PATH
the result is:
/opt/lsf/9.1/linux2.6-glibc2.3-x86_64/lib
so in my shell script I write the following code:
export LD_LIBRARY_PATH="/opt/lsf/9.1/linux2.6-glibc2.3-x86_64/lib:$INSTALL_ROOT/tcl_tk/lib64:$INSTALL_ROOT/tcl_tk/lib64"
where the "$INSTALL_ROOT/tcl_tk/lib64:$INSTALL_ROOT/tcl_tk/lib64" part is what I want to add. It works well. Now the issue is:
If I want to run the script on any server, so the original "$LD_LIBRARY_PATH" will be different, based on my understanding. So how to make it flexible on any server?
I try this in my shell script:
export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:$INSTALL_ROOT/tcl_tk/lib64:$INSTALL_ROOT/tcl_tk/lib64"
But not so sure,
I am new to system stuffs, need some help. Hope explain the issue clearly.
If your default shell is bash, i would define the variables in ~/.bashrc like:
export INSTALL_ROOT=...##assuming a lready defined
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$INSTALL_ROOT/tcl_tk/lib64:$INSTALL_ROOT/tcl_tk/lib64
So in this way, you dont have to worry anything about setting variables in multiple shell scripts as this .bashrc is going to setup variable for you beforehand.

turning on / off environment variables

I have been trying to set the $PATH environment variable to point to different paths as I need them, but I have run into some issues.
The main thing is that when I set the PATH to point to my service that I need, I want it to stay that way in all subsequent bash shells. That is, when I open another bash shell it will be set there as well, until I decide to switch it back. And when I switch the PATH back to its original value. I want it to stay that way.
I added a small script to my .bashrc, I also tried doing a separate script that will change environment variables. But the problem stays: If I open a new bash shell, it inherits the default environment variables and the default PATH.
I am setting those to enable the use of a daemon service. I tried turning on/off the service itself. That does not work because the paths have to be changed or else the shell would try to use those environment paths, but the service being off it will just hang.
I tried running a small C program wrapper to do those things but I ran into the same issue.
In order for environment variable to exist across shell sessions you will need to place those variables into a shell configuration file. This is usually done in either ~/.bashrc or ~/.profile (if you are using Bash). For example, in my ~/.profile file I have:
export PATH=/opt/local/bin:/opt/local/sbin:$PATH
I can add to that path as much as I want. The $PATH variable at the end will append whatever is current in the path.
If you need scripts to be able to access the variables, you should put the PATH into the .bashrc file. (what is the difference between .bashrc and .profile)
If you make a change to one of your shell configuration files, the changes will not be in effect until those files are used again. Those files are only used when the shell is first initialized. In order to make changes take effect, you need to log out and back in, or open a new shell and close the old, or source the file that you made changes to.
$ vim ~/.bashrc (edit the file)
$ source ~/.bashrc (then reinitialize the shell with the file)
If you only need a variable to be available in the current shell and any subprocesses, using export would be all you need.
You might also be interested in this snippet. It reloads the .bashrc in any shell, whenever it is modified. (After re-editing the .bashrc file, press (or run a command) to cause the new .bashrc to be reloaded.)
# Make sure our version of the .bashrc file is up-to-date, or reload it.
chk_bashrc_timestamp () {
if [[ "$_BASHRC_TIMESTAMP" -lt "$(get_file_timestamp "$HOME/.bashrc")" ]]; then
echo >&2 "Reloading .bashrc..."
. ~/.bashrc
fi
}
_BASHRC_TIMESTAMP=$(date +%s)
PROMPT_COMMAND=chk_bashrc_timestamp
The only difficulty is, you must not use $PATH in the definition of PATH, or it will repeat the whole PATH each time it is needed:
Use
SYSPATH="/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin"
PATH="$HOME/bin:$SYSPATH:/opt/mystuff"
instead of
PATH="$HOME/bin:$PATH:/opt/mystuff"
I have used this with much satisfaction in my own .bashrc, except that I have protected my PATH from reloading each time by setting it in an if statement. (You may find you have other things you need to protect this way.)
if _BASHRC_WAS_RUN 2>/dev/null; then
:;
else # Stuff that only needs to run the first time we source .bashrc.
# Useful to allow resourcing new changes
export PATH="$HOME/bin:$HOME/.cabal/bin: ..."
alias _BASHRC_WAS_RUN=true
fi

How to source file from bash script

I'm trying to source a file with an environment variable from my bash script, but it doesn't work.
This is the content of my script (test.sh), which is located in ~/scripts/test.sh.
#!/bin/bash
FILE_NAME=/tmp/source_file
touch $FILE_NAME
echo "export TEST=\"test\"" > $FILE_NAME
source $FILE_NAME
Then I use alias in my ~/.bashrc.
alias testScript=~/scripts/test.sh
But when I use my script testScript, it didn't set the environment variable.
You need to use:
alias testScript=". ~/scripts/test.sh"
to source the file. Or you can use source in place of ., but I don't much like C shells so I don't use C shell notations such as source.
Environment variables only flow downstream in the process tree.
When you type testScript to a bash process, it creates a child process and execs /bin/bash or whatever is set by #!
Any environment variables set there remain only with the child process. Export causes the variables to be copied to additional grandchildren (children of that child) that might be spawned from that child.
Nothing can copy back to a parent. You need to use source instead of running the file. See Jonathan's answer.
You could try editing the files ~/.bashrc or ~/.login to set enviornment variables you need frequently.
See also https://superuser.com/q/153371 and https://superuser.com/questions/18988/difference-between-a-b-and-export-a-b-in-bash for more explanation of export in bash.
None of the other methods worked for me [source /path/to/file vs . ./path/to/file, alias, etc...], until, thanks to this tutorial I found that using the:
#!/usr/bin/env bash shebang
instead of the simpler #!/usr/bin/env one lets arguments pass on to the interpreter, which I think is the key here – see this document for more info.
In any event, if source commands in any form aren't working for you, try checking your shebang, that might be the problem :)

Setting permanent path through a script

I am trying to add a folder to the PATH in linux. I want to automate it through a script.
This is the script I have written:
#!/bin/sh
echo "Setting PATH..."
echo "export PATH=$PATH:/opt/mysoftware/scripts/client" >> ~/.bashrc
. ~/.bashrc
Even after executing the script, PATH is not getting updated.
But I can see that bashrc file has been updated.
When I logout and login, PATH is updated.
What might be the problem?
You're running the script in a child shell. Try sourcing it:
source script.sh
If you want this in your .bashrc, delete the script. You're done now. ;-)
The alternative is to put this in a function. I used to have two functions,
use() and forget() in my ksh environment that did exactly that.
use /opt/python would be equivalent to PATH=/opt/python/bin:$PATH, once.
A second run would do nothing. Even use python would look in a couple of
locations for /{usr,opt}/python/{bin,sbin} and insert the first match to PATH.
Conversely, forget python would remove /opt/python/bin: from the PATH again.

shell export variable not come into effect

I (on mac osx) often use
export http_proxy=http://192.168.0.205:1099
to proxy http connection to get a highed download speed. To make things easy, I wrote a shell file named proxy.sh to do this:
#!/bin/sh
export http_proxy=http://192.168.0.205:1099
Before I downlaod, I execute proxy.sh shell command, but I found it did't not come into effect.It lost http_proxy variable in current commnad window(terminal). I must type export command in current terminal,it will come into effect.
So I want to know what's reason for this and a solution? thanks.
Running a shell script "normally" (with proxy.sh for example) results in that running in a sub-process so that it cannot affect the environment of the parent process.
Using . or source will run the shell script in the context of the current shell, so it will be able to affect the environment, using one of the following:
. proxy.sh
source proxy.sh
Another possibility (if you're using bash at least) is to create an alias to do the work for you. You can use something like:
alias faster='export http_proxy=http://192.168.0.205:1099'
so that you can then simply type faster on the command line and it will export that variable (in the context of the current shell).
You could also allow for one-shot settings such as:
alias faster='http_proxy=http://192.168.0.205:1099'
and then use:
faster your_program
which would translate into:
http_proxy=http://192.168.0.205:1099 your_program
That's a bash way to set a variable for just the one invocation of a command.
The export variable will only apply to the script -- if you want it to apply to the shell, you need to use source, and execute the script like so:
. ./proxy.sh
or:
source ./proxy.sh
Note the "." in the first example -- the dot follow by space means the script will apply to the shell.
The reason why your script does not work has been explained by Drakosha & how to make your script work has been explained by Anothony. But with the export in the script you need to source your script each time you open a new terminal. A better solution will be to add the export in .bash_profile or .bashrc
Hope this helps!
When executing a shell script a new shell is launched, the script is executed, and the shell dies. That's why you don't see the variable defined in your shell.
I suggest using an alias for the same purpose.

Resources