Results 1 to 5 of 5
  1. #1
    Join Date
    Jan 2003
    Posts
    1,605

    Unanswered: 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

  2. #2
    Join Date
    Jun 2003
    Location
    Toronto, Canada
    Posts
    5,516
    Provided Answers: 1
    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.
    ---
    "It does not work" is not a valid problem statement.

  3. #3
    Join Date
    Jan 2003
    Posts
    1,605
    Hi,
    thanks a lot. It works fine. Excellent.
    Regards

  4. #4
    Join Date
    Jan 2003
    Posts
    1,605
    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 14:17.

  5. #5
    Join Date
    Jun 2003
    Location
    Toronto, Canada
    Posts
    5,516
    Provided Answers: 1
    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.
    ---
    "It does not work" is not a valid problem statement.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •