-1

I am creating a script to extract the data from one DB and inserting the rows to another DB using the sql loader. I have developed all the scripts and compiled in the .ksh file and need to run the file but before I need to set the environment for using the tnsnames.ora file. Earlier, I had been manually giving the setenv command to set environment but I tried to set using the command in the .ksh file

set TNS_ADMIN = /abc/tnsnames.ora; export TNS_ADMIN

but the environment is not getting set.

I have started the shell using #!/bin/ksh but no avail.

SHELL = /bin/csh
Sylvain Leroux
  • 50,096
  • 7
  • 103
  • 125
10hero
  • 15
  • 8
  • In a Bourne shell derivative (Korn shell, Bash, etc), the `set` command, when invoked as shown, sets `$1` to `TNS_ADMIN`, `$2` to `=` and `$3` to `/abc/tnsnames.ora`. The export exports an empty value for `TNS_ADMIN`. You would need to use `TNS_ADMIN=/abc/tnsnames.ora; export TNS_ADMIN` or `export TNS_ADMIN=/abc/tnsnames.ora` to set the environment. With C shell, you use `setenv` to set the environment. In general, what works in C shell won't work in Bash or Korn shell, and vice versa. The common syntax is limited — and doesn't include variable setting notations in particular. – Jonathan Leffler Jul 05 '15 at 23:41

2 Answers2

1

You can use that syntax:

#!/bin/ksh
...
export TNS_ADMIN=/abc/tnsnames.ora
...
someScript.csh
...

Note that you cannot set csh environment variables from a sourced ksh script, if it is what you are trying to do. The only way to set csh variables when the script (or your interactive csh shell) is already running is by sourcing a file that uses the csh syntax, not the ksh one, eg:

setenv TNS_ADMIN /abc/tnsnames.ora

Finally, unless you have strong reasons to keep it, I would advise you to replace your login shell csh by a POSIX shell like ksh or bash. csh and ksh do not mix together.

jlliagre
  • 29,783
  • 6
  • 61
  • 72
  • I already tried that but not working. Is there any way I set the csh environment in the script, launch the command and exit the environment for the remaining commands. – 10hero Jul 05 '15 at 02:10
  • It is unclear from your question and comment how you expect `csh` and `kshè to interact. Please update your question with a better description of what you want to do. – jlliagre Jul 05 '15 at 03:00
  • Why unclear? I have clearly mentioned that after coding the .ksh file, l am able to run export command but still the sql plus environment is not getting set unless I am manually executing the command setenv TNA_ADMIN /abc/tnsnames.ora – 10hero Jul 05 '15 at 10:06
  • What is unclear is precisely how you expect a `ksh` script to interact with a `csh` script, i.e. the sequence of scripts you run and which ones call which other ones, and how. Environment variables can only be set from the current script or inherited from a parent script/command. If what I suggested is not working with you, please explain where and how it fails. – jlliagre Jul 05 '15 at 10:20
0

You should write settings like set TNS_ADMIN = /abc/tnsnames.ora without spaces around the '='. When you have a file with such settings (let's call it setoracle.ksh), you must take care the file is sourced (read into the current environment, use a dot for that).
Now you have several approaches.

Start a ksh, source the settings and call your scripts

 ksh
 . setoracle.ksh
 extract_script

Start a script that will start the others.

#!/bin/ksh
if [ $# -ne 1 ]; then
   echo "Usage: $0 [extract|insert|...]"
   exit 1
fi
. setoracle.ksh
case $1 in
   "extract") extract_script;;
   "insert") insert_script;;
   *) echo "$1 not supported";
esac

Source the settings in all your existing scripts.
Start insert_script and extract_script with

#!/bin/ksh
. setoracle.ksh
Walter A
  • 19,067
  • 2
  • 23
  • 43