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?
4 Answers 4
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.
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)
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
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.
-
4And how does this answer the question?Balazs Papp– Balazs Papp2020年01月28日 13:58:58 +00:00Commented 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?Gandolf989– Gandolf9892020年01月28日 16:10:07 +00:00Commented Jan 28, 2020 at 16:10
. 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