2019-04-25 11:00:55 2694瀏覽
本篇文章扣丁學(xué)堂Linux培訓(xùn)小編給讀者們分享一下shell簡單處理mysql查詢結(jié)果的方法,文中列出代碼供讀者們參考,對Linux開發(fā)技術(shù)感興趣的小伙伴就隨小一起來了解一下吧。
首先理清要了解shell腳本的數(shù)組與字符串的一些特性:
str=("hello" "world" "!") #結(jié)果: str: 3 #普通的字符串?dāng)?shù)組 echo "str: " ${#str[@]} str1=("hello world !") #結(jié)果: str1: 1 #普通的字符串?dāng)?shù)組 echo "str1: "${#str1[@]} str2=(`echo "Hello world !"`) #結(jié)果: str2: 3 #等價于 str echo "str2: " ${#str2[@]} function strDeal(){ param=("$@") echo ${param[@]} echo $1 echo $2 echo $3 } echo "-----------first----------------" strDeal "Hello world !" echo "-----------second----------------" strDeal "Hello" "world" "!" echo "-----------third----------------" strDeal $str1 #等價于second
用mysql自帶數(shù)據(jù)庫world.city為例來展示處理查詢結(jié)果
#!/bin/sh #filename:demo.sh cityRes="" cityColNum=5 function getCurValue(){ curValue="" colIndex=$1 rowIndex=$2 idx=$[$cityColNum*$colIndex+$rowIndex-1] #通過行列進(jìn)行計算目標(biāo)位置 if [ $idx -le ${#cityRes[@]} ] ;then echo ${cityRes[$idx]} #獲取目標(biāo)結(jié)果 fi } #獲取city表總行數(shù) function getCityRowNum(){ echo $[${#cityRes[@]}/$cityColNum-1] } cityRes=(`mysql -uroot -p123456 world -e "select * from city"`) #查詢結(jié)果以數(shù)組來保存,等價于上面的str2 curValue=`getCurValue $1 $2` #$1為行數(shù) $2為列數(shù) echo $curValue rowNum=`getCityRowNum` #獲取總行數(shù) echo $rowNum
調(diào)用示例
sh demo.sh 1 2
注意的事項
getCityRowNum后的記錄數(shù)與實(shí)際的記錄數(shù)并不一致,這是由于city表Name 或者District字段中由于多個字符串組成,如:Andorra la Vella
這樣就會占用3個位置。
以上就是扣丁學(xué)堂Linux在線學(xué)習(xí)小編給大家分享的shell簡單處理mysql查詢結(jié)果的方法,希望對小伙伴們有所幫助,想要了解更多內(nèi)容的小伙伴可以登錄扣丁學(xué)堂官網(wǎng)咨詢。
想要學(xué)好Linux開發(fā)小編給大家推薦口碑良好的扣丁學(xué)堂,扣丁學(xué)堂有專業(yè)老師制定的Linux學(xué)習(xí)路線圖輔助學(xué)員學(xué)習(xí),此外還有與時俱進(jìn)的Linux課程體系和Linux視頻教程供大家學(xué)習(xí),想要學(xué)好Linux開發(fā)技術(shù)的小伙伴快快行動吧。扣丁學(xué)堂Linux技術(shù)交流群:422345477。
【關(guān)注微信公眾號獲取更多學(xué)習(xí)資料】 【掃碼進(jìn)入HTML5前端開發(fā)VIP免費(fèi)公開課】
查看更多關(guān)于“Linux培訓(xùn)資訊”的相關(guān)文章>>