Linux shell自動讀mongo數據、遠程獲取文件大小示例腳本

一、示例1mongodb

功能:對mongoDB導出數據,根據sid的不一樣狀態進行統計shell

技術點:shell bash  讀寫文件、字符串截取、函數、用多個文件提到的map、grep查找並賦值給變量bash

#!/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 

二、示例ssh

功能:根據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
相關文章
相關標籤/搜索