Bash中管道会致使数据库链接信息丢失

Bash中管道会致使数据库链接信息丢失
简化问题

说明该问题前,咱们先把它简单化。数据库

有一个sample data,tab.lst:rest

tab1,col1
tab2,col2
...

如下两段代码的输出是一致的。code

while IFS=, read -r tab col
do
  echo "$tab     $col"
done < tab.lst

cat tab.lst | while IFS=, read -r tab col
do
  echo "$tab     $col"
done
加入实际需求

咱们指望从一个文本文件中取出表名和列名,而后查询该列的最大值,并重设列生成值的起始。it

咱们把需求分别填充到上面的两段代码中。获得以下两段代码:table

db2 connect to <db_name> user <user_name> using xxxxxx
while IFS=, read -r tab col
do
  echo "$tab     $col"
  max_id=$(db2 -x "select max($col)+1 from $tab")

  db2 -v alter table $tab alter column $col set generated by default restart with $max_id
done < tab.lst

# 在Bash中,这一段代码没有像咱们所指望的那样正常执行,而是提示“没有数据库链接”
db2 connect to <db_name> user <user_name> using xxxxxx
cat tab.lst | while IFS=, read -r tab col
do
  echo "$tab     $col"
  max_id=$(db2 -x "select max($col)+1 from $tab")

  db2 -v alter table $tab alter column $col set generated by default restart with $max_id
done

很遗憾,第二段代码在Bash中不能正常执行,提示“没有数据库链接”,而在Ksh中可正常执行。select

而这两段代码惟一的区别仅仅在于第二段代码在链接数据库后使用了管道。管道致使数据库链接丢失。数据

相关文章
相关标签/搜索