简体   繁体   English

为什么有时无法识别子Shell中的DB2连接

[英]Why sometimes a DB2 connection in a subshell is not identified

I am writing the following script: 我正在编写以下脚本:

#!/bin/bash

db2 connect to andres
a=$(db2 connect)
echo $a

b=$(db2 connect && echo $?)
echo $b

c=$(db2 connect ; echo $?)
echo $c

d=$(db2 connect)
echo $d

What I am doing is to execute multiples commands inside a subshell by using the current established connection; 我正在做的是通过使用当前已建立的连接在子外壳中执行多个命令; however, the connection is only identified as connected when only a db2 command is issued. 但是,仅在发出db2命令时,该连接才被标识为已连接。 If I use a pipe or multiple commands in the subshell, the connection is not identified. 如果在子外壳程序中使用管道或多个命令,则无法识别连接。 Why? 为什么?

$ ./test

   Database Connection Information

 Database server        = DB2/LINUXX8664 10.5.5
 SQL authorization ID   = DB2INST1
 Local database alias   = ANDRES

Database Connection Information Database server = DB2/LINUXX8664 10.5.5 SQL authorization ID = DB2INST1 Local database alias = ANDRES
SQL1024N A database connection does not exist. SQLSTATE=08003
SQL1024N A database connection does not exist. SQLSTATE=08003 4
Database Connection Information Database server = DB2/LINUXX8664 10.5.5 SQL authorization ID = DB2INST1 Local database alias = ANDRES

As you can see, the connection is still active after the last statement. 如您所见,在最后一条语句之后,连接仍处于活动状态。

Excerpt from: https://www.ibm.com/support/knowledgecenter/en/SSEPGG_9.7.0/com.ibm.db2.luw.admin.cmd.doc/doc/r0010412.html 摘录自: https : //www.ibm.com/support/knowledgecenter/zh/SSEPGG_9.7.0/com.ibm.db2.luw.admin.cmd.doc/doc/r0010412.html

The first db2 invocation starts the back-end process. 第一次db2调用将启动后端进程。 All front-end processes with the same parent are serviced by a single back-end process, and therefore share a single database connection 具有相同父级的所有前端进程都由单个后端进程提供服务,因此共享一个数据库连接

In the cases of $b and $c they are running as single command (as pointed out by mustaccio), it fails because only pipe and redirect are supported in db2 CPL. 在$ b和$ c情况下,它们作为单个命令运行(如mustaccio所指出的),因为db2 CPL仅支持管道和重定向,所以它失败了。 To prove that your approach is still achievable, you may want to consider using here string in the variable assignments: 为了证明您的方法仍然可以实现,您可能需要考虑在变量分配中使用此处字符串:

b=$(echo $? <<< $(db2 connect))

or, display like all the others: 或者,像其他所有内容一样显示:

b=$(paste <(db2 connect) <(echo $?))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM