1

I connected to the DB2 instance, and I can successfully verify that the connection exists:

db2inst1@hostname:/tmp$ db2 connect

   Database Connection Information

 Database server        = DB2/LINUXX8664 10.5.3
 SQL authorization ID   = DB2INST1
 Local database alias   = DBNAME

However, when I run the following script

db2inst1@hostname:/tmp$ cat test.sh
#!/bin/bash 
db2 connect

the connection seems to disappear:

db2inst1@hostname:/tmp$ ./test.sh 
SQL1024N  A database connection does not exist.  SQLSTATE=08003

I wonder,

  • What is reason for that?
  • How to work in scripts with the existing connection?
4
  • Is bash the shell you're working in when you execute the command in-shell?
    – Philᵀᴹ
    Jun 8, 2017 at 9:23
  • @Philᵀᴹ, yes, I execute from bash. The following command also does not work: bash ./test.sh Jun 8, 2017 at 9:28
  • 2
    A connection is local to its subshell; ./test.sh runs in its own subshell.
    – mustaccio
    Jun 8, 2017 at 11:15
  • 1
    Is there a problem making the connection from within the script? Jun 8, 2017 at 14:32

1 Answer 1

1

As @mustaccio points out in the comment, a connection is local to its subshell.

In order to call the shell script in the context of the current shell, source could be used:

db2inst1@hostname:/tmp$ source test.sh

   Database Connection Information

 Database server        = DB2/LINUXX8664 10.5.3
 SQL authorization ID   = DB2INST1
 Local database alias   = DBNAME

Of course, . also works:

db2inst1@hostname:/tmp$ . test.sh

   Database Connection Information

 Database server        = DB2/LINUXX8664 10.5.3
 SQL authorization ID   = DB2INST1
 Local database alias   = DBNAME

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.