標籤:shell
在另一博文《Shell指令碼實現DB2資料庫表匯出到檔案》中實現了通過指令碼實現將DB2資料庫匯出到檔案,需要傳入七個參數,最後一個是一個帶有空白字元串,所以傳入的時候有點問題,會自動識別空格,預設會將空格前的當作第7個參數,以下是傳入的參數:
MD duanwf 23742 CDR_CALL_YYYYMMDD /home/duanwf/asiainfo/export/T141015001_20141014.avl & fetch first 100000 rows only
最後的“fetch first 100000 rows only”為第七個參數,但是識別的時候只擷取到fetch,日誌如下:
2014-10-15 17:51:59 [Thread-9] INFO com.asiainfo.dmp.proc.ExportDataServiceDB2.ExportData:70 - Run Command: /home/duanwf/workspace/shell2.sh MD duanwf 23742 CDR_CALL_YYYYMMDD /home/duanwf/asiainfo/export/T141015001_20141014.avl & fetch first 100000 rows only2014-10-15 17:51:59 [Thread-9] INFO com.asiainfo.dmp.proc.ExportDataServiceDB2.ExportData:78 - execute sql .................2014-10-15 17:51:59 [Thread-9] INFO com.asiainfo.dmp.proc.ExportDataServiceDB2.ExportData:78 - Begin to export the data: 2014-10-15 17:51:59 [Thread-9] INFO com.asiainfo.dmp.proc.ExportDataServiceDB2.ExportData:78 - \n2014/10/15-17:51:59 ====================connect to MD=======================2014-10-15 17:51:59 [Thread-9] INFO com.asiainfo.dmp.proc.ExportDataServiceDB2.ExportData:78 - \n2014/10/15-17:51:59 db2 connect to MD user duanwf 2014-10-15 17:52:01 [Thread-9] INFO com.asiainfo.dmp.proc.ExportDataServiceDB2.ExportData:78 - \n2014/10/15-17:52:01 Succeed connect to MD 2014-10-15 17:52:01 [Thread-9] INFO com.asiainfo.dmp.proc.ExportDataServiceDB2.ExportData:78 - \n2014/10/15-17:52:01 export to /home/duanwf/asiainfo/export/T141015001_20141014.avl of del modified by nochardel codepage=1208 COLDEL& select * from CDR_CALL_YYYYMMDD fetch:
那要怎麼處理呢?
之前問了有人說可以用雙引號把他引起來,最後結果還是一樣,無法拿到,只能擷取到
"fetch
還是會自動在空格前自動斷開。
可以通過[email protected]命令來處理,即將$7換成echo ${@:7},這樣將自動識別到的第7個開始,全部擷取到作為最後第7個參數,參數擷取改為:
#!/bin/bashDBSCHEMA=$1DBUSER=$2DBPASSWORD=$3TABLENAME=$4FILEPATH=$5DELIMITER=$6EXPORTLIMIT=`echo ${@:7}`
再次運行結果:
2014-10-15 23:36:05 [Thread-2] INFO com.asiainfo.dmp.proc.ExportDataServiceDB2.ExportData:57 - Export Parameters: MD duanwf 23742 ST_ZGD_SCOPE_RSFR_GR_DM_201409 /home/duanwf/asiainfo/export/M141015003_201409.avl & fetch first 100000 rows only2014-10-15 23:36:05 [Thread-2] INFO com.asiainfo.dmp.proc.ExportDataServiceDB2.ExportData:70 - Run Command: /home/duanwf/workspace/shell/db2.sh MD duanwf 23742 ST_ZGD_SCOPE_RSFR_GR_DM_201409 /home/duanwf/asiainfo/export/M141015003_201409.avl & fetch first 100000 rows only2014-10-15 23:36:05 [Thread-2] INFO com.asiainfo.dmp.proc.ExportDataServiceDB2.ExportData:78 - execute sql .................2014-10-15 23:36:05 [Thread-2] INFO com.asiainfo.dmp.proc.ExportDataServiceDB2.ExportData:78 - Begin to export the data: 2014-10-15 23:36:05 [Thread-2] INFO com.asiainfo.dmp.proc.ExportDataServiceDB2.ExportData:78 - \n2014/10/15-23:36:05 ====================connect to MD=======================2014-10-15 23:36:05 [Thread-2] INFO com.asiainfo.dmp.proc.ExportDataServiceDB2.ExportData:78 - \n2014/10/15-23:36:05 db2 connect to MD user duanwf 2014-10-15 23:36:08 [Thread-2] INFO com.asiainfo.dmp.proc.ExportDataServiceDB2.ExportData:78 - \n2014/10/15-23:36:08 Succeed connect to MD 2014-10-15 23:36:08 [Thread-2] INFO com.asiainfo.dmp.proc.ExportDataServiceDB2.ExportData:78 - \n2014/10/15-23:36:08 export to /home/duanwf/asiainfo/export/M141015003_201409.avl of del modified by nochardel codepage=1208 COLDEL& select * from ST_ZGD_SCOPE_RSFR_GR_DM_201409 fetch first 100000 rows only: 2014-10-15 23:36:09 [Thread-2] INFO com.asiainfo.dmp.proc.ExportDataServiceDB2.ExportData:78 - SQL3104N EXPORT 公用程式 正在開始將資料匯出至檔案2014-10-15 23:36:09 [Thread-2] INFO com.asiainfo.dmp.proc.ExportDataServiceDB2.ExportData:78 - "/home/duanwf/asiainfo/export/M141015003_201409.avl"。2014-10-15 23:36:09 [Thread-2] INFO com.asiainfo.dmp.proc.ExportDataServiceDB2.ExportData:78 - 2014-10-15 23:36:09 [Thread-2] INFO com.asiainfo.dmp.proc.ExportDataServiceDB2.ExportData:78 - SQL3105N Export 公用程式已經完成匯出 "12742" 行。2014-10-15 23:36:09 [Thread-2] INFO com.asiainfo.dmp.proc.ExportDataServiceDB2.ExportData:78 - 2014-10-15 23:36:09 [Thread-2] INFO com.asiainfo.dmp.proc.ExportDataServiceDB2.ExportData:78 - 2014-10-15 23:36:09 [Thread-2] INFO com.asiainfo.dmp.proc.ExportDataServiceDB2.ExportData:78 - 匯出的行數:12742
問題解決!!
<--------------------------------- 我是華麗的分割線 --------------------------------->
補充對Shell函數參數的說明:
來源:http://www.w3cschool.cc/linux/linux-shell-func.html
在Shell中,調用函數時可以向其傳遞參數。在函數體內部,通過 $n 的形式來擷取參數的值,例如,$1表示第一個參數,$2表示第二個參數...
帶參數的函數樣本:
#!/bin/bashfunWithParam(){ echo "The value of the first parameter is $1 !" echo "The value of the second parameter is $2 !" echo "The value of the tenth parameter is $10 !" echo "The value of the tenth parameter is ${10} !" echo "The value of the eleventh parameter is ${11} !" echo "The amount of the parameters is $# !" echo "The string of the parameters is $* !"}funWithParam 1 2 3 4 5 6 7 8 9 34 73
輸出結果:
The value of the first parameter is 1 !The value of the second parameter is 2 !The value of the tenth parameter is 10 !The value of the tenth parameter is 34 !The value of the eleventh parameter is 73 !The amount of the parameters is 12 !The string of the parameters is 1 2 3 4 5 6 7 8 9 34 73 !"
注意,$10 不能擷取第十個參數,擷取第十個參數需要${10}。當n>=10時,需要使用${n}來擷取參數。
另外,還有幾個特殊字元用來處理參數:
參數處理 |
說明 |
$# |
傳遞到指令碼的參數個數 |
$* |
以一個單字串顯示所有向指令碼傳遞的參數 |
$$ |
指令碼啟動並執行當前進程ID號 |
$! |
後台啟動並執行最後一個進程的ID號 |
[email protected] |
與$#相同,但是使用時加引號,並在引號中返回每個參數。 |
$- |
顯示Shell使用的當前選項,與set命令功能相同。 |
$? |
顯示最後命令的退出狀態。0表示沒有錯誤,其他任何值表明有錯誤。 |
本文出自 “Forever Love” 部落格,請務必保留此出處http://dwf07223.blog.51cto.com/8712758/1564600
Shell指令碼傳遞帶有空格的參數