1

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 !!!

1
  • 2
    Note while [ 1 -gt 0 ] can be written directly to while :. See “while :” vs. “while true” for a good discussion about the topic. Commented Jan 5, 2015 at 12:07

3 Answers 3

7

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.

Sign up to request clarification or add additional context in comments.

5 Comments

I am using bash shell. I just ran first one as you told but still no luck
from your command line you have to start your script with source ... and you have to modify your script in that way that it also uses source ... where you call a subscript.
Thanks klaus... I ran the script as " source name.sh " in current shell. Then it asked for a path and then displayed oracle home set successfully. I am new for this could u plz explain little bit where i am wrong.
As I see you have a line ./oracle_env.sh ... which is a shellscript I expect. This script will be started in another shell. So you have to change this to source ./oracle_env.sh ....
Hi klaus i tried the same but not getting what i need as output. As u said " execute a script it starts in a new shell, sets the environment there and close the shell. ", it solved my problem. Thanks a lot.
1

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.

Comments

0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.