If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > How to execute bash script from Linux and get db2 values from executed command shell?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-12-09, 14:27
grofaty grofaty is offline
Registered User
 
Join Date: Jan 2003
Posts: 1,570
How to execute bash script from Linux and get db2 values from executed command shell?

Hi,
I would like to connect to db2 from command prompt and execute batch file which will get result from my connection.

Windows:
1. I open text editor and paste the following text to it:
Code:
db2 connect | find "Local"
2. Save file test.bat
3. Open DB2 Command Window
4. Connect to sample database from command prompt
Code:
db2 connect to sample
5. Execute test.bat file:
Code:
test.bat
I get output:
Local database alias = SAMPLE

OK, Windows works fine, but the problem is on Linux.

Linux:
Doing the same thing in Linux:
1. I open text editor and paste the following text to it (bash is my default shell):
Code:
db2 connect | grep Local
2. Save file test.sh
3. Make file executable:
Code:
chmod 700 test.sh
4. Connect to sample database from shell
Code:
db2 connect to sample
5. Execute test.sh file:
Code:
./test.sh
I get no! output. But executing the same command (from step 1) I get:
Local database alias = SAMPLE

It looks like Linux opens new process when executing batch file.

Is there any way I could get info when executing shell file from shell just like on Windows?

My system:
db2 v8.2 on Windows and Linux
Reply With Quote
  #2 (permalink)  
Old 01-12-09, 16:45
n_i n_i is online now
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,448
Try "source ./test.sh" or ". ./test.sh" (note there's a space between the two dots in the second example. These should execute in the same shell.
Reply With Quote
  #3 (permalink)  
Old 01-13-09, 08:27
grofaty grofaty is offline
Registered User
 
Join Date: Jan 2003
Posts: 1,570
Hi,
thanks a lot. It works fine. Excellent.
Regards
Reply With Quote
  #4 (permalink)  
Old 01-14-09, 13:11
grofaty grofaty is offline
Registered User
 
Join Date: Jan 2003
Posts: 1,570
Hi,
any idea how to do this in Python programming language?

Python code looks like this:
Code:
#!/usr/bin/python
import os
os.system('db2 connect')
but when executing "./test.py" from shell it opens new system process, so I get db2 error:
SQL1024N A database connection does not exist. SQLSTATE=08003

Executing with:
"source ./test.py" (without double quotes)
I get error: syntax error near unexpected token `'db2 connect''

Any idea how to execute Python code from current process?

Regards

Last edited by grofaty; 01-14-09 at 13:17.
Reply With Quote
  #5 (permalink)  
Old 01-14-09, 16:37
n_i n_i is online now
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,448
Short answer: you can't.

Long answer: you could, if Python were your command shell and if it had a command similar to the bash "source" command. "source" (or ".") tells bash not to invoke another shell but interpret commands in the specified file within the current shell. To execute a Python script you must call another program (the Python interpreter), which on a Unix or Linux system creates a separate process environment.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On