tags:

views:

210

answers:

3

My ruby script requires connection to an Oracle database. So I need to export ORACLE_HOME and LD_LIBRARY_PATH correctly before the script would run. Is there a way that I can export those env variables without using shell script? I tried to put ENV['ORACLE_HOME'] = '/usr/local/oracle_client' at the first line of the script and it doesn't work. Now the only way it would work is to write a shell script, where export those variables and then run ruby there. The shell script looks like:

export ORACLE_HOME='/usr/local/oracle_client' export LD_LIBRARY_PATH='/usr/local/oracle_client/lib' ruby myscript.rb --options

It's kinda ugly because user has to go inside the shell script to change options. I'm wondering whether there's a better way of doing it. So user can just do at command line: ruby myscript.rb --options

A: 

Have a look here.

khelll
I did read that, but it doesn't work for my purpose. It has to set the environment and then launch another script from within the script. It kinda work the same way as the shell script I mentioned above. Users have to go inside the code to change any option if they want to run it.
swingfuture
+1  A: 

Why not supply the ruby options as arguments to the shell script? E.g.,

#!/bin/bash    
export ORACLE_HOME='/usr/local/oracle_client' 
export LD_LIBRARY_PATH='/usr/local/oracle_client/lib' 
ruby myscript.rb $*

Obviously you may want to add argument reasonableness checks, etc., but this gives the idea.

DCookie
Cause he is not the consumer of his script, so it's better to save the clinets such conf...
khelll
What client configuration do you mean? OP said the script was ugly because the user has to modify script to supply options. This answer addresses that concern. It gives a command line solution no more complex than running the ruby command. It also adds value, as default options and validation can be done in the shell script.
DCookie
It does work, thanks DCookie.
swingfuture
+1  A: 

Why not call it out via Kernel.system?

system("export ORACLE_HOME='/usr/local/oracle_client'")
system("export LD_LIBRARY_PATH='/usr/local/oracle_client/lib'")
hgimenez
You mean I put those lines at top of the script? I tried, but when I printed the env variable (puts ENV['ORACLE_HOME']), they're still empty.
swingfuture
It doesn't work because, as the documentation for the kernel.system says: "Executes cmd in a subshell, ...". As soon as the system call completes, the subshell exits (hence it's environment is gone), and you're back to square one.
DCookie
What you really need is something akin to Perl's Env module, but I know zip about ruby so I can't say if there's anything available along those lines...
DCookie