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

时间:2016-03-09 13:13:17

标签: bash shell db2

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

file1.sh包含DB2查询,查询返回employee表中的员工总数。

现在我想将员工总数分配到文件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

1 个答案:

答案 0 :(得分:0)

使用以下两个脚本 driver.sh child.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 ${1}"`

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
相关问题