2013-02-21 126 views
2

當我運行下面的代碼,將數據存儲在變量部門得到來自於同一個變量的數據庫空間中的數據,testDate 和原料與材料成爲彼此的混合物中存在的數據,例如空間如果 「計算機科學」是該部門的名稱,然後「計算機」存儲在可變 department和「科學」存儲在testDate變量,但很明顯,我想 存儲整個「計算機科學」變量department也是數據變量
testDatetestTime因爲您在輸出中也可能觀察到問題。 我該如何解決這個問題?如何使用shell腳本

mysql -uroot -proot -Dproject_ivr_db -rN --execute "SELECT Dpartment,TestDate, 
TestTime FROM entrytests_datetime WHERE Discipline='msc'" | 
while read department testDate testTime 
do 

    echo "V,department=$department" 
    echo "V,testDate=$testDate" 
    echo "V,testTime=$testTime" 

done 

echo "E,resume" 

輸出:

V,department=computer 
    V,testDate=science 
    V,testTime=first february 2013 nine thirty a m 
    V,department=electronics 
    V,testDate=first 
    V,testTime=february 2013 ten thirty a m 
    E,resume 
+0

你的意思是使用參數或變量這樣的鏈接:http://stackoverflow.com/questions/10229324/pass-parameter-to-mysql-script-command-line ? – 2013-02-21 06:48:29

回答

1

基本上,你必須確保的字段由一個空格其他一些字符在輸出分開mysql,你必須告訴哪個字符外殼那是。

通過IFS變量告訴shell。

告訴mysql命令與--batch-B選項來完成的:

--batch,使用標籤作爲列分離器,具有在一個新行的每一行-B

打印結果。使用此選項,mysql不使用歷史文件。

在nontabular輸出格式批處理模式的結果和特殊字符逸出。通過使用原始模式可能會禁用轉義;請參閱--raw選項的說明。

所以:

IFS=" " # Tab (only) in quotes 
mysql -B -uroot -proot -Dproject_ivr_db -rN --execute "SELECT Dpartment,TestDate, 
TestTime FROM entrytests_datetime WHERE Discipline='msc'" | 
while read department testDate testTime 
do 

    echo "V,department=$department" 
    echo "V,testDate=$testDate" 
    echo "V,testTime=$testTime" 

done 

echo "E,resume" 
+0

感謝您的幫助 – shehzy 2013-02-21 07:42:12