通过 bash 脚本将 DB2 输出到变量
DB2 Output to Variable via bash script
我希望有人可以帮助将 db2 命令的输出应用到一个变量,以便稍后在脚本中使用。
到目前为止我在...
db2 "connect to <database> user <username> using <password>"
while read HowMany ;
do
Counter=$HowMany
echo $HowMany
done < <(db2 -x "SELECT COUNT(1) FROM SYSCAT.COLUMNS WHERE TABNAME = 'TableA' AND TABSCHEMA='SchemaA' AND GENERATED = 'A'")
当试图在 while 循环之外引用 $Counter
时,它 returns SQL1024N A database connection does not exist. SQLSTATE=08003
和 echo $HowMany
一样
我试过另一种使用管道的方法,它使 $HowMany
显示正确的值,但由于它是一个子 shell,它后来丢失了。
我宁愿不使用临时文件并尽可能删除它们,因为我不喜欢脚本在任何时候中止时留下的文件。
如您所见,一个 shell 中的 DB2 连接对子 shell 不可用。您可以使用 sub-shell,但您必须将 CONNECT
语句放在该 sub-shell.
中
所以它更像是一个简单的重写,不要使用子shell:
db2 "connect to <database> user <username> using <password>"
db2 -x "SELECT COUNT(1) FROM SYSCAT.COLUMNS WHERE TABNAME = 'TableA' AND TABSCHEMA='SchemaA' AND GENERATED = 'A'" | while read HowMany ; do
Counter=$HowMany
echo $HowMany
done
Linux 和 UNIX 上的 DB2 CLP 可以处理 command substitution 而不会丢失其数据库连接上下文,从而可以将查询结果捕获到本地 shell 变量中或将其视为内联文本块。
#!/bin/sh
# This script assumes the db2profile script has already been sourced
db2 "connect to <database> user <username> using <password>"
# Backtick command substitution is permitted
HowMany=`db2 -x "SELECT COUNT(1) FROM SYSCAT.COLUMNS WHERE TABNAME = 'TableA' AND TABSCHEMA='SchemaA' AND GENERATED = 'A'"`
# This command substitution syntax will also work
Copy2=$(db2 -x "SELECT COUNT(1) FROM SYSCAT.COLUMNS WHERE TABNAME = 'TableA' AND TABSCHEMA='SchemaA' AND GENERATED = 'A'")
# One way to get rid of leading spaces
Counter=`echo $HowMany`
# A while loop that is fed by process substitution cannot use
# the current DB2 connection context, but combining a here
# document with command substitution will work
while read HowMany ;
do
Counter=$HowMany
echo $HowMany
done <<EOT
$(db2 -x "SELECT COUNT(1) FROM SYSCAT.COLUMNS WHERE TABNAME = 'TableA' AND TABSCHEMA='SchemaA' AND GENERATED = 'A'")
EOT
我希望有人可以帮助将 db2 命令的输出应用到一个变量,以便稍后在脚本中使用。
到目前为止我在...
db2 "connect to <database> user <username> using <password>"
while read HowMany ;
do
Counter=$HowMany
echo $HowMany
done < <(db2 -x "SELECT COUNT(1) FROM SYSCAT.COLUMNS WHERE TABNAME = 'TableA' AND TABSCHEMA='SchemaA' AND GENERATED = 'A'")
当试图在 while 循环之外引用 $Counter
时,它 returns SQL1024N A database connection does not exist. SQLSTATE=08003
和 echo $HowMany
我试过另一种使用管道的方法,它使 $HowMany
显示正确的值,但由于它是一个子 shell,它后来丢失了。
我宁愿不使用临时文件并尽可能删除它们,因为我不喜欢脚本在任何时候中止时留下的文件。
如您所见,一个 shell 中的 DB2 连接对子 shell 不可用。您可以使用 sub-shell,但您必须将 CONNECT
语句放在该 sub-shell.
所以它更像是一个简单的重写,不要使用子shell:
db2 "connect to <database> user <username> using <password>"
db2 -x "SELECT COUNT(1) FROM SYSCAT.COLUMNS WHERE TABNAME = 'TableA' AND TABSCHEMA='SchemaA' AND GENERATED = 'A'" | while read HowMany ; do
Counter=$HowMany
echo $HowMany
done
Linux 和 UNIX 上的 DB2 CLP 可以处理 command substitution 而不会丢失其数据库连接上下文,从而可以将查询结果捕获到本地 shell 变量中或将其视为内联文本块。
#!/bin/sh
# This script assumes the db2profile script has already been sourced
db2 "connect to <database> user <username> using <password>"
# Backtick command substitution is permitted
HowMany=`db2 -x "SELECT COUNT(1) FROM SYSCAT.COLUMNS WHERE TABNAME = 'TableA' AND TABSCHEMA='SchemaA' AND GENERATED = 'A'"`
# This command substitution syntax will also work
Copy2=$(db2 -x "SELECT COUNT(1) FROM SYSCAT.COLUMNS WHERE TABNAME = 'TableA' AND TABSCHEMA='SchemaA' AND GENERATED = 'A'")
# One way to get rid of leading spaces
Counter=`echo $HowMany`
# A while loop that is fed by process substitution cannot use
# the current DB2 connection context, but combining a here
# document with command substitution will work
while read HowMany ;
do
Counter=$HowMany
echo $HowMany
done <<EOT
$(db2 -x "SELECT COUNT(1) FROM SYSCAT.COLUMNS WHERE TABNAME = 'TableA' AND TABSCHEMA='SchemaA' AND GENERATED = 'A'")
EOT