首先理清要了解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 #等價(jià)于 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 #等價(jià)于second
用mysql自帶數(shù)據(jù)庫(kù)world.city為例來(lái)展示處理查詢(xún)結(jié)果
#!/bin/sh
#filename:demo.sh
cityRes=""
cityColNum=5
function getCurValue(){
curValue=""
colIndex=$1
rowIndex=$2
idx=$[$cityColNum*$colIndex+$rowIndex-1] #通過(guò)行列進(jìn)行計(jì)算目標(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"`) #查詢(xún)結(jié)果以數(shù)組來(lái)保存,等價(jià)于上面的str2
curValue=`getCurValue $1 $2` #$1為行數(shù) $2為列數(shù)
echo $curValue
rowNum=`getCityRowNum` #獲取總行數(shù)
echo $rowNum
調(diào)用示例
注意的事項(xiàng)
getCityRowNum后的記錄數(shù)與實(shí)際的記錄數(shù)并不一致,這是由于city表Name 或者District字段中由于多個(gè)字符串組成,如:Andorra la Vella
這樣就會(huì)占用3個(gè)位置。
以上這篇shell簡(jiǎn)單處理mysql查詢(xún)結(jié)果的方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
您可能感興趣的文章:- HBASE 常用shell命令,增刪改查方法
- shell腳本實(shí)現(xiàn)mysql定時(shí)備份、刪除、恢復(fù)功能
- 用shell寫(xiě)一個(gè)mysql數(shù)據(jù)備份腳本
- Shell下實(shí)現(xiàn)免密碼快速登陸MySQL數(shù)據(jù)庫(kù)的方法
- MySQL數(shù)據(jù)庫(kù)的shell腳本自動(dòng)備份
- 通過(guò)shell腳本對(duì)mysql的增刪改查及my.cnf的配置