0

I want to do same commande shell CLi into a script

Command shell without script (CLI)

su - oracle
export ORAENV_ASK=NO
export ORACLE_SID=HRPRD
. oraenv
env | grep ora

output:

USER=oracle
**LD_LIBRARY_PATH**=/u01/pe/oracle/u/25/bas/lib
ORACLE_BASE=/u01/pe/oracle
MAIL=/var/spool/mail/oracle
PATH=/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/u01/pe/oracle/u/25/bas
PWD=/oracle
HOME=/oracle
LOGNAME=oracle
**ORACLE_HOME**=/u01/pe/oracle/u/25/bas/

My script

rr=`su - oracle`
export ORAENV_ASK=NO
export ORACLE_SID=MySID
uu=`. oraenv`
searchOraHome= `env | grep ora`
echo $searchOraHome

output:

USER=oracle ORACLE_BASE=/u01/app/oracle 
PATH=/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/oracle/bin 
MAIL=/var/spool/mail/oracle PWD=/oracle HOME=/oracle LOGNAME=oracle

Why are the variables ORACLE_HOME and LD_LIBRARY_PATH not present with the script shell?

Is the line:

uu=`. oraenv`

... not correct?

John K. N.
18.9k14 gold badges56 silver badges117 bronze badges
asked Jan 28, 2020 at 10:53
1
  • i change a ligne of my script with . oraenv instead of uu=. oraenv it works with a script ./test.sh but crontab no i think maybe because of PATH=/sbin:/bin:/usr/sbin:/usr/bin instead of PATH=/sbin:/bin:/usr/sbin:/usr/bin:/oracle/bin Commented Jan 28, 2020 at 12:17

4 Answers 4

2

If you us a shell to execute a command in backticks (`), it is executed in a subshell, so all the environment variables it sets are only set in the subshell and are gone when the subshell exits.

You have to "source" the script with

. oraenv

without the backticks.

answered Jan 28, 2020 at 16:07
1

It looks like oraenv isn't actually being executed successfully. Are you sure that oraenv is in the $PATH of the script when it's being called?

You can check where oraenv is by running:

which oraenv

from your command prompt.

Assuming you're trying to run this from crontab, you can then check the $PATH in that environment by adding:

* * * * * env > /tmp/environment.txt

to your crontab, letting it run, removing the crontab entry and then checking the contents of this file.

(or * * * * * echo $PATH > /emp/path.txt - but env is fewer characters)

answered Jan 28, 2020 at 14:13
0

when you run a script from your command prompt it takes the variables from your current session but when you execute it with cron we need to provide the variables

Therefore the difference

answered Jan 28, 2020 at 14:26
-2

I never liked the default oraenv script or the coraenv scripts. so I wrote my own. I use grep and cut to pull out the sid that I am looking for as well as the Oracle home from the /etc/oratab file. I can then set any other environment variables that I want. Things such as email addresses for shell scripts, backup directory, any aliases that I want to use etc. The Oracle provided scripts work ok, I just like being able to control how setting the environment variables works.

answered Jan 28, 2020 at 13:57
2
  • 4
    And how does this answer the question? Commented Jan 28, 2020 at 13:58
  • If the question is, "I want to do same commande shell CLi into a script", then writing a script that replaces the oraenv/coraenv scripts would provide an answer to the question. If you don't like something that Oracle provides, you can always write your own code. Should I have provided a working script for setting environment variables, or should I just provide some parameters for writing a script that can be used by the OP? Commented Jan 28, 2020 at 16:10

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.