Set environment variables using shell script (Linux) [duplicate] - linux

This question already has answers here:
Global environment variables in a shell script
(7 answers)
Closed 8 years ago.
I am novice for shell scripting. I have written one script which checks if ORACLE_HOME AND ORACLE_SID are set. Inside this I have called other script to set the env variables.
First Script
while [ 1 -gt 0 ]
do
echo -e "Please enter path of oracle home directory:\c"
read DB_HOME
if [ -d $DB_HOME ]
then
./oracle_env.sh $DB_HOME "test1"
echo "ORACLE_HOME has been set successfully!"
status="Y"
break
else
echo "Path or directory does not exist."
fi
done
Second Script
#This script will set ORACLE_HOME and SID
export ORACLE_HOME=$1
export ORACLE_SID=$2
When I run the second script as
./oracle_env.sh /u01/app/oracle test
it's working fine. I mean, when I run
echo $ORACLE_HOME
it gives path like
/u01/app/oracle
Now the problem is when I run the same script from first script, it's not working.
Please help me out !!!

The problem is quite simple:
If you execute a script it starts in a new shell, sets the environment there and close the shell. As result nothing changes in the first calling shell.
So you have to execute the script in the first shell with source <shellscript>
For details see man bash
I have no idea which shell you use. Maybe the solution is a bit different for other shells.

Try this for setting environment variable in your terminal: (Below code is for xampp not for oracle, path will vary w.r.t your requirement)
export PATH=/opt/lamp/bin:$PATH
You can see your environment variables by:
echo $PATH
See, if that works for you.

Run the script with source (or) . command.
while [ 1 -gt 0 ]
do
echo -e "Please enter path of oracle home directory:\c"
read DB_HOME
if [ -d $DB_HOME ]
then
. oracle_env.sh $DB_HOME "test1" ## Here you run the script with . command.
echo "ORACLE_HOME has been set successfully!"
status="Y"
break
else
echo "Path or directory does not exist."
fi
done
Run your first script also with . command.
$ . script.sh

Related

How to check if the command was executed in bash script and do not execute this command again?

I don't want to source my .sh script every time before I start packer build command ... because I always forget to do this. These tasks are repeatable and it makes sense to create a shell script for that.
Problem: If the command
$source env.sh
was executed once, I don't want to execute this command again but continue with the others. Is there any solution for that ? My script:
#!/bin/bash
set -e
echo "Today is $(date)"
echo "--------------------"
sleep 1.5
echo "1. Pass env variables"
source env.sh
echo "2. Check the configuration of Packer template"
packer validate example.pkr.hcl
echo "3. Build the image"
#packer build example.pkr.hcl
Set an variable inside your script, then test for the presence of that variable at the top of the script. For example:
#!/bin/bash
if [ "${ALREADY_LOADED}" -ne "YES" ]; then
...
# Put commands here
...
ALREADY_LOADED=YES
fi
If you want this to persist to child processes, then use an environment variable instead of a local one, by exporting it. But be aware: Some things are not inherited by child processes. For example, if your script sets an array variable, the child will not be able to see the array. So you may want to leave some commands outside the if...then...fi clause.

Bash script exporting variables with if function

I got some not working script about checking if the variable is empty, and based on this create the next variable and export it.
export VARIABLE=macmac123
if [ -z "$VARIBLE" ]
then
unset $VAR2
else
export VAR2="test"
fi
Next I run this script ./script.sh and then running echo $VAR2 doesn't show anything. Any ideas? I can't change the whole script, because I need this, but it doesn't export anything into environmental variables.
Shell scripts are executed in a subshell. If you want the variables in the current shell you have to source the script like: source script.sh or . script.sh (dot is an alias for source).

How to export environment variable to background process run using sudo? [duplicate]

This question already has answers here:
Can I export a variable to the environment from a Bash script without sourcing it?
(13 answers)
How to keep environment variables when using sudo
(7 answers)
Closed 4 years ago.
I have a bash script env_setter.sh. It sets an environment variable. It contains:
#!/bin/bash
echo "inside env_setter script....."
export NAME="jake"
I have another script tester.sh. It tries to print the value of the environment variable set by env_setter.sh. It contains:
#!/bin/bash
echo "Inside tester script..."
echo "$NAME"
Then I have a script runner.sh that executes both of the above scripts as follow:
#!/bin/bash
echo "Inside runner script..."
. ./env_setter.sh
echo "$NAME"
sudo nohup ./tester.sh > demo.log 2>&1 & echo $! > save_pid.pid
Now when I run runner.sh, I get following output in demo.log:
inside env_setter script.....
jake
Inside tester script...
As we can see, the last echo command in tester.sh doesn't print anything. This is because the environment variable that we set with env_setter.sh is not being exported inside the context of tester.sh.
So, how do I export the environment variable to the background process in such cases?
Normally sudo replaces the current environment with the environment of the new user, for security reasons. Use sudo -E to preserve the calling environment. Or you can pass variables on the command line, sudo NAME=jake.

Cannot add environment variables in linux

For an entire day I have been tryng to add an environment variable to linux ad it isn't working. This is the guide that I am following. This is what is written in the .profile file.
# if running bash
if [ -n "$BASH_VERSION" ]; then
# include .bashrc if it exists
if [ -f "$HOME/.bashrc" ]; then
. "$HOME/.bashrc"
fi
fi
# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/bin" ] ; then
PATH="$HOME/bin:$PATH"
fi
JAVA_HOME=/usr/local/java/jdk1.8.0_51
JRE_HOME=$JAVA_HOME/jre
LARAVEL=/home/user/.composer/vendor/bin
PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin:$LARAVEL
export JAVA_HOME
export JRE_HOME
export LARAVEL
export PATH
I am running the file. But when I run echo $PATH I am not getting the $LARAVEL path. What am I doing wrong?
Have you logged out and logged in? Also try to update your .bashrc file instead. I think that .profile is only used for the ksh shell.
Try to source the file, like;
$ source yourfilename
In Unix/Linux (unlike in Windows) a program cannot affect the environment of the parent process. When you are running it as a regular shellscript it becomes a subprocess of the parent, and it will affect only it's own environment and not that of the parent.
Using the keyword source (or using the dot .) will instruct your process to executed the shell script directly rather than fork a new subprocess.
echo 'export PATH="$PATH":/path/to/folder/' >> ~/.bashrc
and
source ~/.bashrc

Bash script causing user to log out [duplicate]

This question already has answers here:
bash script: how to "exit" from sourced script, and allow to work non sourced?
(3 answers)
Closed 11 months ago.
In the script below, when the code inside the if statement is executed, the user gets logged out from bash. Why is this?
#!/bin/sh
if [ -z $1 ]; then
echo "1"
read w
exit 1
fi
if [ "$#" -gt "1" ]; then
echo "1"
read w
exit 2
fi
export PTSUSER=$1.
<some more code>
If the script is sourced, and either of the if blocks are hit, then yes the current users bash shell will be exited, and the user will be logged out. This is due to sourcing a script will use the current users bash shell to execute the script.
$ . ./script.sh
On the other hand if it's run in a new bash shell instance, then this won't happen, and the bash instance will exit once either of the exit commands are executed in the shell (in either of the if blocks).
$ bash ./script.sh
It depends on how you call the script. exit causes the current shell to exit. If you source the script from a shell started after a login, i.e. it is run in the context of the current shell, exit will log the user out.
If you run the script using source and want to end script without logging-out the user,
Use return

Resources