標籤:style blog http color io os java ar 檔案
該Shell指令碼用於實現將DB2資料庫表匯出到檔案,將在另一篇博文《Java代碼調用Shell指令碼並傳入參數實現DB2資料庫表匯出到檔案》中通過Java代碼實現調用該指令碼並傳入參數。
1 #!/usr/bin/env sh 2 3 DBSCHEMA=$1 4 DBUSER=$2 5 DBPASSWORD=$3 6 TABLENAME=$4 7 FILEPATH=$5 8 DELIMITER=$6 9 EXPORTLIMIT=$710 11 SQLERR="NO ERROR MSG"12 13 ############################################################14 # : wlog String 15 ############################################################16 wlog () {17 wlog_dt=`date "+%Y/%m/%d-%H:%M:%S" `18 echo "\n${wlog_dt} $1"19 }20 21 ############################################################22 # : db2connect db2connstring23 ############################################################24 connDB2() {25 wlog "====================connect to $1=======================" 26 wlog "db2 connect to $1 user $2 "27 ## conn=db2 connect to $1 user $2 using $3 > /dev/null28 if( db2 connect to $1 user $2 using $3 > /dev/null )29 then30 wlog "Succeed connect to $1 "31 else32 wlog "Failed connect to $1 "33 exit -134 fi35 }36 37 ############################################################38 # : db2connectRelease db2connstring39 ############################################################40 releaseDB2() {41 db2 connect reset > /dev/null42 }43 44 ############################################################45 # : db2export46 ############################################################47 exportDB2() {48 connDB2 ${DBSCHEMA} ${DBUSER} ${DBPASSWORD}49 sql=" select * from ${TABLENAME} ${EXPORTLIMIT}"50 wlog "export to ${FILEPATH} of del modified by codepage=1208 COLDEL| ${sql}: "51 db2 "export to ${FILEPATH} of del modified by nochardel codepage=1208 COLDEL| ${sql}"52 }53 54 ############################################################55 # : main56 ############################################################57 run() {58 # connDB2 ${DBNODE} ${DBUSER} ${DBPASSWORD} ${DBNAME}59 # sql=" select inter_no,op_time from inter_log "60 61 # db2 -x ${sql}| while read inter_no op_time62 # do63 # echo "Result:${inter_no}->${op_time}"64 # done65 66 echo "Begin to export the data: "67 exportDB268 69 echo "Close the connection."70 releaseDB271 }72 73 echo "execute sql ................."74 run
其中以下匯出命令將會去除匯出檔案中字串的雙引號:
1 db2 "export to ${FILEPATH} of del modified by nochardel codepage=1208 COLDEL| ${sql}"
COLDEL指定了分隔字元為|
如果需要保留字元串雙引號命令:
1 db2 "export to ${FILEPATH} of del modified by codepage=1208 COLDEL| ${sql}"
Shell指令碼實現DB2資料庫表匯出到檔案