将查询结果分配给变量并从其他文件访问它

Assign query result to variable and access it from other file

我有两个文件,即 file1.shfile2.sh

file1.sh包含DB2查询,查询return员工总数table。

现在我想将员工总数分配给文件 file1.sh 中的一个变量。

文件 1:

#!/bin/bash
#database connection goes here
echo The total number employees: 
db2 -x "select count(*) from employee"

当我运行上面的文件显示员工总数。

但是

我想将该总数存储到某个变量中,并希望它从另一个文件 file2.sh.

访问

文件 2:

#!/bin/bash
#Here i want to use total number of employees 
#Variable to be accessed here

使用以下两个脚本,driver.shchild.sh:

driver.sh

#!/bin/bash

cnt=`./child.sh syscat.tables`
echo "Number of tables:  ${RESULT}"
cnt=`./child.sh syscat.columns`
echo "Number of columns:  ${RESULT}"

child.sh

#!/bin/bash
db2 connect to pocdb > /dev/null 2>&1

cnt=`db2 -x "select count(*) from "`

db2 connect reset > /dev/null 2>&1
db2 terminate > /dev/null 2>&1

echo ${cnt}

结果

[db2inst1@dbms stack]$ ./driver.sh 
Number of tables:  474
Number of columns:  7006

[db2inst1@dbms stack]$ ./child.sh syscat.columns
7006