实例讲解一个shell处理db2数据库的程序
#SQL文定义SQL='SELECT AAA, BBB, CCC FROM MYTBL1'#执行SQLSDATA=`db2 '$SQL'`#返回值判断if [ $? -ne 0 ]then#显示db2返回的错误信息echo '$SDATA'exit 1fi#对取得的数据进行处理。echo '$SDATA' | sed -e '4,/^$/!d;/^$/d' |while read AAA BBB CCCdo echo 'AAA IS $AAA, BBB IS $BBB, CCC IS $CCC'done#取得数据件数echo '$SDATA' | sed -n -e '/^$/{1,3d;n;s/[^0-9]*([0-9]*)[^0-9]*/1/;p;}' | read CNTecho 'The count of selected data is $CNT.'exit 0
◆更新db2的数据,并取得更新结果
SQL='UPDATE MYTBL1 SET AAA='2005',BBB='05',CCC='12''#执行SQLSDATA=`db2 -a '$SQL'`#取得SQLCODEecho '$SDATA' | sed -n -e 's/^.*sqlcode: ([-,0-9][0-9]*).*/1/p' | read SQLCODEecho 'Sqlcode is $SQLCODE.'#取得SQLSTATEecho '$SDATA' | sed -n -e 's/^.*sqlstate: ([-,0-9][0-9]*).*/1/p' | read SQLSTATEecho 'Sqlstate is $SQLSTATE.'#取得更新件数(即sqlerrd的第三个值)echo '$SDATA' | sed -n -e '/sqlerrd/s/^.*(3) ([-,0-9][0-9]*).*/1/p' | read UPDCNTecho 'Updated data's count is $UPDCNT.'#取得sqlerrd的第五个值echo '$SDATA' | sed -n -e '/sqlerrd/{n;s/^.*(5) ([-,0-9][0-9]*).*/1/;p;}' | read SQLERRD5echo 'Sqlerrd(5) is $SQLERRD5.'