shell简单处理mysql查询结果的方法
首先理清要了解shell脚本的数组与字符串的一些特性:
str=("hello" "world" "!") #结果: str: 3 #普通的字符串数组
echo "str: " ${#str[@]}
str1=("hello world !") #结果: str1: 1 #普通的字符串数组
echo "str1: "${#str1[@]}
str2=(`echo "Hello world !"`) #结果: 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自带数据库world.city为例来展示处理查询结果
#!/bin/sh
#filename:demo.sh
cityRes=""
cityColNum=5
function getCurValue(){
curValue=""
colIndex=$1
rowIndex=$2
idx=$[$cityColNum*$colIndex+$rowIndex-1] #通过行列进行计算目标位置
if [ $idx -le ${#cityRes[@]} ] ;then
echo ${cityRes[$idx]} #获取目标结果
fi
}
#获取city表总行数
function getCityRowNum(){
echo $[${#cityRes[@]}/$cityColNum-1]
}
cityRes=(`mysql -uroot -p123456 world -e "select * from city"`) #查询结果以数组来保存,等价于上面的str2
curValue=`getCurValue $1 $2` #$1为行数 $2为列数
echo $curValue
rowNum=`getCityRowNum` #获取总行数
echo $rowNum调用示例
sh demo.sh 1 2
注意的事项
getCityRowNum后的记录数与实际的记录数并不一致,这是由于city表Name 或者District字段中由于多个字符串组成,如:Andorra la Vella
这样就会占用3个位置。
相关推荐
laisean 2020-11-11
Julyth 2020-10-16
laisean 2020-09-27
flycappuccino 2020-09-27
liguojia 2020-09-27
tvk 2020-07-30
Zaratustra 2020-06-26
IsanaYashiro 2020-06-16
MXstudying 2020-09-05
WasteLand 2020-09-15
<?php. if (!empty($_POST)) {. $data1 = $_POST["data1"];$data2 = $_POST["data2"];$fuhao = $_POST["fuh
mathchao 2020-09-15
Zaratustra 2020-07-29
zhaowj00 2020-07-26
ldcwang 2020-06-25
拿什么来拯救自己 2020-06-21
赵家小少爷 2020-06-14
大牛牛 2020-06-14