shell简单处理mysql查询结果的方法
首先理清要了解shell脚本的数组与字符串的一些特性:
str=("hello""world""!")#结果:str:3#普通的字符串数组 echo"str:"${#str[@]} str1=("helloworld!")#结果:str1:1#普通的字符串数组 echo"str1:"${#str1[@]} str2=(`echo"Helloworld!"`)#结果:str2:3#等价于str echo"str2:"${#str2[@]} functionstrDeal(){ param=("$@") echo${param[@]} echo$1 echo$2 echo$3 } echo"-----------first----------------" strDeal"Helloworld!" echo"-----------second----------------" strDeal"Hello""world""!" echo"-----------third----------------" strDeal$str1#等价于second
用mysql自带数据库world.city为例来展示处理查询结果
#!/bin/sh #filename:demo.sh cityRes="" cityColNum=5 functiongetCurValue(){ curValue="" colIndex=$1 rowIndex=$2 idx=$[$cityColNum*$colIndex+$rowIndex-1]#通过行列进行计算目标位置 if[$idx-le${#cityRes[@]}];then echo${cityRes[$idx]}#获取目标结果 fi } #获取city表总行数 functiongetCityRowNum(){ echo$[${#cityRes[@]}/$cityColNum-1] } cityRes=(`mysql-uroot-p123456world-e"select*fromcity"`)#查询结果以数组来保存,等价于上面的str2 curValue=`getCurValue$1$2`#$1为行数$2为列数 echo$curValue rowNum=`getCityRowNum`#获取总行数 echo$rowNum
调用示例
shdemo.sh12
注意的事项
getCityRowNum后的记录数与实际的记录数并不一致,这是由于city表Name或者District字段中由于多个字符串组成,如:AndorralaVella
这样就会占用3个位置。
以上这篇shell简单处理mysql查询结果的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。