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

Chankey Pathak
  • 21,187
  • 12
  • 85
  • 133
Prabhakar
  • 237
  • 1
  • 3
  • 10
  • 2
    Note `while [ 1 -gt 0 ]` can be written directly to `while :`. See [“while :” vs. “while true”](http://stackoverflow.com/q/10797835/1983854) for a good discussion about the topic. – fedorqui Jan 05 '15 at 12:07

3 Answers3

6

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.

Klaus
  • 24,205
  • 7
  • 58
  • 113
  • I am using bash shell. I just ran first one as you told but still no luck – Prabhakar Jan 05 '15 at 12:13
  • 1
    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. – Klaus Jan 05 '15 at 12:14
  • 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. – Prabhakar Jan 05 '15 at 12:22
  • 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 ...`. – Klaus Jan 05 '15 at 12:24
  • 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. – Prabhakar Jan 05 '15 at 13:50
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.

Gaurav Dave
  • 6,838
  • 9
  • 25
  • 39
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
Sriharsha Kalluru
  • 1,743
  • 3
  • 21
  • 27