1、示例1
功能:对mongoDB导出数据,根据sid的不同状态进行统计
技术点:shell bash 读写文件、字符串截取、函数、用多个文件提到的map、grep查找并赋值给变量
#!/bin/bash
i=1
file=/Users/jifeng/Downloads/diu.txt
read_recorde(){
findstr=$(grep "$1" tmp.sid.1)
find_sid=${findstr:0:20}
find_status=${findstr:23:2}
find_chanel=${findstr:21:1}
find_say=${findstr:25}
echo "$findstr :have" >> tmp.sid.finded
if [[ $find_status == 'nu' ]] && [[ $3 == 'nu' ]];
then
echo "$find_sid:$find_chanel:$find_status:$find_say" >> tmp.sid.2null
echo "$1:$2:$3:$4" >> tmp.sid.2null
elif [[ $find_status == 'nu' ]] && [[ $3 == 'ok' ]];
then
echo "$find_sid:$find_chanel:$find_status:$find_say" >> tmp.sid.1null
echo "$1:$2:$3:$4" >> tmp.sid.1null
elif [[ $find_status == 'ok' ]] && [[ $3 == 'nu' ]];
then
echo "$find_sid:$find_chanel:$find_status:$find_say" >> tmp.sid.1null
echo "$1:$2:$3:$4" >> tmp.sid.1null
elif [[ $find_status == 'ok' ]] && [[ $3 == 'ok' ]];
then
echo "$find_sid:$find_chanel:$find_status:$find_say" >> tmp.sid.2ok
echo "$1:$2:$3:$4" >> tmp.sid.2ok
else
echo "$find_sid:$find_chanel:$find_status:$find_say" >> tmp.sid.undefind
echo "$1:$2:$3:$4" >> tmp.sid.undefind
fi
}
rm -rf tmp.sid.1null tmp.sid.2null tmp.sid.2ok tmp.sid.undefind tmp.sid.1 tmp.sid.finded tmp.sid.single
touch tmp.sid.1
cat $file | while read line || [[ -n ${line} ]];
do
sid=${line:0:20}
channel=${line:20:1}
status=${line:62:2}
say=${line:64}
echo line $i:$sid:$channel:$status
if grep -q "$sid" tmp.sid.1
then
read_recorde "$sid" "$channel" "$status" "$say"
echo ">>>$findsid"
else
echo "$sid:$channel:$status:$say" >> tmp.sid.1
fi
let "i=$i+1"
done
echo "回归"
cat $file | while read line || [[ -n ${line} ]];
do
_sid=${line:0:20}
if grep -q "$_sid" tmp.sid.finded
then
a=1+1
else
echo $line >> tmp.sid.single
fi
done
2、示例
功能:根据ID调用mongo client进行查询并将结果存入变量,通过ssh远程登录获取响应的文件大小
#!/bin/bash
i=1
file=/root/1616/tmp.sid.1null
#file=/root/1616/tmp.sid.2ok
rm -rf tmp.sid.1null.filesize
cat $file | while read line || [[ -n ${line} ]];
do
sid=${line:0:20}
channel=${line:21:1}
args='db.iat.find({"sId":"'${sid}${channel}'"},{"host":1}).forEach(function(e){print(e.host)})'
result=`/data/mongodb/bin/mongo --quiet 172.16.175.68:40000/shtel --eval "$args"`
ip="${result%%:*}"
#echo $ip
ssh_args='ls -lh /data/voice/$sid*'
#echo "ssh_arg:$ssh_args"
#echo "ssh:ssh -p 20000 root@$ip "$ssh_args
#files=`ssh -p -q 20000 root@$ip "$ssh_args"`
files=`ssh -f -p 20000 root@$ip 'ls -lh /data/voice/'$sid$channel'*'`
#echo "file:"$files
fileSize=`echo $files | awk -F' ' '{print $5}'`
#echo "filesize:"$fileSize
echo "line $i $line:$ip:$fileSize"
echo "$line:$ip:$fileSize" >> tmp.sid.1null.filesize
let "i=$i+1"
done